Skip to content

Client: Improve query params #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions lib/tenkit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ def initialize
Tenkit.config.validate!
end

def availability(lat, lon, country: 'US')
get("/availability/#{lat}/#{lon}?country=#{country}")
def availability(lat, lon, **options)
options[:country] ||= 'US'

query = { country: options[:country] }
get("/availability/#{lat}/#{lon}", query: query)
end

def weather(lat, lon, data_sets: [:current_weather], language: 'en')
path_root = "/weather/#{language}/#{lat}/#{lon}?dataSets="
path = path_root + data_sets.map { |ds| DATA_SETS[ds] }.compact.join(',')
response = get(path)
def weather(lat, lon, **options)
options[:data_sets] ||= [:current_weather]
options[:language] ||= 'en'

query = weather_query_for_options(options)
path = "/weather/#{options[:language]}/#{lat}/#{lon}"

response = get(path, query: query)
WeatherResponse.new(response)
end

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

private

def get(url)
def get(url, query: nil)
headers = { Authorization: "Bearer #{token}" }
self.class.get(url, { headers: headers })
params = { headers: headers }

if !query.nil?
params[:query] = query
end

self.class.get(url, params)
end

def header
Expand All @@ -72,5 +85,22 @@ def key
def token
JWT.encode payload, key, 'ES256', header
end

# Snake case options to expected query parameters
# https://developer.apple.com/documentation/weatherkitrestapi/get-api-v1-weather-_language_-_latitude_-_longitude_#query-parameters
def weather_query_for_options(options)
data_sets_param = options[:data_sets].map { |ds| DATA_SETS[ds] }.compact.join(',')

{
countryCode: options[:country_code],
currentAsOf: options[:current_as_of],
dailyEnd: options[:daily_end],
dailyStart: options[:daily_start],
dataSets: data_sets_param,
hourlyEnd: options[:hourly_end],
hourlyStart: options[:hourly_start],
timezone: options[:timezone]
}.compact
end
end
end
30 changes: 25 additions & 5 deletions spec/tenkit/tenkit/weather_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
RSpec.describe Tenkit::Weather do
let(:lat) { 37.32 }
let(:lon) { 122.03 }
let(:path) { "/weather/en/#{lat}/#{lon}" }
let(:data_sets) { [Tenkit::Utils.snake(data_set).to_sym] }
let(:query) { {query: {dataSets: data_set}} }

let(:client) { Tenkit::Client.new }
let(:api_response) { double("WeatherResponse", body: json) }
let(:json) { File.read("test/fixtures/#{data_set}.json") }
Expand All @@ -12,7 +16,12 @@
describe "currentWeather" do
let(:data_set) { "currentWeather" }

subject { client.weather(lat, lon).weather.current_weather }
subject { client.weather(lat, lon, data_sets: data_sets).weather.current_weather }

it "returns response from correct data_sets" do
subject
expect(client).to have_received(:get).with(path, query)
end

it "includes expected metadata" do
expect(subject.name).to eq "CurrentWeather"
Expand All @@ -30,16 +39,26 @@
expect(subject.temperature).to be(-5.68)
expect(subject.temperature_apparent).to be(-6.88)
end

context "without options" do
subject { client.weather(lat, lon).weather.current_weather }

it "returns response from default currentWeather data set" do
expect(subject.name).to eq "CurrentWeather"
expect(client).to have_received(:get).with(path, query)
end
end
end

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

subject { client.weather(lat, lon).weather.forecast_daily }
subject { client.weather(lat, lon, data_sets: data_sets).weather.forecast_daily }

it "returns 10 days of data" do
it "returns 10 days of data from correct data sets" do
expect(subject.days.size).to be 10
expect(client).to have_received(:get).with(path, query)
end

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

subject { client.weather(lat, lon).weather.forecast_hourly }
subject { client.weather(lat, lon, data_sets: data_sets).weather.forecast_hourly }

it "returns 25 hours of data" do
it "returns 25 hours of data from correct data sets" do
expect(subject.hours.size).to be 25
expect(client).to have_received(:get).with(path, query)
end

it "includes expected metadata" do
Expand Down
Loading