Class: OpenWeatherClient::Caching

Inherits:
Object
  • Object
show all
Defined in:
lib/open_weather_client/caching.rb,
lib/open_weather_client/caching/memory.rb

Overview

Caching of OpenWeatherMap requests

The entries are cached according to latitude, longitude and time of the request. The time is clamped to the current hour

This is the caching interface and equals a none cache. Get requests raise an error.

Direct Known Subclasses

Memory

Defined Under Namespace

Classes: Memory

Instance Method Summary collapse

Instance Method Details

#cache_key(lat, lon, time) ⇒ Object

Calculate the key for storage in the cache

Parameters:

  • lat (Float)

    latitude of the requests location

  • lon (Float)

    longitude of the requests location

  • time (Time)

    time of the request



53
54
55
# File 'lib/open_weather_client/caching.rb', line 53

def cache_key(lat, lon, time)
  "weather:#{lat}:#{lon}:#{time.strftime('%Y-%m-%dT%H')}"
end

#get(lat:, lon:, time:) ⇒ Hash

Get an entry out of the cache defined by its lat, lon and time.

Parameters:

  • lat (Float)

    latitude of the requests location

  • lon (Float)

    longitude of the requests location

  • time (Time)

    time of the request

Returns:

  • (Hash)

    the stored data

Raises:

  • (KeyError)

    if no entry is present

  • (NotImplementedError)

    if an unsupported caching method is used



23
24
25
26
27
28
# File 'lib/open_weather_client/caching.rb', line 23

def get(lat:, lon:, time:)
  key = cache_key(lat, lon, time)
  raise KeyError unless present?(key)

  caching_get(key)
end

#store(data:, lat:, lon:, time:) ⇒ Hash

Store the data by its lat, lon and time.

Parameters:

  • data (Hash)

    data to be stored, must be able to be formatted and parsed as text

  • lat (Float)

    latitude of the requests location

  • lon (Float)

    longitude of the requests location

  • time (Time)

    time of the request

Returns:

  • (Hash)

    the input data



39
40
41
42
43
44
45
# File 'lib/open_weather_client/caching.rb', line 39

def store(data:, lat:, lon:, time:)
  key = cache_key(lat, lon, time)

  caching_store(key, data)

  data
end