Skip to content

Commit e691ed9

Browse files
committed
Client: Improve query params
Adds better handling of query parameters using HTTParty instead of manually. Fills in the gaps in the supported WeatherKit query params.
1 parent b38c900 commit e691ed9

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

lib/tenkit/client.rb

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ def initialize
2424
Tenkit.config.validate!
2525
end
2626

27-
def availability(lat, lon, country: 'US')
28-
get("/availability/#{lat}/#{lon}?country=#{country}")
27+
def availability(lat, lon, **options)
28+
options[:country] ||= 'US'
29+
30+
query = { country: options[:country] }
31+
get("/availability/#{lat}/#{lon}", query: query)
2932
end
3033

31-
def weather(lat, lon, data_sets: [:current_weather], language: 'en')
32-
path_root = "/weather/#{language}/#{lat}/#{lon}?dataSets="
33-
path = path_root + data_sets.map { |ds| DATA_SETS[ds] }.compact.join(',')
34-
response = get(path)
34+
def weather(lat, lon, **options)
35+
options[:data_sets] ||= [:current_weather]
36+
options[:language] ||= 'en'
37+
38+
query = weather_query_for_options(options)
39+
path = "/weather/#{options[:language]}/#{lat}/#{lon}"
40+
41+
response = get(path, query: query)
3542
WeatherResponse.new(response)
3643
end
3744

@@ -43,9 +50,15 @@ def weather_alert(id, language: 'en')
4350

4451
private
4552

46-
def get(url)
53+
def get(url, query: nil)
4754
headers = { Authorization: "Bearer #{token}" }
48-
self.class.get(url, { headers: headers })
55+
params = { headers: headers }
56+
57+
if !query.nil?
58+
params[:query] = query
59+
end
60+
61+
self.class.get(url, params)
4962
end
5063

5164
def header
@@ -72,5 +85,22 @@ def key
7285
def token
7386
JWT.encode payload, key, 'ES256', header
7487
end
88+
89+
# Snake case options to expected query parameters
90+
# https://developer.apple.com/documentation/weatherkitrestapi/get-api-v1-weather-_language_-_latitude_-_longitude_#query-parameters
91+
def weather_query_for_options(options)
92+
data_sets_param = options[:data_sets].map { |ds| DATA_SETS[ds] }.compact.join(',')
93+
94+
{
95+
countryCode: options[:country_code],
96+
currentAsOf: options[:current_as_of],
97+
dailyEnd: options[:daily_end],
98+
dailyStart: options[:daily_start],
99+
dataSets: data_sets_param,
100+
hourlyEnd: options[:hourly_end],
101+
hourlyStart: options[:hourly_start],
102+
timezone: options[:timezone]
103+
}.compact
104+
end
75105
end
76106
end

spec/tenkit/tenkit/weather_spec.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
RSpec.describe Tenkit::Weather do
44
let(:lat) { 37.32 }
55
let(:lon) { 122.03 }
6+
let(:path) { "/weather/en/#{lat}/#{lon}" }
7+
let(:data_sets) { [Tenkit::Utils.snake(data_set).to_sym] }
8+
let(:query) { {query: {dataSets: data_set}} }
9+
610
let(:client) { Tenkit::Client.new }
711
let(:api_response) { double("WeatherResponse", body: json) }
812
let(:json) { File.read("test/fixtures/#{data_set}.json") }
@@ -12,7 +16,12 @@
1216
describe "currentWeather" do
1317
let(:data_set) { "currentWeather" }
1418

15-
subject { client.weather(lat, lon).weather.current_weather }
19+
subject { client.weather(lat, lon, data_sets: data_sets).weather.current_weather }
20+
21+
it "returns response from correct data_sets" do
22+
subject
23+
expect(client).to have_received(:get).with(path, query)
24+
end
1625

1726
it "includes expected metadata" do
1827
expect(subject.name).to eq "CurrentWeather"
@@ -30,16 +39,26 @@
3039
expect(subject.temperature).to be(-5.68)
3140
expect(subject.temperature_apparent).to be(-6.88)
3241
end
42+
43+
context "without options" do
44+
subject { client.weather(lat, lon).weather.current_weather }
45+
46+
it "returns response from default currentWeather data set" do
47+
expect(subject.name).to eq "CurrentWeather"
48+
expect(client).to have_received(:get).with(path, query)
49+
end
50+
end
3351
end
3452

3553
describe "forecastDaily" do
3654
let(:data_set) { "forecastDaily" }
3755
let(:first_day) { subject.days.first }
3856

39-
subject { client.weather(lat, lon).weather.forecast_daily }
57+
subject { client.weather(lat, lon, data_sets: data_sets).weather.forecast_daily }
4058

41-
it "returns 10 days of data" do
59+
it "returns 10 days of data from correct data sets" do
4260
expect(subject.days.size).to be 10
61+
expect(client).to have_received(:get).with(path, query)
4362
end
4463

4564
it "returns correct object types" do
@@ -80,10 +99,11 @@
8099
let(:data_set) { "forecastHourly" }
81100
let(:first_hour) { subject.hours.first }
82101

83-
subject { client.weather(lat, lon).weather.forecast_hourly }
102+
subject { client.weather(lat, lon, data_sets: data_sets).weather.forecast_hourly }
84103

85-
it "returns 25 hours of data" do
104+
it "returns 25 hours of data from correct data sets" do
86105
expect(subject.hours.size).to be 25
106+
expect(client).to have_received(:get).with(path, query)
87107
end
88108

89109
it "includes expected metadata" do

0 commit comments

Comments
 (0)