Class: OpenWeatherClient::Weather

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

Overview

Request weather information from OpenWeatherMap or the cache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat:, lon:, time: Time.now) ⇒ Weather

Initialize a new Weather request

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

Raises:

  • (RangeError)

    if one lat or lon are out of the expected ranges



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/open_weather_client/weather.rb', line 22

def initialize(lat:, lon:, time: Time.now)
  if OpenWeatherClient.configuration.spatial_quantization.respond_to?(:call)
    lat, lon = OpenWeatherClient.configuration.spatial_quantization.call(lat, lon)
  end

  raise RangeError unless (-90..90).member?(lat)
  raise RangeError unless (-180..180).member?(lon)

  @lat = lat
  @lon = lon
  @time = time
end

Instance Attribute Details

#latObject

Float

latitude of the requested location



8
9
10
# File 'lib/open_weather_client/weather.rb', line 8

def lat
  @lat
end

#lonObject

Float

longitude of the requested location



10
11
12
# File 'lib/open_weather_client/weather.rb', line 10

def lon
  @lon
end

#timeObject

Time

time of the requested weather



12
13
14
# File 'lib/open_weather_client/weather.rb', line 12

def time
  @time
end

Instance Method Details

#getObject

get the weather according to the specified parameters

Returns:

  • the stored or received data

Raises:

  • (AuthenticationError)

    if the request is not authorized, e.g in case the API key is not correct



41
42
43
44
45
# File 'lib/open_weather_client/weather.rb', line 41

def get
  OpenWeatherClient.cache.get(lat: lat, lon: lon, time: time)
rescue KeyError
  OpenWeatherClient::Request.get(lat: lat, lon: lon, time: time)
end