Module: Spreedly

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

Overview

Provides a convenient wrapper around the spreedly.com API. Instead of mucking around with http you can just Spreedly.configure and Spreedly::Subscriber.find. Much of the functionality is hung off of the Spreedly::Subscriber class, and there's also a Spreedly::SubscriptionPlan class.

One of the goals of this wrapper is to keep your tests fast while also keeping your app working. It does this by providing a drop-in replacement for the real Spreedly functionality that skips the network and gives you a simple (some might call it stupid) implementation that will work for 90% of your tests. At least we hope so.

Help us make the mock work better by telling us when it doesn't work so we can improve it. Thanks!

Example mock usage:

if ENV["SPREEDLY"] == "REAL"
  require 'spreedly'
else
  require 'spreedly/mock'
end

Defined Under Namespace

Classes: Invoice, Resource, Subscriber, SubscriptionPlan

Constant Summary

REAL =

:nodoc:

"real"
MOCK =
"mock"
VERSION =
"1.4.0"

Class Method Summary (collapse)

Class Method Details

+ (Object) configure(name, token)

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



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

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

+ (Object) edit_subscriber_url(token, return_url = nil)

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



36
37
38
39
40
41
42
43
# File 'lib/spreedly/common.rb', line 36

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

+ (Object) site_name

:nodoc:



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

def self.site_name # :nodoc:
  @site_name
end

+ (Object) subscribe_url(id, plan, options = {})

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


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

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

+ (Object) to_xml_params(hash)

:nodoc:



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

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