Class: Chargify::Subscription

Inherits:
Base
  • Object
show all
Defined in:
lib/chargify/subscription.rb

Instance Attribute Summary

Attributes inherited from Base

#errors

Class Method Summary collapse

Methods inherited from Base

api_request, #api_request, #attributes=, #initialize

Constructor Details

This class inherits a constructor from Chargify::Base

Class Method Details

.allObject



6
7
8
9
# File 'lib/chargify/subscription.rb', line 6

def all
  result = api_request(:get, "/subscriptions.json")
  result.map{|p| Hashie::Mash.new p['subscription']}
end

.cancel(sub_id, message = "") ⇒ Object



53
54
55
56
57
# File 'lib/chargify/subscription.rb', line 53

def cancel(sub_id, message="")
  cancel!(sub_id, message)
rescue Chargify::Error::Base => e
  return false
end

.cancel!(sub_id, message = "") ⇒ Object



48
49
50
51
# File 'lib/chargify/subscription.rb', line 48

def cancel!(sub_id, message="")
  result = api_request(:delete, "/subscriptions/#{sub_id}.json", :body => {:subscription => {:cancellation_message => message} })
  true if result.code == 200
end

.charge(sub_id, subscription_attributes = {}) ⇒ Object



77
78
79
80
81
# File 'lib/chargify/subscription.rb', line 77

def charge(sub_id, subscription_attributes={})
  charge!(sub_id, subscription_attributes)
rescue Chargify::Error::Base => e
  return false
end

.charge!(sub_id, subscription_attributes = {}) ⇒ Object



71
72
73
74
75
# File 'lib/chargify/subscription.rb', line 71

def charge!(sub_id, subscription_attributes={})
  result = api_request(:post, "/subscriptions/#{sub_id}/charges.json", :body => { :charge => subscription_attributes })
  response = Hashie::Mash.new(result)
  response.charge
end

.component_usage(subscription_id, component_id) ⇒ Object



145
146
147
148
149
# File 'lib/chargify/subscription.rb', line 145

def component_usage(subscription_id, component_id)
  component_usage!(subscription_id, component_id)
rescue Chargify::Error::Base => e
  return false
end

.component_usage!(subscription_id, component_id) ⇒ Object



139
140
141
142
143
# File 'lib/chargify/subscription.rb', line 139

def component_usage!(subscription_id, component_id)
  result = api_request(:get, "/subscriptions/#{subscription_id}/components/#{component_id}/usages.json")
  response = Hashie::Mash.new(result)
  response
end

.components(subscription_id) ⇒ Object



111
112
113
114
115
# File 'lib/chargify/subscription.rb', line 111

def components(subscription_id)
  components!(subscription_id)
rescue Chargify::Error::Base => e
  return false
end

.components!(subscription_id) ⇒ Object



106
107
108
109
# File 'lib/chargify/subscription.rb', line 106

def components!(subscription_id)
  result = api_request(:get, "/subscriptions/#{subscription_id}/components.json")
  result.map{|c| Hashie::Mash.new c['component']}
end

.create(subscription_attributes = {}) ⇒ Object



30
31
32
33
34
# File 'lib/chargify/subscription.rb', line 30

def create(subscription_attributes={})
  create!(subscription_attributes)
rescue Chargify::Error::Base => e
  return false
end

.create!(subscription_attributes = {}) ⇒ Object



24
25
26
27
28
# File 'lib/chargify/subscription.rb', line 24

def create!(subscription_attributes={})
  result = api_request(:post, "/subscriptions.json", :body => {:subscription => subscription_attributes})
  response = Hashie::Mash.new(result)
  response.subscription
end

.find(id) ⇒ Object



18
19
20
21
22
# File 'lib/chargify/subscription.rb', line 18

def find(id)
  find!(id)
rescue Chargify::Error::Base => e
  return nil
end

.find!(id) ⇒ Object



11
12
13
14
15
16
# File 'lib/chargify/subscription.rb', line 11

def find!(id)
  return all if id == :all

  result = api_request(:get, "/subscriptions/#{id}.json")
  Hashie::Mash.new(result).subscription
end

.find_component(subscription_id, component_id) ⇒ Object



122
123
124
125
126
# File 'lib/chargify/subscription.rb', line 122

def find_component(subscription_id, component_id)
  find_component!(subscription_id, component_id)
rescue Chargify::Error::Base => e
  return false
end

.find_component!(subscription_id, component_id) ⇒ Object



117
118
119
120
# File 'lib/chargify/subscription.rb', line 117

def find_component!(subscription_id, component_id)
  result = api_request(:get, "/subscriptions/#{subscription_id}/components/#{component_id}.json")
  Hashie::Mash.new(result).component
end

.migrate(sub_id, product_id) ⇒ Object



89
90
91
92
93
# File 'lib/chargify/subscription.rb', line 89

def migrate(sub_id, product_id)
  migrate!(sub_id, product_id)
rescue Chargify::Error::Base => e
  return false
end

.migrate!(sub_id, product_id) ⇒ Object



83
84
85
86
87
# File 'lib/chargify/subscription.rb', line 83

def migrate!(sub_id, product_id)
  result = api_request(:post, "/subscriptions/#{sub_id}/migrations.json", :body => {:product_id => product_id })
  response = Hashie::Mash.new(result)
  response.subscription
end

.reactivate(sub_id) ⇒ Object



65
66
67
68
69
# File 'lib/chargify/subscription.rb', line 65

def reactivate(sub_id)
  reactivate!(sub_id)
rescue Chargify::Error::Base => e
  return false
end

.reactivate!(sub_id) ⇒ Object



59
60
61
62
63
# File 'lib/chargify/subscription.rb', line 59

def reactivate!(sub_id)
  result = api_request(:put, "/subscriptions/#{sub_id}/reactivate.json", :body => "")
  response = Hashie::Mash.new(result)
  response.subscription
end

.transactions(sub_id, options = {}) ⇒ Object



100
101
102
103
104
# File 'lib/chargify/subscription.rb', line 100

def transactions(sub_id, options={})
  transactions!(sub_id, options)
rescue Chargify::Error::Base => e
  return false
end

.transactions!(sub_id, options = {}) ⇒ Object



95
96
97
98
# File 'lib/chargify/subscription.rb', line 95

def transactions!(sub_id, options={})
  result = api_request(:get, "/subscriptions/#{sub_id}/transactions.json", :query => options)
  result.map{|t| Hashie::Mash.new t['transaction']}
end

.update(sub_id, subscription_attributes = {}) ⇒ Object



42
43
44
45
46
# File 'lib/chargify/subscription.rb', line 42

def update(sub_id, subscription_attributes={})
  update!(sub_id, subscription_attributes)
rescue Chargify::Error::Base => e
  return false
end

.update!(sub_id, subscription_attributes = {}) ⇒ Object



36
37
38
39
40
# File 'lib/chargify/subscription.rb', line 36

def update!(sub_id, subscription_attributes={})
  result = api_request(:put, "/subscriptions/#{sub_id}.json", :body => {:subscription => subscription_attributes})
  response = Hashie::Mash.new(result)
  response.subscription
end

.update_component(subscription_id, component_id, quantity) ⇒ Object



133
134
135
136
137
# File 'lib/chargify/subscription.rb', line 133

def update_component(subscription_id, component_id, quantity)
  update_component!(subscription_id, component_id, quantity)
rescue Chargify::Error::Base => e
  return false
end

.update_component!(subscription_id, component_id, quantity) ⇒ Object



128
129
130
131
# File 'lib/chargify/subscription.rb', line 128

def update_component!(subscription_id, component_id, quantity)
  result = api_request(:put, "/subscriptions/#{subscription_id}/components/#{component_id}.json", :body => {:component => {:allocated_quantity => quantity}})
  Hashie::Mash.new(result)
end