class Spreedly::Subscriptions::Subscriber

Public Class Methods

all() click to toggle source

Returns all the subscribers in your site.

# File lib/spreedly/subscriptions.rb, line 139
def self.all
  Spreedly::Subscriptions.get('/subscribers.xml')['subscribers'].collect{|data| new(data)}
end
create!(id, *args) click to toggle source

Creates a new subscriber on Spreedly. The subscriber will NOT be active - they have to pay or you have to comp them for that to happen.

Usage:

Spreedly::Subscriptions.Subscriber.create!(id, email)
Spreedly::Subscriptions.Subscriber.create!(id, email, screen_name)
Spreedly::Subscriptions.Subscriber.create!(id, :email => email, :screen_name => screen_name)
Spreedly::Subscriptions.Subscriber.create!(id, email, screen_name, :billing_first_name => first_name)
# 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::Subscriptions.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) click to toggle source

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

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

# File lib/spreedly/subscriptions.rb, line 97
def self.delete!(id)
  Spreedly::Subscriptions.delete("/subscribers/#{id}.xml")
end
find(id) click to toggle source

Looks a subscriber up by id.

# 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
new(params={}) click to toggle source
Calls superclass method Spreedly::Subscriptions::Resource::new
# 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
subscribers() click to toggle source
# File lib/spreedly/subscriptions/mock.rb, line 85
def self.subscribers
  @subscribers ||= {}
end
wipe!() click to toggle source

This will DELETE all the subscribers from the site.

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

# File lib/spreedly/subscriptions.rb, line 90
def self.wipe!
  Spreedly::Subscriptions.delete('/subscribers.xml')
end

Public Instance Methods

activate_free_trial(plan_id) click to toggle source

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

# 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) click to toggle source

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

# 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_trial() click to toggle source

Allow Another Free Trial usage: @subscriber.allow_free_trial

# 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) click to toggle source

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

# 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::Subscriptions.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
id() click to toggle source

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

# File lib/spreedly/subscriptions.rb, line 145
def id
  customer_id
end
invoices() click to toggle source

Get the invoices for the subscriber

# File lib/spreedly/subscriptions.rb, line 251
def invoices
  @invoices ||= @data["invoices"].collect{|i| Invoice.new(i)}
end
last_successful_invoice() click to toggle source

Get the last successful invoice

# File lib/spreedly/subscriptions.rb, line 256
def last_successful_invoice
  invoices.detect do |invoice|
    invoice.closed?
  end
end
stop_auto_renew() click to toggle source

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

# 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") click to toggle source
# File lib/spreedly/subscriptions/mock.rb, line 145
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) click to toggle source

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

# File lib/spreedly/subscriptions.rb, line 208
def update(args)
  result = Spreedly::Subscriptions.put("/subscribers/#{id}.xml", :body => Spreedly::Subscriptions.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