erbmicha-spreedly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Erb
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = spreedly
2
+
3
+ This gem is a wrapper for the Spreedly Web Services API.
4
+
5
+ == Installation
6
+
7
+ gem install erbmicha-spreedly
8
+
9
+ == Usage
10
+
11
+ spreedly = Spreedly.new("site_name", "site_token")
12
+ spreedly.get_subscribers
13
+
14
+ == Note on Patches/Pull Requests
15
+
16
+ * Fork the project.
17
+ * Make your feature addition or bug fix.
18
+ * Add tests for it. This is important so I don't break it in a
19
+ future version unintentionally.
20
+ * Commit, do not mess with rakefile, version, or history.
21
+ (if you want to have your own version, that is fine but
22
+ bump version in a commit by itself I can ignore when I pull)
23
+ * Send me a pull request. Bonus points for topic branches.
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2009 Michael Erb. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "spreedly"
8
+ gem.summary = %Q{This gem is a wrapper for the Spreedly Web Services API.}
9
+ gem.description = %Q{TODO: longer description of your gem}
10
+ gem.email = "michael@erbmicha.com"
11
+ gem.homepage = "http://github.com/erbmicha/spreedly"
12
+ gem.authors = ["Michael Erb"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION')
39
+ version = File.read('VERSION')
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "spreedly #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/lib/spreedly.rb ADDED
@@ -0,0 +1,122 @@
1
+ require 'rubygems'
2
+ require 'httparty'
3
+
4
+ require 'spreedly/error'
5
+ require 'spreedly/invoice'
6
+ require 'spreedly/request'
7
+ require 'spreedly/subscriber'
8
+ require 'spreedly/subscription_plan'
9
+
10
+ class Spreedly
11
+
12
+ # include HTTParty
13
+ # base_uri "https://spreedly.com/api/v4"
14
+ # format :xml
15
+ # class << self; end
16
+
17
+ def initialize(site_name, api_token)
18
+ @site_name = site_name
19
+ @auth = {:username => api_token, :password => "X"}
20
+ end
21
+
22
+ def get_subscriber(subscriber_id, options={})
23
+ options.merge!({:basic_auth => @auth})
24
+ response = Spreedly::Request.new(:get, "/#{@site_name}/subscribers/#{subscriber_id}.xml", options)
25
+ Spreedly::Subscriber.new(response)
26
+ end
27
+
28
+ def get_subscribers(options={})
29
+ options.merge!({:basic_auth => @auth})
30
+ response = Spreedly::Request.new(:get, "/#{@site_name}/subscribers.xml", options)
31
+ response.map! { |subscriber| Spreedly::Subscriber.new(subscriber) }
32
+ end
33
+
34
+ def create_subscriber(subscriber={}, options={})
35
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(subscriber)})
36
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers.xml", options)
37
+ Spreedly::Subscriber.new(response)
38
+ end
39
+
40
+ def update_subscriber(subscriber={}, options={})
41
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(subscriber)})
42
+ response = Spreedly::Request.new(:put, "/#{@site_name}/subscribers/#{subscriber[:id]}.xml", options)
43
+ Spreedly::Subscriber.new(response)
44
+ end
45
+
46
+ def add_complimentary_subscription(subscriber_id, complimentary_subscription={}, options={})
47
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(complimentary_subscription)})
48
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers/#{subscriber_id}/complimentary_subscriptions.xml", options)
49
+ Spreedly::Subscriber.new(response)
50
+ end
51
+
52
+ def add_complimentary_time_extension(subscriber_id, complimentary_time_extension={}, options={})
53
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(complimentary_time_extension)})
54
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers/#{subscriber_id}/complimentary_time_extension.xml", options)
55
+ Spreedly::Subscriber.new(response)
56
+ end
57
+
58
+ def stop_auto_renew(subscriber_id, options={})
59
+ options.merge!({:basic_auth => @auth})
60
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers/#{subscriber_id}/stop_auto_renew.xml", options)
61
+ Spreedly::Subscriber.new(response)
62
+ end
63
+
64
+ def subscribe_to_free_trial(subscriber_id, subscription_plan={}, options={})
65
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(subscription_plan)})
66
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers/#{subscriber_id}/subscribe_to_free_trial.xml", options)
67
+ Spreedly::Subscriber.new(response)
68
+ end
69
+
70
+ def allow_another_free_trial(subscriber_id, options={})
71
+ options.merge!({:basic_auth => @auth})
72
+ response = Spreedly::Request.new(:post, "/#{@site_name}/subscribers/#{subscriber_id}/allow_free_trial.xml", options)
73
+ Spreedly::Subscriber.new(response)
74
+ end
75
+
76
+ def get_subscription_plans(options={})
77
+ options.merge!({:basic_auth => @auth})
78
+ response = Spreedly::Request.new(:get, "/#{@site_name}/subscription_plans.xml", options)
79
+ # response.map! { |subscription_plan| Spreedly::SubscriptionPlan.new(response) }
80
+ end
81
+
82
+ def create_invoice(invoice={}, options={})
83
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(invoice)})
84
+ response = Spreedly::Request.new(:post, "/#{@site_name}/invoices.xml", options)
85
+ Spreedly::Invoice.new(response)
86
+ end
87
+
88
+ def pay_invoice(invoice_token, payment={}, options={})
89
+ options.merge!({:basic_auth => @auth, :body => to_xml_params(payment)})
90
+ response = Spreedly::Request.new(:get, "/#{@site_name}/invoices/#{invoice_token}/pay.xml", options)
91
+ Spreedly::Invoice.new(response)
92
+ end
93
+
94
+ def delete_test_subscriber(subscriber_id, options={})
95
+ options.merge!({:basic_auth => @auth})
96
+ response = Spreedly::Request.new(:delete, "/#{@site_name}/subscribers/#{subscriber_id}.xml", options)
97
+ end
98
+
99
+ def delete_test_subscribers(options={})
100
+ options.merge!({:basic_auth => @auth})
101
+ response = Spreedly::Request.new(:delete, "/#{@site_name}/subscribers.xml", options)
102
+ end
103
+
104
+ private
105
+
106
+ def to_xml_params(hash) # :nodoc:
107
+ hash.collect do |key, value|
108
+ tag = key.to_s.tr('_', '-')
109
+ result = "<#{tag}>"
110
+ if value.is_a?(Hash)
111
+ result << to_xml_params(value)
112
+ else
113
+ result << value.to_s
114
+ end
115
+ result << "</#{tag}>"
116
+ result
117
+ end.join('')
118
+ end
119
+ end
120
+
121
+ spreedly = Spreedly.new("mm-test", "fbc375641ec5f13bdc10c8c8ed22f0c3e4348dcc")
122
+ spreedly.get_subscription_plans
@@ -0,0 +1,7 @@
1
+ class Spreedly
2
+ class SpreedlyError < StandardError; end
3
+ class Forbidden < SpreedlyError; end # 403 errors
4
+ class BadRequest < SpreedlyError; end # 422 errors
5
+ class NotFound < SpreedlyError; end # 404 errors
6
+ class GatewayTimeout < SpreedlyError; end # 504 errors
7
+ end
@@ -0,0 +1,31 @@
1
+ class Spreedly
2
+ class Invoice
3
+ attr_accessor :closed, :created_at, :token, :updated_at, :subscriber_store_credit, :price, :amount, :currency_code, :line_items, :subscriber
4
+ def initialize(invoice={})
5
+ self.closed = invoice["closed"]
6
+ self.created_at = invoice["created_at"]
7
+ self.token = invoice["token"]
8
+ self.updated_at = invoice["updated_at"]
9
+ self.subscriber_store_credit = invoice["subscriber_store_credit"]
10
+ self.price = invoice["price"]
11
+ self.amount = invoice["amount"]
12
+ self.currency_code = invoice["currency_code"]
13
+ self.line_items = []
14
+ invoice["line_items"].each do |line_item|
15
+ self.line_items << LineItem(line_item)
16
+ end
17
+ self.subscriber = Spreedly::Subscriber.new(invoice["subscriber"])
18
+ end
19
+
20
+ class LineItem
21
+ attr_accessor :amount, :currency_code, :description, :price, :subscription_plan_version_id
22
+ def initialize(line_item={})
23
+ self.amount = line_item["amount"]
24
+ self.currency_code = line_item["currency_code"]
25
+ self.description = line_item["description"]
26
+ self.price = line_item["price"]
27
+ self.subscription_plan_version_id = line_item["subscription_plan_version_id"]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ class Spreedly
2
+ class Request
3
+ include HTTParty
4
+ base_uri "https://spreedly.com/api/v4"
5
+ format :xml
6
+
7
+ def initialize(type, path, options)
8
+ response = self.class.send type.to_s, path, options
9
+ return response unless raise_errors(response)
10
+ end
11
+
12
+ def raise_errors(response)
13
+ if(response.is_a?(Hash))
14
+ message = "#{response[:status]}: #{response[:body]}"
15
+ code = response[:status].to_i
16
+ else
17
+ message = "#{response.status}: #{response.body}"
18
+ code = response.status.to_i
19
+ end
20
+
21
+ case code
22
+ when 403
23
+ raise(Forbidden.new, message)
24
+ when 422
25
+ raise(BadRequest.new, message)
26
+ when 404
27
+ raise(NotFound.new, message)
28
+ when 201
29
+ return false
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ class Spreedly
2
+ class Subscriber
3
+ attr_accessor :active_until, :billing_first_name, :billing_last_name, :created_at, :customer_id, :eligible_for_free_trial, :email, :lifetime_subscription, :screen_name, :store_credit, :token, :updated_at, :recurring, :card_expires_before_next_auto_renew, :subscription_plan_name, :active, :on_trial, :feature_level
4
+ def initialize(subscriber={})
5
+ self.active_until = subscriber["active_until"]
6
+ self.billing_first_name = subscriber["billing_first_name"]
7
+ self.billing_last_name = subscriber["billing_last_name"]
8
+ self.created_at = subscriber["created_at"]
9
+ self.customer_id = subscriber["customer_id"]
10
+ self.eligible_for_free_trial = subscriber["eligible_for_free_trial"]
11
+ self.email = subscriber["email"]
12
+ self.lifetime_subscription = subscriber["lifetime_subscription"]
13
+ self.screen_name = subscriber["screen_name"]
14
+ self.store_credit = subscriber["store_credit"]
15
+ self.token = subscriber["token"]
16
+ self.updated_at = subscriber["updated_at"]
17
+ self.recurring = subscriber["recurring"]
18
+ self.card_expires_before_next_auto_renew = subscriber["card_expires_before_next_auto_renew"]
19
+ self.subscription_plan_name = subscriber["subscription_plan_name"]
20
+ self.active = subscriber["active"]
21
+ self.on_trial = subscriber["on_trial"]
22
+ self.feature_level = subscriber["feature_level"]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ class Spreedly
2
+ class SubscriptionPlan
3
+ attr_accessor :force_recurring, :name, :price, :charge_after_first_period, :created_at, :charge_later_duration_units, :return_url, :updated_at, :amount, :charge_later_duration_quantity, :duration_quantity, :duration_units, :enabled, :id, :needs_to_be_renewed, :version, :plan_type, :description, :currency_code, :feature_level, :terms, :site_id
4
+ def initialize(subscription_plan={})
5
+ self.force_recurring = subscription_plan["force_recurring"]
6
+ self.name = subscription_plan["name"]
7
+ self.price = subscription_plan["price"]
8
+ self.charge_after_first_period = subscription_plan["charge_after_first_period"]
9
+ self.created_at = subscription_plan["created_at"]
10
+ self.charge_later_duration_units = subscription_plan["charge_later_duration_units"]
11
+ self.return_url = subscription_plan["return_url"]
12
+ self.updated_at = subscription_plan["updated_at"]
13
+ self.amount = subscription_plan["amount"]
14
+ self.charge_later_duration_quantity = subscription_plan["charge_later_duration_quantity"]
15
+ self.duration_quantity = subscription_plan["duration_quantity"]
16
+ self.duration_units = subscription_plan["duration_units"]
17
+ self.enabled = subscription_plan["enabled"]
18
+ self.id = subscription_plan["id"]
19
+ self.needs_to_be_renewed = subscription_plan["needs_to_be_renewed"]
20
+ self.version = subscription_plan["version"]
21
+ self.plan_type = subscription_plan["plan_type"]
22
+ self.description = subscription_plan["description"]
23
+ self.currency_code = subscription_plan["currency_code"]
24
+ self.feature_level = subscription_plan["feature_level"]
25
+ self.terms = subscription_plan["terms"]
26
+ self.site_id = subscription_plan["site_id"]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'spreedly'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Spreedly" do
4
+ before(:each) do
5
+ spreedly = Spreedly.new("mm-test", "fbc375641ec5f13bdc10c8c8ed22f0c3e4348dcc")
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erbmicha-spreedly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Erb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-26 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: This gem is a wrapper for the Spreedly Web Services API
26
+ email: michael@erbmicha.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/spreedly.rb
41
+ - lib/spreedly/error.rb
42
+ - lib/spreedly/invoice.rb
43
+ - lib/spreedly/request.rb
44
+ - lib/spreedly/subscriber.rb
45
+ - lib/spreedly/subscription_plan.rb
46
+ - spec/spec_helper.rb
47
+ - spec/spreedly_spec.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/erbmicha/spreedly
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: This gem is a wrapper for the Spreedly Web Services API.
76
+ test_files:
77
+ - spec/spec_helper.rb
78
+ - spec/spreedly_spec.rb