Class: OldBill::Session
- Inherits:
-
Object
- Object
- OldBill::Session
- Includes:
- Api::Crime, Api::Neighbourhood
- Defined in:
- lib/oldbill/session.rb
Instance Attribute Summary collapse
-
#api_version ⇒ Object
accessors.
-
#cache ⇒ Object
accessors.
-
#caching ⇒ Object
accessors.
-
#expires_in ⇒ Object
accessors.
-
#logging ⇒ Object
accessors.
-
#server ⇒ Object
accessors.
Class Method Summary collapse
-
.create(options = {}) ⇒ OldBill::Session
creating a session one must supply the api_key as this is always required basically.
Instance Method Summary collapse
- #api_call(method, path, params = {}, &proc) ⇒ Object
- #caching? ⇒ Boolean
-
#initialize(username, password, options) ⇒ Session
constructor
default options caching => true expires_in => 60*60*24 cache => Moneta::Memory.new server => “policeapi2.rkh.co.uk/api/” server has api version hmm should be param! api_version => 2 logging => true.
- #service ⇒ Object
Methods included from Api::Neighbourhood
#events, #locate, #neighbourhood, #neighbourhoods, #police_officers
Methods included from Api::Crime
#crime_categories, #crimes_by_month, #force, #forces, #street_level_crimes
Constructor Details
#initialize(username, password, options) ⇒ Session
default options
caching => true
expires_in => 60*60*24
cache => Moneta::Memory.new
server => "policeapi2.rkh.co.uk/api/" server has api version hmm should be param!
api_version => 2
logging => true
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/oldbill/session.rb', line 29 def initialize(username, password, ) @username = username @password = password if @caching = [:caching].nil? ? true : .delete(:caching) @expires_in = .delete(:expires_in) || 60*60*24 @cache = .delete(:cache) || Moneta::Memory.new end @server = .delete(:server) || "policeapi2.rkh.co.uk/api" @api_version = .delete(:api_version) || 2 @logging = [:logging].nil? ? false : .delete(:logging) end |
Instance Attribute Details
#api_version ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def api_version @api_version end |
#cache ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def cache @cache end |
#caching ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def caching @caching end |
#expires_in ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def expires_in @expires_in end |
#logging ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def logging @logging end |
#server ⇒ Object
accessors
5 6 7 |
# File 'lib/oldbill/session.rb', line 5 def server @server end |
Class Method Details
.create(options = {}) ⇒ OldBill::Session
creating a session one must supply the api_key as this is always required basically
13 14 15 16 17 18 |
# File 'lib/oldbill/session.rb', line 13 def self.create( = {}) username = .delete(:username) password = .delete(:password) raise ArgumentError.new("You need a username password") unless username && password new(username, password, ) end |
Instance Method Details
#api_call(method, path, params = {}, &proc) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/oldbill/session.rb', line 41 def api_call(method, path, params ={}, &proc) if cache = caching? && (@cache[cache_key(path, params)]) return cache else result = service.send(method, path, params) result = yield result if block_given? store_call(result, path, params || {}) if caching? result end end |
#caching? ⇒ Boolean
56 57 58 |
# File 'lib/oldbill/session.rb', line 56 def caching? @caching end |
#service ⇒ Object
52 53 54 |
# File 'lib/oldbill/session.rb', line 52 def service @service ||= OldBill::Service.new(@server, @api_version, @username, @password, @logging) end |