Class: OpenWeatherClient::Request
- Inherits:
-
Object
- Object
- OpenWeatherClient::Request
- Defined in:
- lib/open_weather_client/request.rb
Overview
Request the weather from OpenWeatherMap
Class Method Summary collapse
- .connection(lat, lon) ⇒ Object
-
.get(lat:, lon:, time: Time.now) ⇒ Object
Request the current weather.
- .headers ⇒ Object
- .params(lat, lon) ⇒ Object
- .path ⇒ Object
Class Method Details
.connection(lat, lon) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/open_weather_client/request.rb', line 32 def self.connection(lat, lon) Faraday.new( url: OpenWeatherClient.configuration.url, params: params(lat, lon), headers: headers ) do |f| f.response :raise_error f.response :json end end |
.get(lat:, lon:, time: Time.now) ⇒ Object
Request the current weather
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/open_weather_client/request.rb', line 21 def self.get(lat:, lon:, time: Time.now) raise OpenWeatherClient::NotCurrentError if time < Time.now - 1 * 60 * 60 begin response = connection(lat, lon).get(path) OpenWeatherClient.cache.store(lat: lat, lon: lon, data: response.body, time: time) rescue Faraday::UnauthorizedError raise OpenWeatherClient::AuthenticationError end end |
.headers ⇒ Object
43 44 45 46 47 |
# File 'lib/open_weather_client/request.rb', line 43 def self.headers { 'User-Agent': OpenWeatherClient.configuration.user_agent } end |
.params(lat, lon) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/open_weather_client/request.rb', line 49 def self.params(lat, lon) { appid: OpenWeatherClient.configuration.appid, lat: lat, lon: lon, lang: OpenWeatherClient.configuration.lang, units: OpenWeatherClient.configuration.units } end |
.path ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/open_weather_client/request.rb', line 59 def self.path case OpenWeatherClient.configuration.api_version when :v25 '2.5/weather' when :v30 '3.0/onecall' else raise OpenWeatherClient::APIVersionNotSupportedError end end |