Class: OldBill::Session

Inherits:
Object
  • Object
show all
Includes:
Api::Crime, Api::Neighbourhood
Defined in:
lib/oldbill/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • api_key (String)
  • options (Hash)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oldbill/session.rb', line 29

def initialize(username, password, options)
  @username = username
  @password = password
  if @caching = options[:caching].nil? ? true : options.delete(:caching)
    @expires_in = options.delete(:expires_in) || 60*60*24
    @cache = options.delete(:cache) || Moneta::Memory.new 
  end
  @server = options.delete(:server) || "policeapi2.rkh.co.uk/api"
  @api_version = options.delete(:api_version) || 2
  @logging = options[:logging].nil? ? false : options.delete(:logging)
end

Instance Attribute Details

#api_versionObject

accessors



5
6
7
# File 'lib/oldbill/session.rb', line 5

def api_version
  @api_version
end

#cacheObject

accessors



5
6
7
# File 'lib/oldbill/session.rb', line 5

def cache
  @cache
end

#cachingObject

accessors



5
6
7
# File 'lib/oldbill/session.rb', line 5

def caching
  @caching
end

#expires_inObject

accessors



5
6
7
# File 'lib/oldbill/session.rb', line 5

def expires_in
  @expires_in
end

#loggingObject

accessors



5
6
7
# File 'lib/oldbill/session.rb', line 5

def logging
  @logging
end

#serverObject

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

Parameters:

  • options (Hash) (defaults to: {})

Returns:

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/oldbill/session.rb', line 13

def self.create(options = {})
  username = options.delete(:username)
  password = options.delete(:password)
  raise ArgumentError.new("You need a username password") unless username && password
  new(username, password, options)
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

Returns:

  • (Boolean)


56
57
58
# File 'lib/oldbill/session.rb', line 56

def caching?
  @caching
end

#serviceObject



52
53
54
# File 'lib/oldbill/session.rb', line 52

def service 
  @service ||= OldBill::Service.new(@server, @api_version, @username, @password, @logging)
end