Class: BillerBotResource::Resource

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/biller_bot_resource/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Resource

Returns a new instance of Resource.



44
45
46
47
# File 'lib/biller_bot_resource/resource.rb', line 44

def initialize(attributes = {}, persisted = false)
  attributes = enrich_attributes(attributes)
  super(attributes, persisted)
end

Class Method Details

.configObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/biller_bot_resource/resource.rb', line 16

def self.config
  if defined?(@config)
    @config
  elsif superclass != ActiveResource::Base && superclass.respond_to?(:config) && !superclass.config.nil?
    superclass.config
  else
    @config = BillerBotResource::Configuration.new
    @config
  end
end

.configure {|config| ... } ⇒ Object

Allow the resource to be configured via block style.

Yields:



8
9
10
11
12
13
14
# File 'lib/biller_bot_resource/resource.rb', line 8

def self.configure
  yield config

  self.site   = config.site
  self.logger = config.logger if config.logger
  self.timeout = config.timeout
end

.instantiate_collection(collection, prefix_options = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/biller_bot_resource/resource.rb', line 36

def self.instantiate_collection(collection, prefix_options = {})
  return super if collection.is_a? Array
  remote_collection = []
  remote_collection.concat super(collection["results"], prefix_options)
  # TODO: Add additional keys to the remote collection
  remote_collection
end

.query_string(options) ⇒ Object

Append the auth token to all of our queries. The easiest way to do this is to override the query string method and inject it.



30
31
32
33
34
# File 'lib/biller_bot_resource/resource.rb', line 30

def self.query_string(options)
  options ||= {}
  options[:auth_token] = config.api_key
  super(options)
end

Instance Method Details

#cache_keyObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/biller_bot_resource/resource.rb', line 55

def cache_key
  case
  when new_record?
    "#{self.class.name}/new"
  when timestamp = @attributes[:updated_at]
    timestamp = timestamp.utc.to_s(:number)
    "#{self.class.name}/#{id}-#{timestamp}"
  else
    "#{self.class.name}/#{id}"
  end
end

#save(*args) ⇒ Object



49
50
51
52
53
# File 'lib/biller_bot_resource/resource.rb', line 49

def save(*args)
  @attributes.delete :created_at
  @attributes.delete :updated_at
  super
end