Class: OpenWeatherClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/open_weather_client/request.rb

Overview

Request the weather from OpenWeatherMap

Class Method Summary collapse

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

Parameters:

  • lat (Float)

    latitude of the requests location

  • lon (Float)

    longitude of the requests location

  • time (Time) (defaults to: Time.now)

    time of the request

Returns:

  • the response body

Raises:



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

.headersObject



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

.pathObject



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