Class: Aptible::BillForward::Resource

Inherits:
Sawyer::Resource
  • Object
show all
Defined in:
lib/aptible/billforward/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(query = {}) ⇒ Object



6
7
8
# File 'lib/aptible/billforward/resource.rb', line 6

def self.all(query = {})
  client.get collection_path, query: query
end

.basenameObject



31
32
33
# File 'lib/aptible/billforward/resource.rb', line 31

def self.basename
  name.split('::').last.underscore.dasherize.pluralize
end

.by_subscription_id(subscription_id, options = {}) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/aptible/billforward/resource.rb', line 53

def self.by_subscription_id(subscription_id, options = {})
  state = options[:active] ? '/active' : ''
  client.get(
    "#{collection_path}/#{subscription_id}#{state}",
    order: 'DESC', order_by: 'start'
  )
end

.clientObject



35
36
37
# File 'lib/aptible/billforward/resource.rb', line 35

def self.client
  @client ||= Aptible::BillForward::Client.new
end

.collection_pathObject



18
19
20
# File 'lib/aptible/billforward/resource.rb', line 18

def self.collection_path
  basename
end

.create(params, query = {}) ⇒ Object



14
15
16
# File 'lib/aptible/billforward/resource.rb', line 14

def self.create(params, query = {})
  client.post collection_path, new(client.agent, params), query: query
end

.find(id) ⇒ Object



10
11
12
# File 'lib/aptible/billforward/resource.rb', line 10

def self.find(id)
  client.get("#{collection_path}/#{id}").first
end

Instance Method Details

#clientObject



39
40
41
# File 'lib/aptible/billforward/resource.rb', line 39

def client
  @client ||= Aptible::BillForward::Client.new
end

#hrefObject



27
28
29
# File 'lib/aptible/billforward/resource.rb', line 27

def href
  "#{self.class.collection_path}/#{id}"
end

#invoiceObject



43
44
45
46
# File 'lib/aptible/billforward/resource.rb', line 43

def invoice
  return nil unless invoiceID
  Aptible::BillForward::Invoice.find(invoiceID)
end

#serializeObject



61
62
63
64
65
66
# File 'lib/aptible/billforward/resource.rb', line 61

def serialize
  type = self.class.basename.underscore.camelize(:lower).pluralize
  body = {}
  body[type] = [to_attrs]
  body
end

#subscriptionObject



48
49
50
51
# File 'lib/aptible/billforward/resource.rb', line 48

def subscription
  return nil unless subscriptionID
  Aptible::BillForward::Subscription.find(subscriptionID)
end

#update(params) ⇒ Object



22
23
24
25
# File 'lib/aptible/billforward/resource.rb', line 22

def update(params)
  self.attrs = attrs.merge(params)
  client.put href, self
end