lib/bikes.rb in bikes-0.0.2 vs lib/bikes.rb in bikes-0.0.3
- old
+ new
@@ -1,7 +1,8 @@
require 'httparty'
require 'hashie'
+require_relative 'cache'
class BikesOffline < Exception; end
class Bikes
include HTTParty
@@ -10,21 +11,34 @@
CONTRACTS_URL = 'https://api.jcdecaux.com/vls/v1/contracts'
def initialize(apikey, scheme)
@api = apikey
@scheme = scheme
+ # Contracts refresh daily
+ @contracts_cache = Cache.new(self, :contracts_fetch)
+
+ # Full stations list refresh every 60 seconds
+ @stations_cache = Cache.new(self, :stations_fetch, 60)
end
- def stations
- invoke(STATIONS_URL, { contract: @scheme})
+ def station(num)
+ invoke(STATIONS_URL + '/' + num.to_s, {contract: @scheme})
end
def contracts
+ @contracts_cache.contents
+ end
+
+ def contracts_fetch
invoke(CONTRACTS_URL)
end
- def station(num)
- invoke(STATIONS_URL + '/' + num.to_s, {contract: @scheme})
+ def stations
+ @stations_cache.contents
+ end
+
+ def stations_fetch
+ invoke(STATIONS_URL, { contract: @scheme })
end
private
def invoke(url, queryx = {})