simplepay 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data.tar.gz.sig +2 -0
  2. data/History.txt +5 -0
  3. data/Manifest.txt +38 -0
  4. data/README.rdoc +78 -0
  5. data/Rakefile +24 -0
  6. data/lib/simplepay.rb +26 -0
  7. data/lib/simplepay/authentication.rb +69 -0
  8. data/lib/simplepay/constants.rb +26 -0
  9. data/lib/simplepay/errors.rb +16 -0
  10. data/lib/simplepay/helpers/form_helper.rb +29 -0
  11. data/lib/simplepay/helpers/rails_helper.rb +28 -0
  12. data/lib/simplepay/service.rb +122 -0
  13. data/lib/simplepay/services/standard.rb +36 -0
  14. data/lib/simplepay/services/subscription.rb +53 -0
  15. data/lib/simplepay/support.rb +15 -0
  16. data/lib/simplepay/support/amount.rb +55 -0
  17. data/lib/simplepay/support/billing_frequency.rb +14 -0
  18. data/lib/simplepay/support/boolean.rb +28 -0
  19. data/lib/simplepay/support/currency.rb +21 -0
  20. data/lib/simplepay/support/epoch.rb +39 -0
  21. data/lib/simplepay/support/field.rb +147 -0
  22. data/lib/simplepay/support/interval.rb +143 -0
  23. data/lib/simplepay/support/subscription_period.rb +25 -0
  24. data/script/console +10 -0
  25. data/script/destroy +14 -0
  26. data/script/generate +14 -0
  27. data/simplepay.gemspec +43 -0
  28. data/test/simplepay/services/test_subscription.rb +109 -0
  29. data/test/simplepay/support/test_amount.rb +46 -0
  30. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  31. data/test/simplepay/support/test_boolean.rb +17 -0
  32. data/test/simplepay/support/test_epoch.rb +34 -0
  33. data/test/simplepay/support/test_field.rb +99 -0
  34. data/test/simplepay/support/test_interval.rb +92 -0
  35. data/test/simplepay/support/test_subscription_period.rb +49 -0
  36. data/test/simplepay/test_authentication.rb +33 -0
  37. data/test/simplepay/test_service.rb +118 -0
  38. data/test/test_helper.rb +77 -0
  39. data/test/test_simplepay.rb +11 -0
  40. metadata +155 -0
  41. metadata.gz.sig +0 -0
@@ -0,0 +1,25 @@
1
+ require 'simplepay/support/interval'
2
+
3
+ module Simplepay
4
+ module Support
5
+
6
+ class SubscriptionPeriod < Interval
7
+ ALLOWED_INTERVALS = Simplepay::Intervals + ['forever']
8
+
9
+ # Limited to 3 digits.
10
+ ALLOWED_QUANTITY_RANGE = (1...1000)
11
+
12
+ ##
13
+ # See Simplepay::Support::Interval.to_s for more information.
14
+ #
15
+ # If the subscription lasts "forever" then Amazon does not need an
16
+ # interval string.
17
+ #
18
+ def to_s
19
+ super unless interval == 'forever'
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/simplepay.rb'}"
9
+ puts "Loading simplepay gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/simplepay.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{simplepay}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Nathaniel E. Bibler"]
9
+ s.cert_chain = ["/Users/nathan/.gem/gem-public_cert.pem"]
10
+ s.date = %q{2008-12-06}
11
+ s.description = %q{This gem provides a Rails interface to the Amazon Simple Pay payment service.}
12
+ s.email = ["gem@nathanielbibler.com"]
13
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
14
+ s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/simplepay.rb", "lib/simplepay/authentication.rb", "lib/simplepay/constants.rb", "lib/simplepay/errors.rb", "lib/simplepay/helpers/form_helper.rb", "lib/simplepay/helpers/rails_helper.rb", "lib/simplepay/service.rb", "lib/simplepay/services/standard.rb", "lib/simplepay/services/subscription.rb", "lib/simplepay/support.rb", "lib/simplepay/support/amount.rb", "lib/simplepay/support/billing_frequency.rb", "lib/simplepay/support/boolean.rb", "lib/simplepay/support/currency.rb", "lib/simplepay/support/epoch.rb", "lib/simplepay/support/field.rb", "lib/simplepay/support/interval.rb", "lib/simplepay/support/subscription_period.rb", "script/console", "script/destroy", "script/generate", "test/simplepay/services/test_subscription.rb", "test/simplepay/support/test_amount.rb", "test/simplepay/support/test_billing_frequency.rb", "test/simplepay/support/test_boolean.rb", "test/simplepay/support/test_epoch.rb", "test/simplepay/support/test_field.rb", "test/simplepay/support/test_interval.rb", "test/simplepay/support/test_subscription_period.rb", "test/simplepay/test_authentication.rb", "test/simplepay/test_service.rb", "test/test_helper.rb", "test/test_simplepay.rb"]
15
+ s.has_rdoc = true
16
+ s.homepage = %q{http://simplepay.rubyforge.org}
17
+ s.rdoc_options = ["--main", "README.rdoc"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{simplepay}
20
+ s.rubygems_version = %q{1.3.1}
21
+ s.signing_key = %q{/Users/nathan/.gem/gem-private_key.pem}
22
+ s.summary = %q{This gem provides a Rails interface to the Amazon Simple Pay payment service.}
23
+ s.test_files = ["test/simplepay/services/test_subscription.rb", "test/simplepay/support/test_amount.rb", "test/simplepay/support/test_billing_frequency.rb", "test/simplepay/support/test_boolean.rb", "test/simplepay/support/test_epoch.rb", "test/simplepay/support/test_field.rb", "test/simplepay/support/test_interval.rb", "test/simplepay/support/test_subscription_period.rb", "test/simplepay/test_authentication.rb", "test/simplepay/test_service.rb", "test/test_helper.rb", "test/test_simplepay.rb"]
24
+
25
+ if s.respond_to? :specification_version then
26
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
27
+ s.specification_version = 2
28
+
29
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
31
+ s.add_development_dependency(%q<newgem>, [">= 1.1.0"])
32
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
33
+ else
34
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
35
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
36
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
40
+ s.add_dependency(%q<newgem>, [">= 1.1.0"])
41
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
42
+ end
43
+ end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/services/subscription'
3
+
4
+ class Simplepay::Services::TestSubscription < Test::Unit::TestCase
5
+
6
+ def self.model_class; Simplepay::Services::Subscription; end
7
+
8
+ context 'Simplepay::Services::Subscription' do
9
+
10
+ should_have_service_field :immediate_return,
11
+ :class => Simplepay::Support::Boolean,
12
+ :as => 'immediateReturn',
13
+ :required => false
14
+
15
+ should_have_service_field :number_of_promotion_transactions,
16
+ :as => 'noOfPromotionTransactions',
17
+ :required => false
18
+
19
+ should_have_service_field :start_date,
20
+ :class => Simplepay::Support::Epoch,
21
+ :as => 'recurringStartDate',
22
+ :required => false
23
+
24
+ should_have_service_field :collect_shipping_address,
25
+ :as => 'collectShippingAddress',
26
+ :required => false,
27
+ :class => Simplepay::Support::Boolean
28
+
29
+ should_have_service_field :promotion_amount,
30
+ :as => 'promotionAmount',
31
+ :required => false,
32
+ :class => Simplepay::Support::Amount
33
+
34
+ should_have_service_field :access_key,
35
+ :as => 'accessKey',
36
+ :required => true
37
+
38
+ should_have_service_field :subscription_period,
39
+ :as => 'subscriptionPeriod',
40
+ :required => false,
41
+ :class => Simplepay::Support::SubscriptionPeriod
42
+
43
+ should_have_service_field :reference_id,
44
+ :as => 'referenceId',
45
+ :required => false
46
+
47
+ should_have_service_field :recurring_frequency,
48
+ :as => 'recurringFrequency',
49
+ :required => true,
50
+ :class => Simplepay::Support::BillingFrequency
51
+
52
+ should_have_service_field :amount,
53
+ :as => 'amount',
54
+ :required => true,
55
+ :class => Simplepay::Support::Amount
56
+
57
+ should_have_service_field :variable_marketplace_fee,
58
+ :as => 'variableMarketplaceFee',
59
+ :required => false
60
+
61
+ should_have_service_field :signature,
62
+ :as => 'signature',
63
+ :required => true
64
+
65
+ should_have_service_field :donation_widget,
66
+ :as => 'isDonationWidget',
67
+ :required => false
68
+
69
+ should_have_service_field :fixed_marketplace_fee,
70
+ :as => 'fixedMarketplaceFee',
71
+ :required => false
72
+
73
+ should_have_service_field :auto_renew,
74
+ :as => 'isAutoRenewal',
75
+ :required => false,
76
+ :class => Simplepay::Support::Boolean
77
+
78
+ should_have_service_field :description,
79
+ :as => 'description',
80
+ :required => true
81
+
82
+ should_have_service_field :account_id,
83
+ :as => 'amazonPaymentsAccountId',
84
+ :required => true
85
+
86
+ should_have_service_field :ipn_url,
87
+ :as => 'ipnUrl',
88
+ :required => false
89
+
90
+ should_have_service_field :return_url,
91
+ :as => 'returnUrl',
92
+ :required => false
93
+
94
+ should_have_service_field :process_immediately,
95
+ :as => 'processImmediate',
96
+ :required => false,
97
+ :class => Simplepay::Support::Boolean
98
+
99
+ should_have_service_field :cobranding_style,
100
+ :as => 'cobrandingStyle',
101
+ :required => true
102
+
103
+ should_have_service_field :abandon_url,
104
+ :as => 'abandonUrl',
105
+ :required => false
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/amount'
3
+
4
+ class Simplepay::Support::TestAmount < Test::Unit::TestCase
5
+
6
+ context 'Simplepay::Support::Amount' do
7
+
8
+ should 'convert to Amazon formatted string' do
9
+ assert_equal 'USD 5.00', new_amount(5).to_s
10
+ assert_equal 'USD 6.10', new_amount(6.1).to_s
11
+ assert_equal 'USD 5.25', new_amount(5.25).to_s
12
+ end
13
+
14
+ should 'raise an error for negative amounts' do
15
+ assert_raise(ArgumentError) { new_amount(-1) }
16
+ end
17
+
18
+ should 'be able to set your desired currency by code' do
19
+ amount = new_amount(1, 'USD')
20
+ assert_equal 'USD', amount.currency.to_s
21
+ end
22
+
23
+ should 'raise an error when using an unrecognized currency code' do
24
+ assert_raise(ArgumentError) { new_amount(1, 'BADCURRENCYCODE') }
25
+ end
26
+
27
+ should 'be able to use a custom currency by object' do
28
+ custom = Simplepay::Support::Currency.new('Test Currency', 'TEST', "%d")
29
+ amount = new_amount(1, custom)
30
+ assert_equal custom, amount.currency
31
+ assert_equal "TEST 1", amount.to_s
32
+ end
33
+
34
+ end
35
+
36
+
37
+ private
38
+
39
+
40
+ def new_amount(amount, currency = nil)
41
+ currency ?
42
+ Simplepay::Support::Amount.new(amount, currency) :
43
+ Simplepay::Support::Amount.new(amount)
44
+ end
45
+
46
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/billing_frequency'
3
+
4
+ class Simplepay::Support::TestBillingFrequency < Test::Unit::TestCase
5
+
6
+ context 'Simplepay::Support::BillingFrequency' do
7
+
8
+ setup do
9
+ @interval = Simplepay::Support::BillingFrequency.new
10
+ end
11
+
12
+ should 'not set default values' do
13
+ assert_nil @interval.interval
14
+ assert_nil @interval.quantity
15
+ end
16
+
17
+ should 'only allow defined intervals' do
18
+ intervals = ['day', 'week', 'month', 'year']
19
+ added = @interval.class.allowed_intervals - intervals
20
+ missed = intervals - @interval.class.allowed_intervals
21
+ assert added.empty?, "#{@interval.class.name} defines unexpected intervals: #{added.inspect}"
22
+ assert missed.empty?, "#{@interval.class.name} failed to define expected intervals: #{missed.inspect}"
23
+ end
24
+
25
+ should 'limit quantities to two digits' do
26
+ assert_raise(ArgumentError) { @interval.quantity = 100 }
27
+ end
28
+
29
+ should 'disallow 0 quantity' do
30
+ assert_raise(ArgumentError) { @interval.quantity = 0 }
31
+ end
32
+
33
+ should 'disallow negative quantities' do
34
+ assert_raise(ArgumentError) { @interval.quantity = -1 }
35
+ end
36
+
37
+ should 'convert to String' do
38
+ assert_equal '5 year', @interval.class.new(5, 'year').to_s
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Simplepay::Support::BooleanTest < Test::Unit::TestCase
4
+
5
+ context 'Simplepay::Support::Boolean' do
6
+
7
+ should 'use "1" for true' do
8
+ assert_equal '1', Simplepay::Support::Boolean.new(true).to_s
9
+ end
10
+
11
+ should 'use "0" for false' do
12
+ assert_equal '0', Simplepay::Support::Boolean.new(false).to_s
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/epoch'
3
+
4
+ class Simplepay::Support::TestEpoch < Test::Unit::TestCase
5
+
6
+ context "Simplepay::Support::Epoch" do
7
+
8
+ should 'return time as an integer' do
9
+ time = Time.now
10
+ assert_equal time.to_i.to_s, new_epoch(time).to_s
11
+ end
12
+
13
+ should 'parse a given string for Time' do
14
+ assert_equal "1199150625", new_epoch("2008-01-01 01:23:45Z").to_s
15
+ end
16
+
17
+ should 'represent seconds since January 1, 1970' do
18
+ time = Time.parse("1970-01-01 00:00:00Z")
19
+ now = Time.now
20
+ seconds = (now - time).to_i
21
+ assert_equal seconds.to_s, new_epoch(now).to_s
22
+ end
23
+
24
+ end
25
+
26
+
27
+ private
28
+
29
+
30
+ def new_epoch(value)
31
+ Simplepay::Support::Epoch.new(value)
32
+ end
33
+
34
+ end
@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/field'
3
+
4
+ class DelegateTest
5
+ def initialize(v); end
6
+ def to_s; end
7
+ end
8
+
9
+ class Simplepay::Support::TestField < Test::Unit::TestCase
10
+
11
+ context 'Simplepay::Support::Field' do
12
+
13
+ should 'raise error for invalid options' do
14
+ assert_raise(Simplepay::InvalidOptions) { new_field(:bad, {:bad_field => 'foo'}) }
15
+ end
16
+
17
+ should 'set the value via options' do
18
+ assert_equal 'set value', new_field(:test, :value => 'set value').value
19
+ end
20
+
21
+ should 'camelize the service name when given a symbol' do
22
+ assert_equal 'testField', new_field(:test_field).service_name
23
+ end
24
+
25
+ should 'use the given string name as service name' do
26
+ assert_equal 'TestField', new_field('TestField').service_name
27
+ assert_equal 'test_field', new_field('test_field').service_name
28
+ end
29
+
30
+ should 'use the :as option to override the service name' do
31
+ assert_equal 'Overridden', new_field(:original, :as => 'Overridden').service_name
32
+ end
33
+
34
+ should 'use the service name when converted to String' do
35
+ field = new_field(:test_field)
36
+ assert_equal field.service_name, field.to_s
37
+ end
38
+
39
+ should 'not be required by default' do
40
+ assert !new_field(:optional_field).required?
41
+ end
42
+
43
+ should 'be required' do
44
+ assert new_field(:required_field, :required => true).required?
45
+ end
46
+
47
+ should 'change the service parent for cloned objects' do
48
+ field1 = new_field(:clone)
49
+ field2 = field1.clone_for(new_service)
50
+ assert_equal field1.name, field2.name
51
+ assert_equal field1.value, field2.value
52
+ assert_not_equal field1.service, field2.service
53
+ end
54
+
55
+ should 'be equivalent' do
56
+ field1 = new_field(:equal, {})
57
+ field2 = new_field(:equal, {}, field1.service)
58
+ assert_equal field1, field2
59
+ end
60
+
61
+ should 'not be equivalent' do
62
+ field1 = new_field(:equal, {}, new_service)
63
+ field2 = new_field(:equal, {}, new_service)
64
+ assert_not_equal field1, field2
65
+ end
66
+
67
+ should 'correctly sort' do
68
+ fielda, fieldb, fieldc = new_field(:a), new_field(:b), new_field(:c)
69
+ assert_equal [fielda, fieldb, fieldc], [fieldc, fielda, fieldb].sort
70
+ end
71
+
72
+ should 'pass itself as a proc parameter if value is a proc' do
73
+ field = new_field(:test, {:value => Proc.new { |f| f}})
74
+ assert_equal field, field.value
75
+ end
76
+
77
+ should 'be able to delegate value handling to another class' do
78
+ delegate = DelegateTest.new('foo')
79
+ DelegateTest.expects(:new).with('foo').once.returns(delegate)
80
+ delegate.expects(:to_s).times(2).returns('mocked')
81
+ field = new_field(:delegated, {:class => DelegateTest, :value => 'foo'})
82
+ assert_equal delegate.to_s, field.value
83
+ end
84
+
85
+ end
86
+
87
+
88
+ private
89
+
90
+
91
+ def new_field(name, options = {}, service = new_service)
92
+ Simplepay::Support::Field.new(service, name, options)
93
+ end
94
+
95
+ def new_service
96
+ Simplepay::Service.new
97
+ end
98
+
99
+ end