Module: Spreedly::Subscriptions

Includes:
HTTParty
Defined in:
lib/spreedly/subscriptions.rb,
lib/spreedly/subscriptions/mock.rb,
lib/spreedly/subscriptions/common.rb,
lib/spreedly/subscriptions/version.rb,
lib/spreedly/subscriptions/test_hacks.rb

Defined Under Namespace

Classes: Invoice, Resource, Subscriber, SubscriptionPlan

Constant Summary collapse

REAL =

:nodoc:

"real"
MOCK =
"mock"
VERSION =
"2.0.0"

Class Method Summary collapse

Class Method Details

.configure(name, token) ⇒ Object

Call this before you start using the API to set things up.



42
43
44
45
46
# File 'lib/spreedly/subscriptions.rb', line 42

def self.configure(site_name, token)
  base_uri "https://spreedly.com/api/v4/#{site_name}"
  basic_auth token, 'X'
  @site_name = site_name
end

.edit_subscriber_url(token, return_url = nil) ⇒ Object

Generates an edit subscriber for the given subscriber token. The token is returned with the subscriber info (i.e. by Subscriber.find).



38
39
40
41
42
43
44
45
# File 'lib/spreedly/subscriptions/common.rb', line 38

def self.edit_subscriber_url(token, return_url = nil)
  "https://spreedly.com/#{site_name}/subscriber_accounts/#{token}" +
  if return_url
    "?return_url=#{URI.escape(return_url)}"
  else
    ''
  end
end

.site_nameObject

:nodoc:



48
49
50
# File 'lib/spreedly/subscriptions.rb', line 48

def self.site_name # :nodoc:
  @site_name
end

.subscribe_url(id, plan, options = {}) ⇒ Object

Generates a subscribe url for the given user id and plan. Options:

:screen_name => a screen name for the user (shows up in the admin UI)
:email => pre-populate the email field
:first_name => pre-populate the first name field
:last_name => pre-populate the last name field


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spreedly/subscriptions/common.rb', line 20

def self.subscribe_url(id, plan, options={})
  %w(screen_name email first_name last_name return_url).each do |option|
    options[option.to_sym] &&= URI.escape(options[option.to_sym])
  end

  screen_name = options.delete(:screen_name)
  params = %w(email first_name last_name return_url).select{|e| options[e.to_sym]}.collect{|e| "#{e}=#{options[e.to_sym]}"}.join('&')

  url = "https://spreedly.com/#{site_name}/subscribers/#{id}/subscribe/#{plan}"
  url << "/#{screen_name}" if screen_name
  url << '?' << params unless params == ''

  url
end

.to_xml_params(hash) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spreedly/subscriptions.rb', line 52

def self.to_xml_params(hash) # :nodoc:
  hash.collect do |key, value|
  tag = key.to_s.tr('_', '-')
  result = "<#{tag}>"
  if value.is_a?(Hash)
    result << to_xml_params(value)
  else
    result << value.to_s
  end
  result << "</#{tag}>"
  result
end.join('')
end