Class: Spreedly::Subscriber

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

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.



90
91
92
93
94
95
# File 'lib/spreedly/mock.rb', line 90

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

Dynamic Method Handling

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

Class Method Details

.allObject

Returns all the subscribers in your site.



135
136
137
# File 'lib/spreedly.rb', line 135

def self.all
  Spreedly.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.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.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.rb', line 97

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

.find(id) ⇒ Object

Looks a subscriber up by id.



129
130
131
132
# File 'lib/spreedly.rb', line 129

def self.find(id)
  xml = Spreedly.get("/subscribers/#{id}.xml")
  (xml.nil? || xml.empty? ? nil : new(xml['subscriber']))
end

.subscribersObject



82
83
84
# File 'lib/spreedly/mock.rb', line 82

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

.wipe!Object

:nodoc: all



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

def self.wipe!
  Spreedly.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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/spreedly.rb', line 172

def activate_free_trial(plan_id)
  result = Spreedly.post("/subscribers/#{id}/subscribe_to_free_trial.xml", :body => 
    Spreedly.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

#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).



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/spreedly.rb', line 152

def comp(quantity, units, feature_level=nil)
  params = {:duration_quantity => quantity, :duration_units => units}
  params[:feature_level] = feature_level if feature_level
  endpoint = (active? ? "complimentary_time_extensions" : "complimentary_subscriptions")
  result = Spreedly.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."
  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”.



141
142
143
# File 'lib/spreedly.rb', line 141

def id
  customer_id
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



190
191
192
193
194
195
196
197
198
199
# File 'lib/spreedly.rb', line 190

def stop_auto_renew
  result = Spreedly.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) ⇒ Object

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



7
8
9
10
11
# File 'lib/spreedly/test_hacks.rb', line 7

def subscribe(plan_id)
  @attributes[:recurring] = true
  plan = SubscriptionPlan.find(plan_id)
  comp(plan.duration_quantity, plan.duration_units, plan.feature_level)
end