/
client.rb
75 lines (62 loc) · 1.69 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
require 'jwt'
require 'openssl'
require 'httparty'
require_relative './weather_response'
module Tenkit
class Client
include HTTParty
base_uri 'https://weatherkit.apple.com/api/v1'
DATA_SETS = {
current_weather: 'currentWeather',
forecast_daily: 'forecastDaily',
forecast_hourly: 'forecastHourly',
trend_comparison: 'trendComparison',
weather_alerts: 'weatherAlerts',
forecast_next_hour: 'forecastNextHour'
}.freeze
def initialize
Tenkit.config.validate!
end
def availability(lat, lon, country: 'US')
get("/availability/#{lat}/#{lon}?country=#{country}")
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)
WeatherResponse.new(response)
end
def weather_alert(id, language: 'en')
puts 'TODO: implement weather alert endpoint'
puts language
puts id
end
private
def get(url)
headers = { Authorization: "Bearer #{token}" }
self.class.get(url, { headers: headers })
end
def header
{
alg: 'ES256',
kid: Tenkit.config.key_id,
id: "#{Tenkit.config.team_id}.#{Tenkit.config.service_id}"
}
end
def payload
{
iss: Tenkit.config.team_id,
iat: Time.new.to_i,
exp: Time.new.to_i + 600,
sub: Tenkit.config.service_id
}
end
def key
OpenSSL::PKey.read Tenkit.config.key
end
def token
JWT.encode payload, key, 'ES256', header
end
end
end