Class: Spreedly::Subscriber
- Inherits:
-
Resource
- Object
- Resource
- Spreedly::Subscriber
- Defined in:
- lib/spreedly.rb,
lib/spreedly/mock.rb,
lib/spreedly/test_hacks.rb
Instance Attribute Summary
Attributes inherited from Resource
Class Method Summary (collapse)
-
+ (Object) all
Returns all the subscribers in your site.
-
+ (Object) create!(id, *args)
:nodoc: all.
-
+ (Object) delete!(id)
This will DELETE individual subscribers from the site.
-
+ (Object) find(id)
Looks a subscriber up by id.
- + (Object) subscribers
-
+ (Object) wipe!
:nodoc: all.
Instance Method Summary (collapse)
-
- (Object) activate_free_trial(plan_id)
Activates a free trial on the subscriber.
-
- (Object) add_fee(args)
Add a Fee to a Subscriber usage: @subscriber.add_fee(:amount => amount, :group => group_name, :description => description, :name => name).
-
- (Object) allow_free_trial
Allow Another Free Trial usage: @subscriber.allow_free_trial.
-
- (Object) comp(quantity, units, feature_level = nil)
Allows you to give a complimentary subscription (if the subscriber is inactive) or a complimentary time extension (if the subscriber is active).
-
- (Object) id
Spreedly calls your id for the user the “customer id”.
-
- (Subscriber) initialize(params = {})
constructor
A new instance of Subscriber.
-
- (Object) invoices
Get the invoices for the subscriber.
-
- (Object) last_successful_invoice
Get the last successful invoice.
-
- (Object) stop_auto_renew
Stop the auto renew of the subscriber such that their recurring subscription will no longer be renewed.
-
- (Object) subscribe(plan_id, card_number = "4222222222222")
This method is strictly for use when testing, and will probably only work against a test Spreedly site anyhow.
-
- (Object) update(args)
Update a Subscriber usage: @subscriber.update(:email => email, :screen_name => screen_name).
Methods inherited from Resource
attributes, attributes=, #method_missing
Constructor Details
- (Subscriber) initialize(params = {})
A new instance of Subscriber
92 93 94 95 96 97 98 |
# File 'lib/spreedly/mock.rb', line 92 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::Resource
Class Method Details
+ (Object) all
Returns all the subscribers in your site.
138 139 140 |
# File 'lib/spreedly.rb', line 138 def self.all Spreedly.get('/subscribers.xml')['subscribers'].collect{|data| new(data)} end |
+ (Object) create!(id, *args)
:nodoc: all
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/spreedly.rb', line 109 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 |
+ (Object) delete!(id)
This will DELETE individual subscribers from the site. Pass in the customer_id.
Only works for test sites (enforced on the Spreedly side).
96 97 98 |
# File 'lib/spreedly.rb', line 96 def self.delete!(id) Spreedly.delete("/subscribers/#{id}.xml") end |
+ (Object) find(id)
Looks a subscriber up by id.
128 129 130 131 132 133 134 135 |
# File 'lib/spreedly.rb', line 128 def self.find(id) xml = Spreedly.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 |
+ (Object) subscribers
84 85 86 |
# File 'lib/spreedly/mock.rb', line 84 def self.subscribers @subscribers ||= {} end |
+ (Object) wipe!
:nodoc: all
89 90 91 |
# File 'lib/spreedly.rb', line 89 def self.wipe! Spreedly.delete('/subscribers.xml') end |
Instance Method Details
- (Object) activate_free_trial(plan_id)
Activates a free trial on the subscriber. Requires plan_id of the free trial plan
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/spreedly.rb', line 176 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 |
- (Object) add_fee(args)
Add a Fee to a Subscriber usage: @subscriber.add_fee(:amount => amount, :group => group_name, :description => description, :name => name)
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/spreedly.rb', line 235 def add_fee(args) result = Spreedly.post("/subscribers/#{id}/fees.xml", :body => Spreedly.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 |
- (Object) allow_free_trial
Allow Another Free Trial usage: @subscriber.allow_free_trial
223 224 225 226 227 228 229 230 231 |
# File 'lib/spreedly.rb', line 223 def allow_free_trial result = Spreedly.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 |
- (Object) comp(quantity, units, feature_level = nil)
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).
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/spreedly.rb', line 155 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.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 |
- (Object) id
Spreedly calls your id for the user the “customer id”. This gives you a handy alias so you can just call it “id”.
144 145 146 |
# File 'lib/spreedly.rb', line 144 def id customer_id end |
- (Object) invoices
Get the invoices for the subscriber
250 251 252 |
# File 'lib/spreedly.rb', line 250 def invoices @invoices ||= @data["invoices"].collect{|i| Invoice.new(i)} end |
- (Object) last_successful_invoice
Get the last successful invoice
255 256 257 258 259 |
# File 'lib/spreedly.rb', line 255 def last_successful_invoice invoices.detect do |invoice| invoice.closed? end end |
- (Object) stop_auto_renew
Stop the auto renew of the subscriber such that their recurring subscription will no longer be renewed. usage: @subscriber.stop_auto_renew
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/spreedly.rb', line 194 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 |
- (Object) subscribe(plan_id, card_number = "4222222222222")
This method is strictly for use when testing, and will probably only work against a test Spreedly site anyhow.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/spreedly/test_hacks.rb', line 7 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 |
- (Object) update(args)
Update a Subscriber usage: @subscriber.update(:email => email, :screen_name => screen_name)
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/spreedly.rb', line 207 def update(args) result = Spreedly.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 |