Class: Spreedly::Subscriptions::Subscriber

Inherits:
Resource
  • Object
show all
Defined in:
lib/spreedly/subscriptions.rb,
lib/spreedly/subscriptions/mock.rb,
lib/spreedly/subscriptions/test_hacks.rb

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

attributes, attributes=, #method_missing

Constructor Details

#initialize(params = {}) ⇒ Subscriber

Returns a new instance of Subscriber.



93
94
95
96
97
98
99
# File 'lib/spreedly/subscriptions/mock.rb', line 93

def initialize(params={})
  super
  if !id || id == ''
    raise "Could not create subscriber: Customer ID can't be blank."
  end
  @invoices ||= []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spreedly::Subscriptions::Resource

Class Method Details

.allObject

Returns all the subscribers in your site.



139
140
141
# File 'lib/spreedly/subscriptions.rb', line 139

def self.all
  Spreedly::Subscriptions.get('/subscribers.xml')['subscribers'].collect{|data| new(data)}
end

.create!(id, *args) ⇒ Object

:nodoc: all



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spreedly/subscriptions.rb', line 110

def self.create!(id, *args)
  optional_attrs = args.last.is_a?(::Hash) ? args.pop : {}
  email, screen_name = args
  subscriber = {:customer_id => id, :email => email, :screen_name => screen_name}.merge(optional_attrs)
  result = Spreedly::Subscriptions.post('/subscribers.xml', :body => Spreedly.to_xml_params(:subscriber => subscriber))
  case result.code.to_s
  when /2../
    new(result['subscriber'])
  when '403'
    raise "Could not create subscriber: already exists."
  when '422'
    errors = [*result['errors']].collect{|e| e.last}
    raise "Could not create subscriber: #{errors.join(', ')}"
  else
    raise "Could not create subscriber: result code #{result.code}."
  end
end

.delete!(id) ⇒ Object

This will DELETE individual subscribers from the site. Pass in the customer_id.

Only works for test sites (enforced on the Spreedly side).



97
98
99
# File 'lib/spreedly/subscriptions.rb', line 97

def self.delete!(id)
  Spreedly::Subscriptions.delete("/subscribers/#{id}.xml")
end

.find(id) ⇒ Object

Looks a subscriber up by id.



129
130
131
132
133
134
135
136
# File 'lib/spreedly/subscriptions.rb', line 129

def self.find(id)
  xml = Spreedly::Subscriptions.get("/subscribers/#{id}.xml")
  if [200, 404].include?(xml.code)
    (xml.nil? || xml.empty? ? nil : new(xml['subscriber']))
  else
    raise "Could not find subscriber: result code #{xml.code}, body '#{xml.body}'"
  end
end

.subscribersObject



85
86
87
# File 'lib/spreedly/subscriptions/mock.rb', line 85

def self.subscribers
  @subscribers ||= {}
end

.wipe!Object

:nodoc: all



90
91
92
# File 'lib/spreedly/subscriptions.rb', line 90

def self.wipe!
  Spreedly::Subscriptions.delete('/subscribers.xml')
end

Instance Method Details

#activate_free_trial(plan_id) ⇒ Object

Activates a free trial on the subscriber. Requires plan_id of the free trial plan



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/spreedly/subscriptions.rb', line 177

def activate_free_trial(plan_id)
  result = Spreedly::Subscriptions.post("/subscribers/#{id}/subscribe_to_free_trial.xml", :body =>
                         Spreedly::Subscriptions.to_xml_params(:subscription_plan => {:id => plan_id}))
  case result.code.to_s
  when /2../
  when '404'
    raise "Could not active free trial for subscriber: subscriber or subscription plan no longer exists."
  when '422'
    raise "Could not activate free trial for subscriber: validation failed. missing subscription plan id"
  when '403'
    raise "Could not activate free trial for subscriber: subscription plan either 1) isn't a free trial, 2) the subscriber is not eligible for a free trial, or 3) the subscription plan is not enabled."
  else
    raise "Could not activate free trial for subscriber: result code #{result.code}."
  end
end

#add_fee(args) ⇒ Object

Add a Fee to a Subscriber usage: @subscriber.add_fee(:amount => amount, :group => group_name, :description => description, :name => name)



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/spreedly/subscriptions.rb', line 236

def add_fee(args)
  result = Spreedly::Subscriptions.post("/subscribers/#{id}/fees.xml", :body => Spreedly::Subscriptions.to_xml_params(:fee => args))

  case result.code.to_s
  when /2../
  when '404'
    raise "Not Found"
  when '422'
    raise "Unprocessable Entity - #{result.body}"
  else
    raise "Could not add fee to subscriber: result code #{result.code}."
  end
end

#allow_free_trialObject

Allow Another Free Trial usage: @subscriber.allow_free_trial



224
225
226
227
228
229
230
231
232
# File 'lib/spreedly/subscriptions.rb', line 224

def allow_free_trial
  result = Spreedly::Subscriptions.post("/subscribers/#{id}/allow_free_trial.xml")

  case result.code.to_s
  when /2../
  else
    raise "Could not allow subscriber to another trial: result code #{result.code}."
  end
end

#comp(quantity, units, feature_level = nil) ⇒ Object

Allows you to give a complimentary subscription (if the subscriber is inactive) or a complimentary time extension (if the subscriber is active). Automatically figures out which to do.

Note: units must be one of “days” or “months” (Spreedly enforced).



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/spreedly/subscriptions.rb', line 156

def comp(quantity, units, feature_level=nil)
  params = {:duration_quantity => quantity, :duration_units => units}
  params[:feature_level] = feature_level if feature_level
  raise "Feature level is required to comp an inactive subscriber" if !active? and !feature_level
  endpoint = (active? ? "complimentary_time_extensions" : "complimentary_subscriptions")
  result = Spreedly::Subscriptions.post("/subscribers/#{id}/#{endpoint}.xml", :body => Spreedly.to_xml_params(endpoint[0..-2] => params))
  case result.code.to_s
  when /2../
  when '404'
    raise "Could not comp subscriber: no longer exists."
  when '422'
    raise "Could not comp subscriber: validation failed (#{result.body})."
  when '403'
    raise "Could not comp subscriber: invalid comp type (#{endpoint})."
  else
    raise "Could not comp subscriber: result code #{result.code}."
  end
end

#idObject

Spreedly calls your id for the user the “customer id”. This gives you a handy alias so you can just call it “id”.



145
146
147
# File 'lib/spreedly/subscriptions.rb', line 145

def id
  customer_id
end

#invoicesObject

Get the invoices for the subscriber



251
252
253
# File 'lib/spreedly/subscriptions.rb', line 251

def invoices
  @invoices ||= @data["invoices"].collect{|i| Invoice.new(i)}
end

#last_successful_invoiceObject

Get the last successful invoice



256
257
258
259
260
# File 'lib/spreedly/subscriptions.rb', line 256

def last_successful_invoice
  invoices.detect do |invoice|
    invoice.closed?
  end
end

#stop_auto_renewObject

Stop the auto renew of the subscriber such that their recurring subscription will no longer be renewed. usage: @subscriber.stop_auto_renew



195
196
197
198
199
200
201
202
203
204
# File 'lib/spreedly/subscriptions.rb', line 195

def stop_auto_renew
  result = Spreedly::Subscriptions.post("/subscribers/#{id}/stop_auto_renew.xml")
  case result.code.to_s
  when /2../
  when '404'
    raise "Could not stop auto renew for subscriber: subscriber does not exist."
  else
    raise "Could not stop auto renew for subscriber: result code #{result.code}."
  end
end

#subscribe(plan_id, card_number = "4222222222222") ⇒ Object

This method is strictly for use when testing, and will probably only work against a test Spreedly site anyhow.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spreedly/subscriptions/test_hacks.rb', line 8

def subscribe(plan_id, card_number="4222222222222")
  plan = SubscriptionPlan.find(plan_id)
  @invoices.unshift(Invoice.new(
    amount: (@invoices.select{|invoice| invoice.closed?}.size > 0 ? 0 : plan.amount),
    closed: false
  ))

  return unless card_number == "4222222222222"

  @invoices.first.attributes[:closed] = true
  @attributes[:recurring] = true
  comp(plan.duration_quantity, plan.duration_units, plan.feature_level)
end

#update(args) ⇒ Object

Update a Subscriber usage: @subscriber.update(:email => email, :screen_name => screen_name)



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/spreedly/subscriptions.rb', line 208

def update(args)
  result = Spreedly::Subscriptions.put("/subscribers/#{id}.xml", :body => Spreedly.to_xml_params(:subscriber => args))

  case result.code.to_s
  when /2../
  when '403'
    raise "Could not update subscriber: new-customer-id is already in use."
  when '404'
    raise "Could not update subscriber: subscriber not found"
  else
    raise "Could not update subscriber: result code #{result.code}."
  end
end