paid 1.0.1 → 1.0.2

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 (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
@@ -1,5 +0,0 @@
1
- module Paid
2
- class APISingleton < APIClass
3
- attribute :object
4
- end
5
- end
@@ -1,10 +0,0 @@
1
- module Paid
2
- class InvalidRequestError < PaidError
3
- attr_accessor :param
4
-
5
- def initialize(message, param, http_status=nil, http_body=nil, json_body=nil)
6
- super(message, http_status, http_body, json_body)
7
- @param = param
8
- end
9
- end
10
- end
@@ -1,69 +0,0 @@
1
- # Setup a fake resource for testing the APIResource
2
-
3
- class NestedResource < Paid::APIResource
4
- attribute :price
5
-
6
- def self.path
7
- "/nested_resources"
8
- end
9
- end
10
-
11
- class MockResource < Paid::APIResource
12
- attribute :name
13
- attribute :tarray
14
- attribute :thash
15
- attribute :nested, NestedResource
16
-
17
- api_class_method :retrieve, :get, ":path/:id", :arguments => [:id]
18
- api_class_method :all, :get, :constructor => Paid::APIList.constructor(MockResource)
19
- api_class_method :create, :post
20
- api_class_method :many_args_get, :get, ":path/:b/many", :arguments => [:a, :b, :c]
21
- api_class_method :many_args_post, :post, ":path/:b/many", :arguments => [:a, :b, :c]
22
- api_class_method :crazy_path, :get, ":crazy"
23
-
24
- api_class_method :with_con_self, :get, :constructor => :self
25
- api_class_method :with_con_class, :get, :constructor => MockResource
26
- api_class_method :with_con_lambda, :get, :constructor => lambda{ |json| "lamdba result" }
27
- api_class_method :with_con_default, :get
28
-
29
- def self.default_lambda
30
- lambda do |this|
31
- self.default_values
32
- end
33
- end
34
- api_class_method :with_lambda, :post, :default_params => self.default_lambda
35
- api_class_method :with_symbol, :post, :default_params => :default_values
36
- api_class_method :with_symbol_and_args, :post, :default_params => :default_values, :arguments => [:name]
37
-
38
- api_instance_method :refresh, :get, :constructor => :self
39
- api_instance_method :save, :put, :default_params => changed_lambda, :constructor => :self
40
- api_instance_method :delete, :delete
41
- api_instance_method :custom_path, :get, ":path", :arguments => [:path]
42
- api_instance_method :name_path, :get, ":name"
43
- api_instance_method :crazy_path, :get, ":crazy"
44
-
45
- api_instance_method :with_con_self, :get, :constructor => :self
46
- api_instance_method :with_con_class, :get, :constructor => MockResource
47
- api_instance_method :with_con_lambda, :get, :constructor => lambda{ |json| "lamdba result" }
48
- api_instance_method :with_con_default, :get
49
-
50
- api_instance_method :with_lambda, :put, :default_params => changed_lambda
51
- api_instance_method :with_symbol, :put, :default_params => :changed_attributes
52
- api_instance_method :with_symbol_and_args, :put, :default_params => :changed_attributes, :arguments => [:name]
53
-
54
- def self.path
55
- "/mocks"
56
- end
57
-
58
- def self.crazy
59
- "/crazy_path"
60
- end
61
-
62
- def self.default_values
63
- {
64
- :name => "default name",
65
- :tarray => [1,2,3]
66
- }
67
- end
68
-
69
- end
@@ -1,28 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- module Paid
4
- class ApiResourceTest < Test::Unit::TestCase
5
-
6
- should 'have an id attribute' do
7
- assert(Paid::APIResource.method_defined?(:id))
8
- assert(Paid::APIResource.method_defined?(:id=))
9
- end
10
-
11
- should 'have an object attribute' do
12
- assert(Paid::APIResource.method_defined?(:object))
13
- assert(Paid::APIResource.method_defined?(:object=))
14
- end
15
-
16
- should 'have a default path' do
17
- mr = MockResource.new('fake_id')
18
- assert_equal("#{MockResource.path}/fake_id", mr.path)
19
- end
20
-
21
- should 'raise an InvalidRequestError when no ID is present for instance path' do
22
- @mock.expects(:get).never
23
- c = MockResource.new
24
- assert_raises(Paid::InvalidRequestError) { c.refresh }
25
- end
26
-
27
- end
28
- end
@@ -1,12 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- module Paid
4
- class ApiSingletonTest < Test::Unit::TestCase
5
-
6
- should 'have an object attribute' do
7
- assert(Paid::APISingleton.method_defined?(:object))
8
- assert(Paid::APISingleton.method_defined?(:object=))
9
- end
10
-
11
- end
12
- end
@@ -1,50 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.expand_path('../../test_helper', __FILE__)
3
-
4
- module Paid
5
- class StatusCodesTest < Test::Unit::TestCase
6
-
7
- context 'AuthenticationError' do
8
- should 'be raised with no API credentials' do
9
- Paid.api_key = nil
10
- assert_raises(Paid::AuthenticationError) do
11
- MockResource.retrieve('fake_id')
12
- end
13
- end
14
-
15
- should 'be raised with invalid credentials' do
16
- Paid.api_key = 'invalid api key' # spaces aren't valid
17
- assert_raises(Paid::AuthenticationError) do
18
- MockResource.new('fake_id').refresh
19
- end
20
- end
21
-
22
- context 'that has been raised' do
23
- setup do
24
- Paid.api_key = 'invalid'
25
- response = test_response(test_invalid_api_key_error, 401)
26
- begin
27
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
28
- MockResource.retrieve('failing')
29
- rescue Paid::AuthenticationError => e
30
- @error = e
31
- end
32
- end
33
-
34
- should 'have an http status of 401' do
35
- assert_equal(401, @error.http_status)
36
- end
37
-
38
- should 'have an http body' do
39
- assert(!!@error.http_body)
40
- end
41
-
42
- should 'have a JSON body with an error message' do
43
- assert(!!@error.json_body[:error][:message])
44
- assert_equal(test_invalid_api_key_error[:error][:message], @error.json_body[:error][:message])
45
- end
46
- end
47
- end
48
-
49
- end
50
- end
@@ -1,63 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.expand_path('../../test_helper', __FILE__)
3
-
4
- module Paid
5
- class StatusCodesTest < Test::Unit::TestCase
6
-
7
- context 'InvalidRequestError' do
8
- should 'be raised when HTTP status code is 400' do
9
- response = test_response(test_missing_id_error, 400)
10
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
11
- begin
12
- MockResource.retrieve('bad_id')
13
- rescue Paid::InvalidRequestError => e
14
- assert_equal(400, e.http_status)
15
- assert(!!e.http_body)
16
- assert(e.json_body.is_a?(Hash))
17
- end
18
- end
19
-
20
- should 'be raised when HTTP status code is 404' do
21
- response = test_response(test_missing_id_error, 404)
22
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
23
- assert_raises
24
- begin
25
- MockResource.retrieve('foo')
26
- rescue Paid::InvalidRequestError => e
27
- rescued = true
28
- assert_equal(404, e.http_status)
29
- assert_equal(true, !!e.http_body)
30
- assert_equal(true, e.json_body.is_a?(Hash))
31
- end
32
- assert(rescued)
33
- end
34
- end
35
-
36
- context 'APIError' do
37
- should 'be raised when HTTP status code is 5XX' do
38
- response = test_response(test_api_error, 500)
39
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
40
-
41
- begin
42
- MockResource.new('fake_id').refresh
43
- rescue Paid::APIError => e
44
- rescued = true
45
- assert(e.is_a?(Paid::APIError))
46
- end
47
- assert(rescued)
48
- end
49
- end
50
-
51
- context 'AuthenticationError' do
52
- should 'be raised when HTTP status code is 401 (invalid credentials)' do
53
- Paid.api_key = 'invalid'
54
- response = test_response(test_invalid_api_key_error, 401)
55
- assert_raises(Paid::AuthenticationError) do
56
- @mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
57
- MockResource.retrieve('failing')
58
- end
59
- end
60
- end
61
-
62
- end
63
- end