harleytt-simplepay 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/History.txt +25 -0
  2. data/Manifest.txt +50 -0
  3. data/README.rdoc +127 -0
  4. data/Rakefile +25 -0
  5. data/lib/simplepay.rb +27 -0
  6. data/lib/simplepay/authentication.rb +41 -0
  7. data/lib/simplepay/constants.rb +64 -0
  8. data/lib/simplepay/errors.rb +16 -0
  9. data/lib/simplepay/helpers/form_helper.rb +29 -0
  10. data/lib/simplepay/helpers/notification_helper.rb +54 -0
  11. data/lib/simplepay/helpers/rails_helper.rb +40 -0
  12. data/lib/simplepay/rails.rb +9 -0
  13. data/lib/simplepay/service.rb +133 -0
  14. data/lib/simplepay/services/donation.rb +91 -0
  15. data/lib/simplepay/services/marketplace.rb +89 -0
  16. data/lib/simplepay/services/marketplace_policy.rb +54 -0
  17. data/lib/simplepay/services/standard.rb +58 -0
  18. data/lib/simplepay/services/subscription.rb +96 -0
  19. data/lib/simplepay/support.rb +16 -0
  20. data/lib/simplepay/support/amount.rb +55 -0
  21. data/lib/simplepay/support/billing_frequency.rb +14 -0
  22. data/lib/simplepay/support/boolean.rb +28 -0
  23. data/lib/simplepay/support/currency.rb +21 -0
  24. data/lib/simplepay/support/epoch.rb +39 -0
  25. data/lib/simplepay/support/field.rb +147 -0
  26. data/lib/simplepay/support/interval.rb +143 -0
  27. data/lib/simplepay/support/simple_amount.rb +37 -0
  28. data/lib/simplepay/support/subscription_period.rb +25 -0
  29. data/script/console +10 -0
  30. data/script/destroy +14 -0
  31. data/script/generate +14 -0
  32. data/simplepay.gemspec +41 -0
  33. data/test/simplepay/helpers/test_notifier.rb +32 -0
  34. data/test/simplepay/services/test_donation.rb +85 -0
  35. data/test/simplepay/services/test_marketplace.rb +85 -0
  36. data/test/simplepay/services/test_marketplace_policy.rb +52 -0
  37. data/test/simplepay/services/test_standard.rb +71 -0
  38. data/test/simplepay/services/test_subscription.rb +109 -0
  39. data/test/simplepay/support/test_amount.rb +46 -0
  40. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  41. data/test/simplepay/support/test_boolean.rb +17 -0
  42. data/test/simplepay/support/test_epoch.rb +34 -0
  43. data/test/simplepay/support/test_field.rb +99 -0
  44. data/test/simplepay/support/test_interval.rb +92 -0
  45. data/test/simplepay/support/test_simple_amount.rb +28 -0
  46. data/test/simplepay/support/test_subscription_period.rb +49 -0
  47. data/test/simplepay/test_authentication.rb +25 -0
  48. data/test/simplepay/test_service.rb +118 -0
  49. data/test/test_helper.rb +87 -0
  50. data/test/test_simplepay.rb +11 -0
  51. metadata +184 -0
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/simple_amount'
3
+
4
+ class Simplepay::Support::TestSimpleAmount < Test::Unit::TestCase
5
+
6
+ context 'Simplepay::Support::SimpleAmount' do
7
+
8
+ should 'convert to Amazon formatted string' do
9
+ assert_equal '5.00', new_amount(5).to_s
10
+ assert_equal '6.10', new_amount(6.1).to_s
11
+ assert_equal '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
+ end
19
+
20
+
21
+ private
22
+
23
+
24
+ def new_amount(amount)
25
+ Simplepay::Support::SimpleAmount.new(amount)
26
+ end
27
+
28
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+ require 'simplepay/support/subscription_period'
3
+
4
+ class Simplepay::Support::TestSubscriptionPeriod < Test::Unit::TestCase
5
+
6
+ context 'Simplepay::Support::SubscriptionPeriod' do
7
+
8
+ setup do
9
+ @interval = Simplepay::Support::SubscriptionPeriod.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', 'forever']
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 three digits' do
26
+ assert_nothing_raised(ArgumentError) { @interval.quantity = 999 }
27
+ assert_raise(ArgumentError) { @interval.quantity = 1000 }
28
+ end
29
+
30
+ should 'disallow 0 quantity' do
31
+ assert_raise(ArgumentError) { @interval.quantity = 0 }
32
+ end
33
+
34
+ should 'disallow negative quantities' do
35
+ assert_raise(ArgumentError) { @interval.quantity = -1 }
36
+ end
37
+
38
+ should 'convert to String' do
39
+ assert_equal '5 year', @interval.class.new(5, 'year').to_s
40
+ end
41
+
42
+ should 'return nil when converted to string with FOREVER interval' do
43
+ @interval.interval = 'forever'
44
+ assert_nil @interval.to_s
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'simplepay/authentication'
3
+
4
+ class Simplepay::TestAuthentication < Test::Unit::TestCase
5
+
6
+ context 'Simplepay::Authentication' do
7
+
8
+ setup do
9
+ @uri = URI.parse('https://authorize.payments-sandbox.amazon.com/pba/pipeline')
10
+ @secret_key = "TESTINGKEY"
11
+ @signature = 'Ni8YGsrFZmAJwQN8mCZvTqOlPuNiNCzoi4LqyQS4ums='
12
+ @data = {
13
+ :symbol => 'string',
14
+ 'string' => 1,
15
+ 2 => :symbol
16
+ }
17
+ end
18
+
19
+ should 'compute an Amazon signature for hash data' do
20
+ assert_equal @signature, Simplepay::Signature.new(@uri, @data, @secret_key).sign
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,118 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'rexml/document'
3
+ require 'simplepay/service'
4
+
5
+ class TestService < Simplepay::Service
6
+ ENDPOINT_URL = 'http://test.host.url/api'
7
+ SANDBOX_URL = 'http://test.host.url/sandbox'
8
+
9
+ set_submit_tag 'testing'
10
+
11
+ field :field_1
12
+ field :field_2
13
+ required_field :required
14
+ required_field :required2, :value => 'preset'
15
+ end
16
+
17
+ class Simplepay::TestService < Test::Unit::TestCase
18
+
19
+ context 'Simplepay::Service' do
20
+
21
+ setup do
22
+ @service = ::TestService.new
23
+ end
24
+
25
+ context 'class' do
26
+
27
+ setup do
28
+ @class = @service.class
29
+ end
30
+
31
+ should 'contain the defined fields' do
32
+ fields = @class.fields
33
+ assert_not_nil fields
34
+ assert !fields.empty?
35
+ end
36
+
37
+ should 'contain required field(s)' do
38
+ fields = @class.fields.select { |f| f.required? }
39
+ assert fields.size > 0
40
+ end
41
+
42
+ should 'have different field instances than its service instance' do
43
+ class_fields, instance_fields = @class.fields, @service.fields
44
+ instance_fields.each do |instance_field|
45
+ assert !class_fields.include?(instance_field)
46
+ assert class_fields.any? {|f| instance_field.service_name == f.service_name }
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ should 'return a collection of fields' do
53
+ assert !@service.fields.empty?
54
+ assert @service.fields.all? { |f| f.kind_of?(Simplepay::Support::Field) }
55
+ end
56
+
57
+ should 'use the live endpoint url' do
58
+ assert_equal TestService::ENDPOINT_URL, @service.url(false)
59
+ end
60
+
61
+ should 'use the sandbox url' do
62
+ assert_equal TestService::SANDBOX_URL, @service.url(true)
63
+ end
64
+
65
+ should 'generate an HTML FORM' do
66
+ @service.required = 'set'
67
+ assert_match(/\A<form/i, @service.form)
68
+ assert_match(/<\/form>\Z/i, @service.form)
69
+ end
70
+
71
+ should 'post the form to the endpoint' do
72
+ assert_match(/<form action="#{Regexp.escape('http://test.host.url/sandbox')}" method="post"/, @service.form({:required => 'set'}))
73
+ end
74
+
75
+ should 'generate HIDDEN HTML INPUTs for each non-empty field' do
76
+ @service.required = 'Test Field'
77
+ @service.field_1 = 'Testing'
78
+
79
+ defined_fields = @service.fields.select { |f| !f.value.blank? }
80
+ assert defined_fields.size > 0, "This test cannot be run without defined fields"
81
+ document = REXML::Document.new(@service.form)
82
+ form_inputs = []
83
+ document.root.each_element("//input[@type='hidden']") do |e|
84
+ field = defined_fields.detect { |f| f.service_name == e.attributes['name'] }
85
+ assert field, "#{e} unrecognized"
86
+ form_inputs << e
87
+ end
88
+ assert_equal defined_fields.size, form_inputs.size
89
+ end
90
+
91
+ should 'allow fields to be set through hash' do
92
+ assert_nothing_raised(Simplepay::RequiredFieldMissing) do
93
+ TestService.new.form({:required => 'covered'})
94
+ end
95
+ end
96
+
97
+ should 'raise an error for undefined required fields' do
98
+ assert_raise(Simplepay::RequiredFieldMissing) { @service.form }
99
+ end
100
+
101
+ should 'default to a basic HTML SUBMIT button' do
102
+ document = REXML::Document.new(@service.form({:required => 'set'}))
103
+ assert_not_nil document.root.elements["input[@type='submit']"]
104
+ end
105
+
106
+ should 'allow class definition of the default submit text' do
107
+ document = REXML::Document.new(@service.form({:required => 'set'}))
108
+ assert_not_nil document.root.elements["input[@type='submit' and @value='testing']"]
109
+ end
110
+
111
+ should 'allow the HTML SUBMIT button to be overridden' do
112
+ document = REXML::Document.new(@service.form({:required => 'set'}, Simplepay::Helpers::FormHelper.tag(:input, {:type => 'submit', :value => 'Send It'})))
113
+ assert_not_nil document.root.elements["input[@type='submit' and @value='Send It']"]
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,87 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+
4
+ require 'rubygems'
5
+ require 'redgreen' rescue nil
6
+
7
+ require 'shoulda'
8
+ require 'mocha'
9
+
10
+ require File.dirname(__FILE__) + '/../lib/simplepay'
11
+
12
+
13
+ module Simplepay #:nodoc:
14
+ module Macros #:nodoc: all
15
+
16
+ def should_not_have_service_field(*names)
17
+ klass = model_class
18
+
19
+ names.each do |name|
20
+ should "not have a field named #{name.inspect}" do
21
+ assert !klass.fields.any? { |f| f.name == name }
22
+ end
23
+ end
24
+ end
25
+
26
+ def should_have_service_field(*names)
27
+ delegate_class, service_name, required = get_options!(names, :class, :as, :required)
28
+ klass = model_class
29
+
30
+ names.each do |name|
31
+ should "have a field named #{name.inspect}" do
32
+ message = "#{klass} does not have a Field named #{name.inspect}"
33
+ assert klass.fields.any? { |f| f.name == name }, message
34
+ end
35
+
36
+ context "#{name} field" do
37
+ should("delegate to #{delegate_class}") do
38
+ message = "#{klass} field #{name.inspect} failed to delegate to #{delegate_class}"
39
+ field = klass.fields.detect { |f| f.name == name }
40
+ assert_equal delegate_class, field.delegate, message
41
+ end if delegate_class
42
+
43
+ should("not delegate") do
44
+ field = klass.fields.detect { |f| f.name == name }
45
+ message = "#{klass} field #{name.inspect} unexpectedly delegated to #{field.delegate}"
46
+ assert !field.delegated?, message
47
+ end unless delegate_class
48
+
49
+ should("have a #{service_name} service name") do
50
+ message = "#{klass} field #{name.inspect} failed to have expected service name"
51
+ field = klass.fields.detect { |f| f.name == name }
52
+ assert_equal service_name, field.service_name
53
+ end if service_name
54
+
55
+ should("#{"not " unless required}be required") do
56
+ message = "#{klass} field #{name.inspect} was#{" not" if required} required"
57
+ field = klass.fields.detect { |f| f.name == name }
58
+ assert_equal required, field.required?, message
59
+ end
60
+ end
61
+ end
62
+ end unless method_defined?(:should_have_service_field)
63
+
64
+
65
+ private
66
+
67
+
68
+ # Returns the values for the entries in the args hash who's keys are listed in the wanted array.
69
+ # Will raise if there are keys in the args hash that aren't listed.
70
+ def get_options!(args, *wanted)
71
+ ret = []
72
+ opts = (args.last.is_a?(Hash) ? args.pop : {})
73
+ wanted.each {|w| ret << opts.delete(w)}
74
+ raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
75
+ return *ret
76
+ end unless defined?(:get_options!)
77
+
78
+ end
79
+ end
80
+
81
+ module Test #:nodoc: all
82
+ module Unit
83
+ class TestCase
84
+ extend Simplepay::Macros
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSimplepay < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: harleytt-simplepay
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 2
10
+ version: 0.2.2
11
+ platform: ruby
12
+ authors:
13
+ - Nathaniel E. Bibler
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2009-06-07 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 2
34
+ version: 2.0.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: newgem
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 1
48
+ - 3
49
+ - 0
50
+ version: 1.3.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: hoe
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 55
62
+ segments:
63
+ - 1
64
+ - 8
65
+ - 0
66
+ version: 1.8.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: This gem provides a Rails interface to the Amazon Simple Pay payment service.
70
+ email:
71
+ - gem@nathanielbibler.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - History.txt
78
+ - Manifest.txt
79
+ - README.rdoc
80
+ files:
81
+ - History.txt
82
+ - Manifest.txt
83
+ - README.rdoc
84
+ - Rakefile
85
+ - lib/simplepay.rb
86
+ - lib/simplepay/authentication.rb
87
+ - lib/simplepay/constants.rb
88
+ - lib/simplepay/errors.rb
89
+ - lib/simplepay/helpers/form_helper.rb
90
+ - lib/simplepay/helpers/notification_helper.rb
91
+ - lib/simplepay/helpers/rails_helper.rb
92
+ - lib/simplepay/rails.rb
93
+ - lib/simplepay/service.rb
94
+ - lib/simplepay/services/donation.rb
95
+ - lib/simplepay/services/marketplace.rb
96
+ - lib/simplepay/services/marketplace_policy.rb
97
+ - lib/simplepay/services/standard.rb
98
+ - lib/simplepay/services/subscription.rb
99
+ - lib/simplepay/support.rb
100
+ - lib/simplepay/support/amount.rb
101
+ - lib/simplepay/support/billing_frequency.rb
102
+ - lib/simplepay/support/boolean.rb
103
+ - lib/simplepay/support/currency.rb
104
+ - lib/simplepay/support/epoch.rb
105
+ - lib/simplepay/support/field.rb
106
+ - lib/simplepay/support/interval.rb
107
+ - lib/simplepay/support/simple_amount.rb
108
+ - lib/simplepay/support/subscription_period.rb
109
+ - script/console
110
+ - script/destroy
111
+ - script/generate
112
+ - simplepay.gemspec
113
+ - test/simplepay/helpers/test_notifier.rb
114
+ - test/simplepay/services/test_donation.rb
115
+ - test/simplepay/services/test_marketplace.rb
116
+ - test/simplepay/services/test_marketplace_policy.rb
117
+ - test/simplepay/services/test_standard.rb
118
+ - test/simplepay/services/test_subscription.rb
119
+ - test/simplepay/support/test_amount.rb
120
+ - test/simplepay/support/test_billing_frequency.rb
121
+ - test/simplepay/support/test_boolean.rb
122
+ - test/simplepay/support/test_epoch.rb
123
+ - test/simplepay/support/test_field.rb
124
+ - test/simplepay/support/test_interval.rb
125
+ - test/simplepay/support/test_subscription_period.rb
126
+ - test/simplepay/test_authentication.rb
127
+ - test/simplepay/test_service.rb
128
+ - test/test_helper.rb
129
+ - test/test_simplepay.rb
130
+ - test/simplepay/support/test_simple_amount.rb
131
+ has_rdoc: true
132
+ homepage: http://simplepay.rubyforge.org
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options:
137
+ - --main
138
+ - README.rdoc
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ hash: 3
156
+ segments:
157
+ - 0
158
+ version: "0"
159
+ requirements: []
160
+
161
+ rubyforge_project: simplepay
162
+ rubygems_version: 1.3.7
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: This gem provides a Rails interface to the Amazon Simple Pay payment service.
166
+ test_files:
167
+ - test/simplepay/helpers/test_notifier.rb
168
+ - test/simplepay/services/test_donation.rb
169
+ - test/simplepay/services/test_marketplace.rb
170
+ - test/simplepay/services/test_marketplace_policy.rb
171
+ - test/simplepay/services/test_standard.rb
172
+ - test/simplepay/services/test_subscription.rb
173
+ - test/simplepay/support/test_amount.rb
174
+ - test/simplepay/support/test_billing_frequency.rb
175
+ - test/simplepay/support/test_boolean.rb
176
+ - test/simplepay/support/test_epoch.rb
177
+ - test/simplepay/support/test_field.rb
178
+ - test/simplepay/support/test_interval.rb
179
+ - test/simplepay/support/test_simple_amount.rb
180
+ - test/simplepay/support/test_subscription_period.rb
181
+ - test/simplepay/test_authentication.rb
182
+ - test/simplepay/test_service.rb
183
+ - test/test_helper.rb
184
+ - test/test_simplepay.rb