Class: Weathercom::Client
- Inherits:
-
Object
- Object
- Weathercom::Client
show all
- Defined in:
- lib/weathercom/client.rb
Defined Under Namespace
Classes: ApiError, ApiKeyScrapeError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(api_key: nil, cache: nil) ⇒ Client
Returns a new instance of Client.
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/weathercom/client.rb', line 16
def initialize(api_key: nil, cache: nil)
@configured_api_key = api_key
@cache = cache
@connection ||= Faraday.new("https://api.weather.com") do |f|
f.request :url_encoded
f.adapter Faraday.default_adapter
f.['user-agent'] = 'Mozilla/5.0 (Compatible)'
end
if api_key.nil?
@api_key = cache.get('weathercom:api_key')
end
end
|
Instance Attribute Details
Returns the value of attribute configured_api_key.
30
31
32
|
# File 'lib/weathercom/client.rb', line 30
def configured_api_key
@configured_api_key
end
|
#connection ⇒ Object
Returns the value of attribute connection.
31
32
33
|
# File 'lib/weathercom/client.rb', line 31
def connection
@connection
end
|
Instance Method Details
#api_key ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/weathercom/client.rb', line 33
def api_key
configured_api_key or begin
@api_key ||= scrape_api_key
if @cache
@cache.set('weathercom:api_key', @api_key)
end
@api_key
end
end
|
#geocode(query, ttl: nil) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/weathercom/client.rb', line 111
def geocode(query, ttl: nil)
if @cache && ttl
cache_key = "weathercom:geocode:#{query}"
result = @cache.get(cache_key)
if result && result['expires_at'] && result['expires_at'] > Time.now.to_i
return GeocodedLocation.new(result['location'], self)
end
end
payload = raw_geocode(query)
if @cache && ttl
@cache.set(cache_key, 'expires_at' => Time.now.to_i + ttl, 'location' => payload)
end
GeocodedLocation.new(payload, self)
end
|
#get_json(url) ⇒ Object
50
51
52
|
# File 'lib/weathercom/client.rb', line 50
def get_json(url)
request_json(:get, url)
end
|
#get_json_with_cache(url) ⇒ Object
54
55
56
|
# File 'lib/weathercom/client.rb', line 54
def get_json_with_cache(url)
request_json_with_cache(:get, url)
end
|
#location(lat, lng) ⇒ Object
129
130
131
|
# File 'lib/weathercom/client.rb', line 129
def location(lat, lng)
Location.new(lat, lng, self)
end
|
#request_json(meth, url) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/weathercom/client.rb', line 58
def request_json(meth, url)
attempt = 1
loop do
if url.include?('?')
full_url = "#{url}&apiKey=#{URI.encode(api_key)}"
else
full_url = "#{url}?apiKey=#{URI.encode(api_key)}"
end
response = connection.send(meth) do |req|
req.url(full_url)
end
if response.status == 401 && configured_api_key.nil? && attempt == 1
clear_api_key
attempt += 1
next
end
unless response.status == 200
error = nil
begin
error = JSON.parse(response.body)['error']
rescue
end
msg = "Weathercom #{meth.to_s.upcase} #{url} failed: #{response.status}"
if error
msg += ": #{error}"
end
raise ApiError.new(msg, status: response.status)
end
return JSON.parse(response.body)
end
end
|
#request_json_with_cache(meth, url) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/weathercom/client.rb', line 91
def request_json_with_cache(meth, url)
cache_key = "weathercom:#{meth}:#{url}"
if @cache && (data = @cache.get(cache_key))
if data.key?('metadata') && data['metadata'].key?('expire_time_gmt') &&
data['metadata']['expire_time_gmt'] > Time.now.to_i
then
return data
end
@cache.set(cache_key, nil)
end
request_json(meth, url).tap do |data|
if @cache
@cache.set(cache_key, data)
end
end
end
|