Sha256: 288ece31534596013886e7724865119c6df4e4d6d0f3cc3ae524c5f095289823

Contents?: true

Size: 937 Bytes

Versions: 7

Compression:

Stored size: 937 Bytes

Contents

require 'booking_locations/version'
require 'booking_locations/api'
require 'booking_locations/location'

require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/cache/null_store'

module BookingLocations
  DEFAULT_PREFIX = 'booking_locations:'.freeze
  DEFAULT_TTL = 2 * 60 * 60 # 2 hours

  mattr_writer :api
  mattr_writer :cache

  def self.api
    @@api ||= BookingLocations::Api.new
  end

  def self.cache
    @@cache ||= ActiveSupport::Cache::NullStore.new
  end

  def self.find(id, expires = DEFAULT_TTL)
    cache.fetch(cache_prefix(id), expires_in: expires) do
      api.get(id) do |response_hash|
        Location.new(response_hash)
      end
    end
  end

  def self.all
    api.all.map do |location|
      Location.new(location)
    end
  end

  def self.clear_cache
    cache.delete_matched(cache_prefix('*'))
  end

  def self.cache_prefix(key)
    DEFAULT_PREFIX.dup.concat(key)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
booking_locations-0.24.0 lib/booking_locations.rb
booking_locations-0.23.0 lib/booking_locations.rb
booking_locations-0.22.0 lib/booking_locations.rb
booking_locations-0.21.0 lib/booking_locations.rb
booking_locations-0.20.0 lib/booking_locations.rb
booking_locations-0.19.0 lib/booking_locations.rb
booking_locations-0.18.0 lib/booking_locations.rb