paid 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/History.txt +6 -0
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/lib/paid.rb +32 -207
  6. data/lib/paid/account.rb +22 -10
  7. data/lib/paid/api_list.rb +56 -17
  8. data/lib/paid/api_method.rb +93 -0
  9. data/lib/paid/api_resource.rb +130 -8
  10. data/lib/paid/customer.rb +104 -40
  11. data/lib/paid/errors/api_connection_error.rb +1 -1
  12. data/lib/paid/errors/api_error.rb +25 -3
  13. data/lib/paid/errors/authentication_error.rb +1 -1
  14. data/lib/paid/errors/paid_error.rb +2 -9
  15. data/lib/paid/event.rb +28 -17
  16. data/lib/paid/event_data.rb +10 -0
  17. data/lib/paid/headers_builder.rb +75 -0
  18. data/lib/paid/invoice.rb +55 -16
  19. data/lib/paid/params_builder.rb +26 -0
  20. data/lib/paid/path_builder.rb +38 -0
  21. data/lib/paid/plan.rb +38 -13
  22. data/lib/paid/refund_list.rb +26 -0
  23. data/lib/paid/requester.rb +97 -0
  24. data/lib/paid/subscription.rb +47 -16
  25. data/lib/paid/transaction.rb +64 -16
  26. data/lib/paid/util.rb +14 -40
  27. data/paid.gemspec +1 -1
  28. data/test/paid/{api_class_test.rb → _api_resource_test.rb} +31 -17
  29. data/test/paid/account_test.rb +3 -3
  30. data/test/paid/api_list_test.rb +14 -8
  31. data/test/paid/api_method_test.rb +89 -0
  32. data/test/paid/customer_test.rb +20 -10
  33. data/test/paid/event_test.rb +3 -4
  34. data/test/paid/headers_builder_test.rb +39 -0
  35. data/test/paid/invoice_test.rb +3 -3
  36. data/test/paid/params_builder_test.rb +57 -0
  37. data/test/paid/path_builder_test.rb +67 -0
  38. data/test/paid/plan_test.rb +3 -3
  39. data/test/paid/requester_test.rb +86 -0
  40. data/test/paid/subscription_test.rb +3 -3
  41. data/test/paid/transaction_test.rb +4 -4
  42. data/test/paid/util_test.rb +36 -35
  43. data/test/test_data.rb +9 -2
  44. data/test/test_helper.rb +14 -14
  45. metadata +23 -19
  46. data/lib/paid/api_class.rb +0 -338
  47. data/lib/paid/api_singleton.rb +0 -5
  48. data/lib/paid/errors/invalid_request_error.rb +0 -10
  49. data/test/mock_resource.rb +0 -69
  50. data/test/paid/api_resource_test.rb +0 -28
  51. data/test/paid/api_singleton_test.rb +0 -12
  52. data/test/paid/authentication_test.rb +0 -50
  53. data/test/paid/status_codes_test.rb +0 -63
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Paid
4
+ class HeadersBuilderTest < ::Test::Unit::TestCase
5
+
6
+ setup do
7
+ @headers = {
8
+ :dog => "dog-value"
9
+ }
10
+ @api_key = "test-api-key"
11
+ @built_headers = HeadersBuilder.build(@headers, @api_key)
12
+ end
13
+
14
+ should 'set the content_type' do
15
+ assert(@built_headers.has_key?(:content_type))
16
+ assert_equal('application/x-www-form-urlencoded', @built_headers[:content_type])
17
+ end
18
+
19
+ should 'set the user_agent' do
20
+ assert(@built_headers.has_key?(:user_agent))
21
+ assert(@built_headers[:user_agent].include?(Paid::VERSION))
22
+ assert(@built_headers[:user_agent].include?(Paid.api_version))
23
+ end
24
+
25
+ should 'set the basic auth header' do
26
+ assert(@built_headers.has_key?("Authorization"))
27
+ encoded_api_key = Base64.encode64("#{@api_key}:")
28
+ assert(@built_headers["Authorization"].include?(encoded_api_key))
29
+ end
30
+
31
+ should 'set the custom auth header' do
32
+ auth_key = "CLIENT-TOKEN"
33
+ header = HeadersBuilder.build(@headers, @api_key, auth_key)
34
+ assert(header.has_key?(auth_key))
35
+ assert_equal(@api_key, header[auth_key])
36
+ end
37
+
38
+ end
39
+ end
@@ -3,7 +3,7 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Paid
4
4
  class InvoiceTest < Test::Unit::TestCase
5
5
  setup do
6
- @invoice_url = "#{Paid.api_base}/v0/invoices"
6
+ @invoice_url = "#{Paid.api_base}/invoices"
7
7
  end
8
8
 
9
9
  context 'Invoice class' do
@@ -106,8 +106,8 @@ module Paid
106
106
  end
107
107
 
108
108
  should 'be registered' do
109
- assert(APIClass.subclasses.include?(Paid::Invoice))
110
- assert_equal(Paid::Invoice, APIClass.subclass_fetch("invoice"))
109
+ assert(APIResource.api_subclasses.include?(Paid::Invoice))
110
+ assert_equal(Paid::Invoice, APIResource.api_subclass_fetch("invoice"))
111
111
  end
112
112
 
113
113
  end
@@ -0,0 +1,57 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Paid
4
+ class ParamsBuilderTest < ::Test::Unit::TestCase
5
+
6
+ setup do
7
+ @params = {
8
+ :dog => "dog-value",
9
+ "string" => "str-value"
10
+ }
11
+ end
12
+
13
+ context '#clean' do
14
+ setup do
15
+ @built_params = ParamsBuilder.clean(@params)
16
+ end
17
+
18
+ should 'convert keys to symbols' do
19
+ assert(@built_params.has_key?(:string))
20
+ assert_equal(@params["string"], @built_params[:string])
21
+ end
22
+
23
+ should 'not have any string keys' do
24
+ @built_params.each do |k, v|
25
+ assert(k.is_a?(Symbol))
26
+ end
27
+ end
28
+ end
29
+
30
+ context '#merge' do
31
+ setup do
32
+ @to_merge = {
33
+ :string => "other-str-value",
34
+ "cat" => "cat-value"
35
+ }
36
+ @built_params = ParamsBuilder.merge(@params, @to_merge)
37
+ end
38
+
39
+ should 'convert keys to symbols' do
40
+ assert(@built_params.has_key?(:cat))
41
+ assert(@built_params.has_key?(:string))
42
+ assert(!@built_params.has_key?("string"))
43
+ end
44
+
45
+ should 'merge in all values' do
46
+ assert_equal(@params[:dog], @built_params[:dog])
47
+ assert_equal(@to_merge[:string], @built_params[:string])
48
+ assert_equal(@to_merge["cat"], @built_params[:cat])
49
+ end
50
+
51
+ should 'prioritize values in @to_merge' do
52
+ assert_equal(@to_merge[:string], @built_params[:string])
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Paid
4
+ class PathBuilderTest < ::Test::Unit::TestCase
5
+ class FakeClass < APIResource
6
+ attr_reader :id
7
+ def initialize(id); @id = id; end
8
+ def abc; return "abc-value"; end
9
+ def self.xyz; return "xyz-value"; end
10
+ end
11
+
12
+ setup do
13
+ @params = {
14
+ :dog => "dog-value"
15
+ }
16
+ @obj = FakeClass.new("fake-id")
17
+ end
18
+
19
+ should 'use instance methods' do
20
+ path = "/a/:abc/123"
21
+ expected = "/a/abc-value/123"
22
+
23
+ actual = PathBuilder.build(path, @obj, nil)
24
+ assert_equal(expected, actual)
25
+ end
26
+
27
+ should 'use instance attributes' do
28
+ path = "/a/:id/123"
29
+ expected = "/a/fake-id/123"
30
+ actual = PathBuilder.build(path, @obj, nil)
31
+ assert_equal(expected, actual)
32
+ end
33
+
34
+ should 'use class methods' do
35
+ path = "/a/:xyz/123"
36
+ expected = "/a/xyz-value/123"
37
+
38
+ actual = PathBuilder.build(path, FakeClass, nil)
39
+ assert_equal(expected, actual)
40
+ end
41
+
42
+ should 'not use class #id methods (1.8.7)' do
43
+ path = "/a/:id/123"
44
+ expected = "/a/param-id/123"
45
+
46
+ actual = PathBuilder.build(path, FakeClass, { :id => "param-id" })
47
+ assert_equal(expected, actual)
48
+ end
49
+
50
+ should 'use param values' do
51
+ path = "/a/:dog/123"
52
+ expected = "/a/dog-value/123"
53
+
54
+ actual = PathBuilder.build(path, nil, @params)
55
+ assert_equal(expected, actual)
56
+ end
57
+
58
+ should 'use both methods and params' do
59
+ path = "/a/:dog/:abc/123"
60
+ expected = "/a/dog-value/abc-value/123"
61
+
62
+ actual = PathBuilder.build(path, @obj, @params)
63
+ assert_equal(expected, actual)
64
+ end
65
+
66
+ end
67
+ end
@@ -3,7 +3,7 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Paid
4
4
  class PlanTest < Test::Unit::TestCase
5
5
  setup do
6
- @plan_url = "#{Paid.api_base}/v0/plans"
6
+ @plan_url = "#{Paid.api_base}/plans"
7
7
  end
8
8
 
9
9
  context 'Plan class' do
@@ -84,8 +84,8 @@ module Paid
84
84
  end
85
85
 
86
86
  should 'be registered' do
87
- assert(APIClass.subclasses.include?(Paid::Plan))
88
- assert_equal(Paid::Plan, APIClass.subclass_fetch("plan"))
87
+ assert(APIResource.api_subclasses.include?(Paid::Plan))
88
+ assert_equal(Paid::Plan, APIResource.api_subclass_fetch("plan"))
89
89
  end
90
90
 
91
91
 
@@ -0,0 +1,86 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Paid
4
+ class RequesterTest < ::Test::Unit::TestCase
5
+ setup do
6
+ end
7
+
8
+ context '#prepare_params' do
9
+ setup do
10
+ @url = "test_url"
11
+ @params = { :a => 1, :b => [2, 3] }
12
+ end
13
+
14
+ should 'convert :get params to a query string' do
15
+ url, params = Requester.prepare_params(@url, @params, :get)
16
+ assert(url != @url)
17
+ assert(params.nil?)
18
+ end
19
+
20
+ should 'convert :delete params to a query string' do
21
+ url, params = Requester.prepare_params(@url, @params, :delete)
22
+ assert(url != @url)
23
+ assert(params.nil?)
24
+ end
25
+
26
+ should 'convert :head params to a query string' do
27
+ url, params = Requester.prepare_params(@url, @params, :head)
28
+ assert(url != @url)
29
+ assert(params.nil?)
30
+ end
31
+
32
+ context 'method = :post' do
33
+ setup do
34
+ @method = :post
35
+ end
36
+
37
+ should 'not convert params to a query string if a file is in them' do
38
+ expected = { :file => File.new(__FILE__) }
39
+ url, params = Requester.prepare_params(@url, expected, @method)
40
+ assert_equal(expected, params)
41
+ end
42
+
43
+ should 'convert params to a query string if a file is not present' do
44
+ url, params = Requester.prepare_params(@url, @params, @method)
45
+ assert(params.is_a?(String))
46
+ end
47
+ end
48
+ end
49
+
50
+ context '#query_string' do
51
+ should 'join #query_array results with an "&"' do
52
+ start = { :a => 1, :b => [2, 3] }
53
+ expected = ["a=1", "b[]=2", "b[]=3"]
54
+
55
+ actual = Requester.query_string(start).split('&')
56
+ actual.each do |str|
57
+ assert(expected.include?(str))
58
+ end
59
+ end
60
+ end
61
+
62
+ context '#query_array' do
63
+ should 'convert { :a => "value" } to []' do
64
+ start = { :a => "value" }
65
+ finish = ["a=value"]
66
+
67
+ assert_equal(finish, Requester.query_array(start))
68
+ end
69
+
70
+ should 'convert { :a => { :b => { :c => "cvalue" } } } to ["a[b][c]=cvalue"]' do
71
+ start = { :a => { :b => { :c => "cvalue" } } }
72
+ finish = ["a[b][c]=cvalue"]
73
+
74
+ assert_equal(finish, Requester.query_array(start))
75
+ end
76
+
77
+ should 'convert { :a => [1, 2] } to ["a[]=1", "a[]=2"]' do
78
+ start = { :a => [1, 2] }
79
+ finish = ["a[]=1", "a[]=2"]
80
+
81
+ assert_equal(finish, Requester.query_array(start))
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -3,7 +3,7 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Paid
4
4
  class SubscriptionTest < Test::Unit::TestCase
5
5
  setup do
6
- @subscription_url = "#{Paid.api_base}/v0/subscriptions"
6
+ @subscription_url = "#{Paid.api_base}/subscriptions"
7
7
  end
8
8
 
9
9
  context 'Subscription class' do
@@ -103,8 +103,8 @@ module Paid
103
103
  end
104
104
 
105
105
  should 'be registered' do
106
- assert(APIClass.subclasses.include?(Paid::Subscription))
107
- assert_equal(Paid::Subscription, APIClass.subclass_fetch("subscription"))
106
+ assert(APIResource.api_subclasses.include?(Paid::Subscription))
107
+ assert_equal(Paid::Subscription, APIResource.api_subclass_fetch("subscription"))
108
108
  end
109
109
 
110
110
  end
@@ -3,7 +3,7 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  module Paid
4
4
  class TransactionTest < Test::Unit::TestCase
5
5
  setup do
6
- @transaction_url = "#{Paid.api_base}/v0/transactions"
6
+ @transaction_url = "#{Paid.api_base}/transactions"
7
7
  end
8
8
 
9
9
  context 'Transaction class' do
@@ -49,7 +49,7 @@ module Paid
49
49
  transaction.description = "new description"
50
50
 
51
51
  @mock.expects(:put).once.with do |url, headers, params|
52
- params == transaction.changed_attributes && url == "#{@transaction_url}/#{transaction.id}"
52
+ params == transaction.changed_api_attributes && url == "#{@transaction_url}/#{transaction.id}"
53
53
  end.returns(test_response(test_transaction))
54
54
 
55
55
  # This should update this instance with test_transaction since it was returned
@@ -115,8 +115,8 @@ module Paid
115
115
  end
116
116
 
117
117
  should 'be registered' do
118
- assert(APIClass.subclasses.include?(Paid::Transaction))
119
- assert_equal(Paid::Transaction, APIClass.subclass_fetch("transaction"))
118
+ assert(APIResource.api_subclasses.include?(Paid::Transaction))
119
+ assert_equal(Paid::Transaction, APIResource.api_subclass_fetch("transaction"))
120
120
  end
121
121
 
122
122
 
@@ -1,50 +1,51 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Paid
4
- class UtilTest < Test::Unit::TestCase
5
- should "symbolize_keys should convert keys to symbols" do
6
- start = {
7
- 'foo' => 'bar',
8
- 'array' => [{ 'foo' => 'bar' }],
9
- 'nested' => {
10
- 1 => 2,
11
- :symbol => 9,
12
- 'string' => nil
4
+ class UtilTest < ::Test::Unit::TestCase
5
+ context '#symbolize_keys' do
6
+ should "convert keys to symbols" do
7
+ start = {
8
+ 'foo' => 'bar',
9
+ 'array' => [{ 'foo' => 'bar' }],
10
+ 'nested' => {
11
+ 1 => 2,
12
+ :symbol => 9,
13
+ 'string' => nil
14
+ }
13
15
  }
14
- }
15
- finish = {
16
- :foo => 'bar',
17
- :array => [{ :foo => 'bar' }],
18
- :nested => {
19
- 1 => 2,
20
- :symbol => 9,
21
- :string => nil
16
+ finish = {
17
+ :foo => 'bar',
18
+ :array => [{ :foo => 'bar' }],
19
+ :nested => {
20
+ 1 => 2,
21
+ :symbol => 9,
22
+ :string => nil
23
+ }
22
24
  }
23
- }
24
25
 
25
- symbolized = Paid::Util.symbolize_keys(start)
26
- assert_equal(finish, symbolized)
26
+ symbolized = Util.symbolize_keys(start)
27
+ assert_equal(finish, symbolized)
28
+ end
27
29
  end
28
30
 
29
- should 'query_array should convert { :a => "value" } to []' do
30
- start = { :a => "value" }
31
- finish = ["a=value"]
31
+ context '#sorta_deep_clone' do
32
+ # Super hand wavy test.. but it works for now so whatever.
33
+ should 'clone well enough that we dont accidentally alter json' do
34
+ start = { :a => "abc", :b => [ { :c => "c-1" }, { :c => "c-2" } ] }
35
+ cloned = Util.sorta_deep_clone(start)
32
36
 
33
- assert_equal(finish, Paid::Util.query_array(start))
34
- end
35
-
36
- should 'query_array should convert { :a => { :b => { :c => "cvalue" } } } to ["a[b][c]=cvalue"]' do
37
- start = { :a => { :b => { :c => "cvalue" } } }
38
- finish = ["a[b][c]=cvalue"]
37
+ cloned[:a] = "123"
38
+ cloned[:b] << { :c => "c-3" }
39
+ cloned[:b][0][:c] = "c-one"
39
40
 
40
- assert_equal(finish, Paid::Util.query_array(start))
41
+ assert_equal({ :a => "abc", :b => [ { :c => "c-1" }, { :c => "c-2" } ] }, start)
42
+ end
41
43
  end
42
44
 
43
- should 'query_array should convert { :a => [1, 2] } to ["a[]=1", "a[]=2"]' do
44
- start = { :a => [1, 2] }
45
- finish = ["a[]=1", "a[]=2"]
46
-
47
- assert_equal(finish, Paid::Util.query_array(start))
45
+ context '#constantize' do
46
+ should 'convert :APIResource to the class object' do
47
+ assert_equal(APIResource, Util.constantize(:APIResource))
48
+ end
48
49
  end
49
50
  end
50
51
  end
data/test/test_data.rb CHANGED
@@ -14,7 +14,6 @@ module Paid
14
14
 
15
15
  def test_mock_resource
16
16
  {
17
- :id => 'test_mock_resource_id',
18
17
  :object => 'mock_resource',
19
18
  :name => 'test mr name',
20
19
  :nested => {
@@ -22,8 +21,14 @@ module Paid
22
21
  :object => 'nested_resource',
23
22
  :price => 500
24
23
  },
24
+ :nested_alt_id => 'nested_alt_id',
25
+ :nested_with => {
26
+ :id => 'nested_with_id',
27
+ :price => 500
28
+ },
25
29
  :thash => { :some_key => "some value" },
26
- :tarray => ["abc", "xyz"]
30
+ :tarray => ["abc", "xyz"],
31
+ :id => 'test_mock_resource_id'
27
32
  }
28
33
  end
29
34
 
@@ -34,6 +39,8 @@ module Paid
34
39
  }
35
40
  end
36
41
 
42
+ # Resources
43
+
37
44
  def test_account
38
45
  {
39
46
  :id => 'account_id',