apruve 0.9.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.
@@ -0,0 +1,108 @@
1
+ module Apruve
2
+
3
+ module Utils
4
+
5
+ def callable( callable_or_not )
6
+ callable_or_not.respond_to?(:call) ? callable_or_not : lambda { callable_or_not }
7
+ end
8
+
9
+ def camelize(underscored_word)
10
+ underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
11
+ end
12
+
13
+ def classify(table_name)
14
+ camelize singularize(table_name.to_s.sub(/.*\./, ''))
15
+ end
16
+
17
+ def demodulize(class_name_in_module)
18
+ class_name_in_module.to_s.sub(/^.*::/, '')
19
+ end
20
+
21
+ def pluralize(word)
22
+ word.to_s.sub(/([^s])$/, '\1s')
23
+ end
24
+
25
+ def singularize(word)
26
+ word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
27
+ end
28
+
29
+ def underscore(camel_cased_word)
30
+ word = camel_cased_word.to_s.dup
31
+ word.gsub!(/::/, '/')
32
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
33
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
34
+ word.tr! '-', '_'
35
+ word.downcase!
36
+ word
37
+ end
38
+
39
+ def extract_href_from_object(object)
40
+ object.respond_to?(:href) ? object.href : object
41
+ end
42
+
43
+ def indifferent_read_access(base = {})
44
+ indifferent = Hash.new do |hash, key|
45
+ hash[key.to_s] if key.is_a? Symbol
46
+ end
47
+ base.each_pair do |key, value|
48
+ if value.is_a? Hash
49
+ value = indifferent_read_access value
50
+ elsif value.respond_to? :each
51
+ if value.respond_to? :map!
52
+ value.map! do |v|
53
+ if v.is_a? Hash
54
+ v = indifferent_read_access v
55
+ end
56
+ v
57
+ end
58
+ else
59
+ value.map do |v|
60
+ if v.is_a? Hash
61
+ v = indifferent_read_access v
62
+ end
63
+ v
64
+ end
65
+ end
66
+ end
67
+ indifferent[key.to_s] = value
68
+ end
69
+ indifferent
70
+ end
71
+
72
+ def stringify_keys!(hash)
73
+ hash.keys.each do |key|
74
+ stringify_keys! hash[key] if hash[key].is_a? Hash
75
+ hash[key.to_s] = hash.delete key if key.is_a? Symbol
76
+ end
77
+ end
78
+
79
+ # Validate all keys in a hash match *valid keys, raising ArgumentError on a mismatch.
80
+ # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
81
+ # as keys, this will fail.
82
+ #
83
+ # ==== Examples
84
+ # { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
85
+ # { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
86
+ # { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
87
+ def assert_valid_keys(hash, *valid_keys)
88
+ unknown_keys = hash.keys - [valid_keys].flatten
89
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(', ')}") unless unknown_keys.empty?
90
+ end
91
+
92
+ # http://pablomanrubia.com/2011/03/extending-ruby-to-validate-required-keys-in-a-hash-table/
93
+ #def assert_required_keys(hash, params)
94
+ # params[:required] ||= []
95
+ # params[:optional] ||= []
96
+ # assert_valid_keys(hash, params[:required] + params[:optional])
97
+ # pending_keys = params[:required] - hash.keys
98
+ # raise(ArgumentError, "Required key(s) not present: #{pending_keys.join(', ')}") unless pending_keys.empty?
99
+ #end
100
+
101
+ def assert_required_keys(hash, params)
102
+ params[:required] ||= []
103
+ pending_keys = params[:required] - hash.keys
104
+ raise(ArgumentError, "Required key(s) not present: #{pending_keys.join(', ')}") unless pending_keys.empty?
105
+ end
106
+ extend self
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Apruve
2
+ VERSION = '0.9.0'
3
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Apruve' do
4
+ before :each do
5
+ # reset the gem
6
+ Apruve.configure
7
+ end
8
+
9
+ describe '#js' do
10
+ describe 'test' do
11
+ let (:script_tag) { '<script type="text/javascript" src="https://test.apruve.com/js/apruve.js"></script>' }
12
+ it 'should print the tag' do
13
+ Apruve.configure(nil, 'test')
14
+ expect(Apruve.js).to eq script_tag
15
+ end
16
+ end
17
+ describe 'prod' do
18
+ let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/apruve.js"></script>' }
19
+ it 'should print the tag' do
20
+ Apruve.configure(nil)
21
+ expect(Apruve.js).to eq script_tag
22
+ end
23
+ end
24
+ describe 'local' do
25
+ let (:script_tag) { '<script type="text/javascript" src="https://www.apruve.com/js/apruve.js"></script>' }
26
+ it 'should print the tag' do
27
+ Apruve.configure(nil, 'prod')
28
+ expect(Apruve.js).to eq script_tag
29
+ end
30
+ end
31
+ describe 'compact' do
32
+ let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/apruve.js?display=compact"></script>' }
33
+ it 'should print the tag' do
34
+ Apruve.configure(nil)
35
+ expect(Apruve.js('compact')).to eq script_tag
36
+ end
37
+ end
38
+ describe 'overrides' do
39
+ let (:script_tag) { '<script type="text/javascript" src="mailto://google.com:4567/js/apruve.js"></script>' }
40
+ it 'should print the tag' do
41
+ Apruve.configure(nil, 'prod', {scheme: 'mailto', host: 'google.com', port: 4567})
42
+ expect(Apruve.js).to eq script_tag
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#button' do
48
+ let(:tag) { '<div id="apruveDiv"></div>' }
49
+ it 'should print the tag' do
50
+ expect(Apruve.button).to eq tag
51
+ end
52
+ end
53
+
54
+ describe '#config' do
55
+
56
+ it 'should have correct init values' do
57
+ config = Apruve.config
58
+ expect(config[:scheme]).to eq 'http'
59
+ expect(config[:host]).to eq 'localhost'
60
+ expect(config[:port]).to eq 3000
61
+ end
62
+ end
63
+
64
+ describe '#get' do
65
+ let (:api_key) { 'an-api-key' }
66
+ let (:url) { 'example.com' }
67
+ before :each do
68
+ Apruve.configure(api_key, 'local', {url: url, port: 5923})
69
+ end
70
+ describe 'server unavailable' do
71
+ it 'should raise' do
72
+ expect { Apruve.get('gibberish') }.to raise_error(Apruve::ServiceUnreachable)
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ describe '#client' do
79
+ describe 'before configure' do
80
+ it 'should provide a client if not configured' do
81
+ expect(Apruve.client).not_to be_nil
82
+ end
83
+ end
84
+
85
+ describe 'after configure' do
86
+ let(:api_key) { 'an-api-key' }
87
+ before :each do
88
+ Apruve.configure(api_key)
89
+ end
90
+ it 'should provide a client instance' do
91
+ expect(Apruve.client).not_to be_nil
92
+ expect(Apruve.client.api_key).to eq api_key
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::Client do
4
+ let (:api_key) { 'an-api-key' }
5
+ let (:url) { 'example.com' }
6
+ before :each do
7
+ Apruve.configure(api_key, 'local', {url: url, port: 5923})
8
+ end
9
+ describe '#get' do
10
+ describe 'server unavailable' do
11
+ it 'should raise' do
12
+ expect { Apruve.get('gibberish') }.to raise_error(Apruve::ServiceUnreachable)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Apruve Configuration' do
4
+ before :each do
5
+ Apruve::Config
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::LineItem do
4
+ let (:line_item) do
5
+ Apruve::LineItem.new(
6
+ title: 'line 2',
7
+ amount_cents: '40'
8
+ )
9
+ end
10
+ subject { line_item }
11
+
12
+ it { should respond_to(:title) }
13
+ it { should respond_to(:amount_cents) }
14
+ it { should respond_to(:price_ea_cents) }
15
+ it { should respond_to(:quantity) }
16
+ it { should respond_to(:description) }
17
+ it { should respond_to(:variant_info) }
18
+ it { should respond_to(:sku) }
19
+ it { should respond_to(:vendor) }
20
+ it { should respond_to(:view_product_url) }
21
+ it { should respond_to(:plan_code) }
22
+
23
+
24
+ describe '#validate' do
25
+ describe 'no errors' do
26
+ it 'should not raise' do
27
+ expect { line_item.validate }.not_to raise_error
28
+ end
29
+ end
30
+ describe 'errors' do
31
+ before :each do
32
+ line_item.title = nil
33
+ end
34
+ it 'should raise on no title' do
35
+ expect { line_item.validate }.to raise_error(Apruve::ValidationError, '["title must be set on line items"]')
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::PaymentItem do
4
+ let (:payment_item) {Apruve::PaymentItem.new}
5
+ subject { payment_item }
6
+
7
+ it { should respond_to(:title) }
8
+ it { should respond_to(:amount_cents) }
9
+ it { should respond_to(:price_ea_cents) }
10
+ it { should respond_to(:quantity) }
11
+ it { should respond_to(:description) }
12
+ it { should respond_to(:variant_info) }
13
+ it { should respond_to(:sku) }
14
+ it { should respond_to(:vendor) }
15
+ it { should respond_to(:view_product_url) }
16
+
17
+ end
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::PaymentRequest do
4
+ before :each do
5
+ Apruve.configure('f5fbe71d68772d1f562ed6f598b995b3', 'local')
6
+ end
7
+
8
+ let (:line_items) do
9
+ [
10
+ Apruve::LineItem.new(
11
+ title: 'line 1',
12
+ amount_cents: '1230',
13
+ price_ea_cents: '123',
14
+ quantity: 10,
15
+ description: 'A line item',
16
+ variant_info: 'small',
17
+ sku: 'LINE1SKU',
18
+ vendor: 'acme, inc.',
19
+ view_product_url: 'http://www.apruve.com/doc'
20
+ ),
21
+ Apruve::LineItem.new(
22
+ title: 'line 2',
23
+ amount_cents: '40'
24
+ )
25
+ ]
26
+ end
27
+
28
+ let (:payment_request) do
29
+ Apruve::PaymentRequest.new(
30
+ merchant_id: '9999',
31
+ merchant_order_id: 'ABC',
32
+ amount_cents: 12340,
33
+ tax_cents: 0,
34
+ shipping_cents: 0,
35
+ line_items: line_items
36
+ )
37
+ end
38
+ subject { payment_request }
39
+
40
+ it { should respond_to(:merchant_id) }
41
+ it { should respond_to(:merchant_order_id) }
42
+ it { should respond_to(:amount_cents) }
43
+ it { should respond_to(:tax_cents) }
44
+ it { should respond_to(:shipping_cents) }
45
+ it { should respond_to(:line_items) }
46
+ it { should respond_to(:api_url) }
47
+ it { should respond_to(:view_url) }
48
+ it { should respond_to(:created_at) }
49
+ it { should respond_to(:updated_at) }
50
+
51
+ describe '#to_json' do
52
+ let(:expected) do
53
+ "{\"merchant_id\":\"9999\",\"merchant_order_id\":\"ABC\",\"amount_cents\":12340,\"tax_cents\":0,"\
54
+ "\"shipping_cents\":0,\"line_items\":[{\"title\":\"line 1\",\"amount_cents\":\"1230\","\
55
+ "\"price_ea_cents\":\"123\",\"quantity\":10,\"description\":\"A line item\",\"variant_info\":\"small\","\
56
+ "\"sku\":\"LINE1SKU\",\"vendor\":\"acme, inc.\",\"view_product_url\":\"http://www.apruve.com/doc\"},"\
57
+ "{\"title\":\"line 2\",\"amount_cents\":\"40\"}]}"
58
+ end
59
+ its(:to_json) { should eq expected }
60
+ end
61
+
62
+ describe '#value_string' do
63
+ let(:expected) do
64
+ "9999ABC1234000line 1123012310A line itemsmallLINE1SKUacme, inc.http://www.apruve.com/docline 240"
65
+ end
66
+ its(:value_string) { should eq expected }
67
+ end
68
+
69
+ describe '#secure_hash' do
70
+ describe 'no api_key' do
71
+ let (:error) { 'api_key has not been set. Set it with Apruve.configure(api_key, environment, options)' }
72
+ before :each do
73
+ Apruve.configure
74
+ end
75
+ it 'should raise' do
76
+ expect { payment_request.secure_hash }.to raise_error(error)
77
+ end
78
+ end
79
+ describe 'with api_key' do
80
+ let (:hash) { '527cf4d85ed1e977c89a1099197d90f00aab9eda1fd3f97538b7e0909593f07f' }
81
+ let (:api_key) { 'an_api_key' }
82
+ before :each do
83
+ Apruve.configure(api_key)
84
+ end
85
+ it 'should hash' do
86
+ expect(payment_request.secure_hash).to eq hash
87
+ end
88
+ end
89
+ end
90
+
91
+ describe '#validate' do
92
+ describe 'no errors' do
93
+ it 'should not raise' do
94
+ expect { payment_request.validate }.not_to raise_error
95
+ end
96
+ end
97
+ describe 'errors' do
98
+ before :each do
99
+ payment_request.merchant_id = nil
100
+ end
101
+ it 'should raise on no merchant_id' do
102
+ expect { payment_request.validate }.to raise_error(Apruve::ValidationError, '["merchant_id must be set"]')
103
+ end
104
+ end
105
+ end
106
+
107
+ describe '#find' do
108
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
109
+ describe 'success' do
110
+ let! (:stubs) do
111
+ faraday_stubs do |stub|
112
+ stub.get("/api/v3/payment_requests/#{id}") { [200, {}, '{}'] }
113
+ end
114
+ end
115
+ it 'should do a get' do
116
+ Apruve::PaymentRequest.find(id)
117
+ stubs.verify_stubbed_calls
118
+ end
119
+ end
120
+
121
+ describe 'not found' do
122
+ let! (:stubs) do
123
+ faraday_stubs do |stub|
124
+ stub.get("/api/v3/payment_requests/#{id}") { [404, {}, 'Not Found'] }
125
+ end
126
+ end
127
+ it 'should raise' do
128
+ expect { Apruve::PaymentRequest.find(id) }.to raise_error(Apruve::NotFound)
129
+ stubs.verify_stubbed_calls
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::Payment do
4
+ before :each do
5
+ Apruve.configure('f5fbe71d68772d1f562ed6f598b995b3', 'local')
6
+ end
7
+
8
+ let (:amount_cents) { 12340 }
9
+ let (:notes) { 'notes from merchant' }
10
+ let (:payment_request_id) { '9999' }
11
+ let (:payment) do
12
+ Apruve::Payment.new(
13
+ payment_request_id: payment_request_id,
14
+ amount_cents: amount_cents,
15
+ )
16
+ end
17
+ subject { payment }
18
+
19
+ it { should respond_to(:id) }
20
+ it { should respond_to(:payment_request_id) }
21
+ it { should respond_to(:status) }
22
+ it { should respond_to(:amount_cents) }
23
+ it { should respond_to(:currency) }
24
+ it { should respond_to(:merchant_notes) }
25
+ it { should respond_to(:payment_items) }
26
+ it { should respond_to(:api_url) }
27
+ it { should respond_to(:view_url) }
28
+ it { should respond_to(:created_at) }
29
+ it { should respond_to(:updated_at) }
30
+
31
+ describe '#to_json' do
32
+ let(:expected) do
33
+ '{"payment_request_id":"9999","amount_cents":12340,"payment_items":[],"currency":"USD"}'
34
+ end
35
+ its(:to_json) { should eq expected }
36
+ end
37
+
38
+ describe '#validate' do
39
+ describe 'no errors' do
40
+ it 'should not raise' do
41
+ expect { payment.validate }.not_to raise_error
42
+ end
43
+ end
44
+ describe 'errors' do
45
+ before :each do
46
+ payment.amount_cents = nil
47
+ end
48
+ it 'should raise on no merchant_id' do
49
+ expect { payment.validate }.to raise_error(Apruve::ValidationError, '["amount_cents must be set"]')
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#find' do
55
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
56
+ let (:payment_request_id) { '8fdc91d337a28633deed058dd2d3fc90' }
57
+ describe 'success' do
58
+ let! (:stubs) do
59
+ faraday_stubs do |stub|
60
+ stub.get("/api/v3/payment_requests/#{payment_request_id}/payments/#{id}") { [200, {}, '{}'] }
61
+ end
62
+ end
63
+ it 'should do a get' do
64
+ Apruve::Payment.find(payment_request_id, id)
65
+ stubs.verify_stubbed_calls
66
+ end
67
+ end
68
+
69
+ describe 'not found' do
70
+ let! (:stubs) do
71
+ faraday_stubs do |stub|
72
+ stub.get("/api/v3/payment_requests/#{payment_request_id}/payments/#{id}") { [404, {}, 'Not Found'] }
73
+ end
74
+ end
75
+ it 'should raise' do
76
+ expect { Apruve::Payment.find(payment_request_id, id) }.to raise_error(Apruve::NotFound)
77
+ stubs.verify_stubbed_calls
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#save' do
83
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
84
+ let (:status) { 'pending' }
85
+ let (:api_url) { Faker::Internet.url }
86
+ let (:view_url) { Faker::Internet.url }
87
+ let (:response) do
88
+ {
89
+ id: id,
90
+ status: status,
91
+ api_url: api_url,
92
+ view_url: view_url
93
+ }
94
+ end
95
+ describe 'success' do
96
+ let! (:stubs) do
97
+ faraday_stubs do |stub|
98
+ stub.post(
99
+ "/api/v3/payment_requests/#{payment_request_id}/payments",
100
+ payment.to_json,
101
+ ) { [200, {}, response.to_json] }
102
+ end
103
+ end
104
+ it 'should do a post' do
105
+ payment.save!
106
+ expect(payment.id).to eq id
107
+ expect(payment.status).to eq status
108
+ expect(payment.api_url).to eq api_url
109
+ expect(payment.view_url).to eq view_url
110
+ stubs.verify_stubbed_calls
111
+ end
112
+ end
113
+
114
+ describe 'payment request not found' do
115
+ let! (:stubs) do
116
+ faraday_stubs do |stub|
117
+ stub.post(
118
+ "/api/v3/payment_requests/#{payment_request_id}/payments",
119
+ payment.to_json,
120
+ ) { [404, {}, 'Not Found'] }
121
+ end
122
+ end
123
+ it 'should raise' do
124
+ expect { payment.save! }.to raise_error(Apruve::NotFound)
125
+ stubs.verify_stubbed_calls
126
+ end
127
+ end
128
+ end
129
+ end