Class: RSpreedly::Subscriber

Inherits:
Base
  • Object
show all
Defined in:
lib/rspreedly/subscriber.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_request, #api_request, #attributes=, do_request, #initialize

Constructor Details

This class inherits a constructor from RSpreedly::Base

Instance Attribute Details

#activeObject

Returns the value of attribute active.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def active
  @active
end

#active_untilObject

Returns the value of attribute active_until.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def active_until
  @active_until
end

#billing_first_nameObject

Returns the value of attribute billing_first_name.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def billing_first_name
  @billing_first_name
end

#billing_last_nameObject

Returns the value of attribute billing_last_name.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def billing_last_name
  @billing_last_name
end

#card_expires_before_next_auto_renewObject

Returns the value of attribute card_expires_before_next_auto_renew.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def card_expires_before_next_auto_renew
  @card_expires_before_next_auto_renew
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def created_at
  @created_at
end

#customer_idObject

Returns the value of attribute customer_id.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def customer_id
  @customer_id
end

#eligible_for_free_trialObject

Returns the value of attribute eligible_for_free_trial.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def eligible_for_free_trial
  @eligible_for_free_trial
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def email
  @email
end

#feature_levelObject

Returns the value of attribute feature_level.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def feature_level
  @feature_level
end

#grace_untilObject

Returns the value of attribute grace_until.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def grace_until
  @grace_until
end

#in_grace_periodObject

Returns the value of attribute in_grace_period.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def in_grace_period
  @in_grace_period
end

#invoicesObject

Returns the value of attribute invoices.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def invoices
  @invoices
end

#lifetime_subscriptionObject

Returns the value of attribute lifetime_subscription.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def lifetime_subscription
  @lifetime_subscription
end

#new_customer_idObject

Returns the value of attribute new_customer_id.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def new_customer_id
  @new_customer_id
end

#on_trialObject

Returns the value of attribute on_trial.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def on_trial
  @on_trial
end

#payment_methodObject

Returns the value of attribute payment_method.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def payment_method
  @payment_method
end

#ready_to_renewObject

Returns the value of attribute ready_to_renew.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def ready_to_renew
  @ready_to_renew
end

#ready_to_renew_sinceObject

Returns the value of attribute ready_to_renew_since.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def ready_to_renew_since
  @ready_to_renew_since
end

#recurringObject

Returns the value of attribute recurring.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def recurring
  @recurring
end

#screen_nameObject

Returns the value of attribute screen_name.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def screen_name
  @screen_name
end

#store_creditObject

Returns the value of attribute store_credit.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def store_credit
  @store_credit
end

#store_credit_currency_codeObject

Returns the value of attribute store_credit_currency_code.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def store_credit_currency_code
  @store_credit_currency_code
end

#subscription_plan_nameObject

Returns the value of attribute subscription_plan_name.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def subscription_plan_name
  @subscription_plan_name
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def token
  @token
end

#updated_atObject

Returns the value of attribute updated_at.



5
6
7
# File 'lib/rspreedly/subscriber.rb', line 5

def updated_at
  @updated_at
end

Class Method Details

.allObject

Get a list of all subscribers (more) GET /api/v4/[short site name]/subscribers.xml



49
50
51
52
53
# File 'lib/rspreedly/subscriber.rb', line 49

def all
  response = api_request(:get, "/subscribers.xml")
  return [] unless response.has_key?("subscribers")
  response["subscribers"].collect{|data| Subscriber.new(data)}
end

.delete_allObject Also known as: destroy_all

Clear all subscribers from a test site (more) DELETE /api/v4/[short site name]/subscribers.xml



57
58
59
# File 'lib/rspreedly/subscriber.rb', line 57

def delete_all
  !! api_request(:delete, "/subscribers.xml")
end

.find(id) ⇒ Object

Get a subscriber’s details GET /api/v4/[short site name]/subscribers/[subscriber id].xml



36
37
38
39
40
41
42
43
44
45
# File 'lib/rspreedly/subscriber.rb', line 36

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

  begin
    data = api_request(:get, "/subscribers/#{id}.xml")
    Subscriber.new(data["subscriber"])
  rescue RSpreedly::Error::NotFound
    nil
  end
end

Instance Method Details

#allow_free_trialObject

Programatically Allow Another Free Trial (more) POST /api/v4/[short site name]/subscribers/[subscriber id]/allow_free_trial.xml



173
174
175
176
177
# File 'lib/rspreedly/subscriber.rb', line 173

def allow_free_trial
  result = api_request(:post, "/subscribers/#{self.customer_id}/allow_free_trial.xml")
  self.attributes = result["subscriber"]
  true
end

#comp_subscription(subscription) ⇒ Object

Give a subscriber a complimentary subscription (more) POST /api/v4/[short site name]/subscribers/[subscriber id]/complimentary_subscriptions.xml



134
135
136
137
138
# File 'lib/rspreedly/subscriber.rb', line 134

def comp_subscription(subscription)
  result = api_request(:post, "/subscribers/#{self.customer_id}/complimentary_subscriptions.xml", :body => subscription.to_xml)
  self.attributes = result["subscriber"]
  true
end

#comp_time_extension(extension) ⇒ Object

Give a subscriber a complimentary time extension (more) POST /api/v4/[short site name]/subscribers/[subscriber id]/complimentary_time_extension.xml



142
143
144
145
146
# File 'lib/rspreedly/subscriber.rb', line 142

def comp_time_extension(extension)
  result = api_request(:post, "/subscribers/#{self.customer_id}/complimentary_time_extensions.xml", :body => extension.to_xml)
  self.attributes = result["subscriber"]
  true
end

#createObject



95
96
97
98
99
100
101
102
103
# File 'lib/rspreedly/subscriber.rb', line 95

def create
  begin
    create!
  rescue RSpreedly::Error::Base
    # gulp those errors down
    # TODO - set self.errors or something?
    nil
  end
end

#create!Object

Create a subscriber (more) POST /api/v4/[short site name]/subscribers.xml



89
90
91
92
93
# File 'lib/rspreedly/subscriber.rb', line 89

def create!
  result = api_request(:post, "/subscribers.xml", :body => self.to_xml)
  self.attributes = result["subscriber"]
  true
end

#credit(amount) ⇒ Object

Give a subscriber a credit (or reduce credit by supplying a negative value (more) POST /api/v4[short site name]/subscribers/[subscriber id]/credits.xml



150
151
152
153
154
155
# File 'lib/rspreedly/subscriber.rb', line 150

def credit(amount)
  credit = Credit.new(:amount => amount)
  result = api_request(:post, "/subscribers/#{self.customer_id}/credits.xml", :body => credit.to_xml)
  self.store_credit = (self.store_credit || 0) + amount
  true
end

#destroyObject Also known as: delete

Delete one subscriber from a test site (more) DELETE /api/v4/[short site name]/subscribers/[subscriber id].xml



123
124
125
126
127
128
129
# File 'lib/rspreedly/subscriber.rb', line 123

def destroy
  begin
    !! api_request(:delete, "/subscribers/#{self.customer_id}.xml")
  rescue RSpreedly::Error::NotFound
    nil
  end
end

#grant_lifetime_subscription(feature_level) ⇒ Object



179
180
181
182
183
184
# File 'lib/rspreedly/subscriber.rb', line 179

def grant_lifetime_subscription(feature_level)
  subscription = LifetimeComplimentarySubscription.new(:feature_level => feature_level)
  result = api_request(:post, "/subscribers/#{self.customer_id}/lifetime_complimentary_subscriptions.xml", :body => subscription.to_xml)
  self.attributes = result["subscriber"]
  true
end

#new_record?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rspreedly/subscriber.rb', line 75

def new_record?
  !self.token
end

#saveObject



79
80
81
# File 'lib/rspreedly/subscriber.rb', line 79

def save
  self.new_record? ? self.create : self.update
end

#save!Object



83
84
85
# File 'lib/rspreedly/subscriber.rb', line 83

def save!
  self.new_record? ? self.create! : self.update!
end

#stop_auto_renewObject

Programatically Stopping Auto Renew of a Subscriber (more) POST /api/v4/[short site name]/subscribers/[subscriber id]/stop_auto_renew.xml



159
160
161
# File 'lib/rspreedly/subscriber.rb', line 159

def stop_auto_renew
  !! api_request(:post, "/subscribers/#{self.customer_id}/stop_auto_renew.xml")
end


186
187
188
189
# File 'lib/rspreedly/subscriber.rb', line 186

def subscribe_link(subscription_plan_id, screen_name, return_url=nil)
  params = return_url.nil? ? "" : "?return_url=" + return_url
  "https://spreedly.com/#{RSpreedly::Config.site_name}/subscribers/#{self.customer_id}/subscribe/#{subscription_plan_id}/#{screen_name}#{params}"
end

#subscribe_to_free_trial(plan) ⇒ Object

Programatically Subscribe a Subscriber to a Free Trial Plan (more) POST /api/v4/[short site name]/subscribers/[subscriber id]/subscribe_to_free_trial.xml



165
166
167
168
169
# File 'lib/rspreedly/subscriber.rb', line 165

def subscribe_to_free_trial(plan)
  result = api_request(:post, "/subscribers/#{self.customer_id}/subscribe_to_free_trial.xml", :body => plan.to_xml)
  self.attributes = result["subscriber"]
  true
end


191
192
193
194
# File 'lib/rspreedly/subscriber.rb', line 191

def subscription_link(return_url=nil)
  params = return_url.nil? ? "" : "?return_url=" + return_url
  "https://spreedly.com/#{RSpreedly::Config.site_name}/subscriber_accounts/#{self.token}#{params}"
end

#to_xml(opts = {}) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rspreedly/subscriber.rb', line 196

def to_xml(opts={})

  # the api doesn't let us send these things
  # so let's strip them out of the XML
  exclude = [
    :active,       :active_until,               :card_expires_before_next_auto_renew,
    :created_at,   :eligible_for_free_trial,    :feature_level,
    :grace_until,  :in_grace_period,            :lifetime_subscription,
    :on_trial,     :ready_to_renew,             :recurring,
    :store_credit, :store_credit_currency_code, :subscription_plan_name,
    :token,        :updated_at,                 :ready_to_renew_since,
    :invoices
  ]

  opts[:exclude] ||= []
  opts[:exclude] |= exclude

  super(opts)
end

#updateObject



111
112
113
114
115
116
117
118
119
# File 'lib/rspreedly/subscriber.rb', line 111

def update
  begin
    update!
  rescue RSpreedly::Error::Base
    # gulp those errors down
    # TODO - set self.errors or something?
    nil
  end
end

#update!Object

Update a Subscriber (more) PUT /api/v4/[short site name]/subscribers/[subscriber id].xml



107
108
109
# File 'lib/rspreedly/subscriber.rb', line 107

def update!
  !! api_request(:put, "/subscribers/#{self.customer_id}.xml", :body => self.to_xml(:exclude => [:customer_id]))
end