@@ -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
76106end
0 commit comments