jsmestad-chargify 0.3.0.pre5 → 0.3.0.pre6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0.pre5
1
+ 0.3.0.pre6
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jsmestad-chargify}
8
- s.version = "0.3.0.pre5"
8
+ s.version = "0.3.0.pre6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wynn Netherland", "Justin Smestad"]
data/lib/chargify/base.rb CHANGED
@@ -26,19 +26,20 @@ module Chargify
26
26
  raise(Chargify::Error::ConnectionFailed.new, "Failed to connect to payment gateway.")
27
27
  end
28
28
 
29
- if raise_errors
30
- case response.code.to_i
31
- when 401
32
- raise(Chargify::Error::AccessDenied.new(response), response.body)
33
- when 403
34
- raise(Chargify::Error::Forbidden.new(response), response.body)
35
- when 422
36
- raise(Chargify::Error::BadRequest.new(response), response.body)
37
- when 404
38
- raise(Chargify::Error::NotFound.new(response), response.body)
39
- when 504
40
- raise(Chargify::Error::GatewayTimeout.new(response), response.body)
41
- end
29
+
30
+ case response.code.to_i
31
+ when 401
32
+ raise(Chargify::Error::AccessDenied.new(response), response.body)
33
+ when 403
34
+ raise(Chargify::Error::Forbidden.new(response), response.body)
35
+ when 422
36
+ raise(Chargify::Error::BadRequest.new(response), response.body)
37
+ when 404
38
+ raise(Chargify::Error::NotFound.new(response), response.body)
39
+ when 500
40
+ raise(Chargify::Error::ServerError.new(response), response.body)
41
+ when 504
42
+ raise(Chargify::Error::GatewayTimeout.new(response), response.body)
42
43
  end
43
44
 
44
45
  Chargify::Config.logger.debug("[CHARGIFY] Response from #{self.base_uri}#{path} was #{response.code}: #{response.body}") if Chargify::Config.debug
@@ -9,13 +9,18 @@ module Chargify
9
9
  customers.map{|c| Hashie::Mash.new c['customer']}
10
10
  end
11
11
 
12
- def find(id)
12
+ def find!(id)
13
13
  return all if id == :all
14
14
 
15
15
  request = api_request(:get, "/customers/#{id}.json")
16
- success = request.code == 200
17
- response = Hashie::Mash.new(request).customer if success
18
- Hashie::Mash.new(response || {}).update(:success? => success)
16
+ response = Hashie::Mash.new(request)
17
+ response
18
+ end
19
+
20
+ def find(id)
21
+ find!(id)
22
+ rescue Chargify::Error::Base => e
23
+ return nil
19
24
  end
20
25
 
21
26
  # def find!(id)
@@ -23,11 +28,16 @@ module Chargify
23
28
  # response = Hashie::Mash.new(request).customer
24
29
  # end
25
30
 
26
- def lookup(reference_id)
31
+ def lookup!(reference_id)
27
32
  request = api_request(:get, "/customers/lookup.json?reference=#{reference_id}")
28
- success = request.code == 200
29
- response = Hashie::Mash.new(request).customer if success
30
- Hashie::Mash.new(response || {}).update(:success? => success)
33
+ response = Hashie::Mash.new(request)
34
+ response.customer
35
+ end
36
+
37
+ def lookup(reference_id)
38
+ lookup!(reference_id)
39
+ rescue Chargify::Error::Base => e
40
+ return nil
31
41
  end
32
42
 
33
43
  #
@@ -37,12 +47,18 @@ module Chargify
37
47
  # * organization (Optional) Company/Organization name
38
48
  # * reference (Optional, but encouraged) The unique identifier used within your own application for this customer
39
49
  #
40
- def create(info={})
50
+ def create!(info={})
41
51
  result = api_request(:post, "/customers.json", :body => {:customer => info})
42
52
  created = true if result.code == 201
43
53
  response = Hashie::Mash.new(result)
44
54
  (response.customer || response).update(:success? => created)
45
55
  end
56
+
57
+ def create(info={})
58
+ create!(info)
59
+ rescue Chargify::Error::Base => e
60
+ return false
61
+ end
46
62
 
47
63
  #
48
64
  # * first_name (Required)
@@ -51,7 +67,7 @@ module Chargify
51
67
  # * organization (Optional) Company/Organization name
52
68
  # * reference (Optional, but encouraged) The unique identifier used within your own application for this customer
53
69
  #
54
- def update(info={})
70
+ def update!(info={})
55
71
  info.stringify_keys!
56
72
  chargify_id = info.delete('id')
57
73
  result = api_request(:put, "/customers/#{chargify_id}.json", :body => {:customer => info})
@@ -60,6 +76,12 @@ module Chargify
60
76
  return response.customer unless response.customer.to_a.empty?
61
77
  response
62
78
  end
79
+
80
+ def update(info={})
81
+ update!(info)
82
+ rescue Chargify::Error::Base => e
83
+ return false
84
+ end
63
85
 
64
86
  def subscriptions(id)
65
87
  subscriptions = api_request(:get, "/customers/#{id}/subscriptions.json")
@@ -13,6 +13,7 @@ module Chargify
13
13
  class Forbidden < Base; end # 403 errors
14
14
  class BadRequest < Base; end # 422 errors
15
15
  class NotFound < Base; end # 404 errors
16
+ class ServerError < Base; end # 500 errors
16
17
  class GatewayTimeout < Base; end # 504 errors
17
18
  class ConnectionFailed < Base; end
18
19
 
@@ -8,14 +8,26 @@ module Chargify
8
8
  result.map{|p| Hashie::Mash.new p['product']}
9
9
  end
10
10
 
11
- def find(id)
11
+ def find!(id)
12
12
  return all if id == :all
13
13
 
14
14
  result = api_request(:get, "/products/#{id}.json")
15
15
  Hashie::Mash.new(result).product
16
16
  end
17
-
17
+
18
+ def find(id)
19
+ find!(id)
20
+ rescue Chargify::Error::Base => e
21
+ return nil
22
+ end
23
+
18
24
  def find_by_handle(handle)
25
+ find_by_handle!(handle)
26
+ rescue Chargify::Error::Base => e
27
+ return nil
28
+ end
29
+
30
+ def find_by_handle!(handle)
19
31
  result = api_request(:get, "/products/handle/#{handle}.json")
20
32
  Hashie::Mash.new(result).product
21
33
  end
@@ -3,88 +3,142 @@ module Chargify
3
3
 
4
4
  class << self
5
5
 
6
- def find(id)
6
+ def find!(id)
7
7
  result = api_request(:get, "/subscriptions/#{id}.json")
8
- return nil if result.code != 200
9
8
  Hashie::Mash.new(result).subscription
10
9
  end
10
+
11
+ def find(id)
12
+ find!(id)
13
+ rescue Chargify::Error::Base => e
14
+ return nil
15
+ end
11
16
 
12
- # Returns all elements outputted by Chargify plus:
13
- # response.success? -> true if response code is 201, false otherwise
14
- def create(subscription_attributes={})
17
+ def create!(subscription_attributes={})
15
18
  result = api_request(:post, "/subscriptions.json", :body => {:subscription => subscription_attributes})
16
- created = true if result.code == 201
17
19
  response = Hashie::Mash.new(result)
18
- (response.subscription || response).update(:success? => created)
20
+ response.subscription
21
+ end
22
+
23
+ def create(subscription_attributes={})
24
+ create!(subscription_attributes)
25
+ rescue Chargify::Error::Base => e
26
+ return false
19
27
  end
20
28
 
21
- # Returns all elements outputted by Chargify plus:
22
- # response.success? -> true if response code is 200, false otherwise
23
- def update(sub_id, subscription_attributes = {})
29
+ def update!(sub_id, subscription_attributes={})
24
30
  result = api_request(:put, "/subscriptions/#{sub_id}.json", :body => {:subscription => subscription_attributes})
25
- updated = true if result.code == 200
26
31
  response = Hashie::Mash.new(result)
27
- (response.subscription || response).update(:success? => updated)
32
+ response.subscription
33
+ end
34
+
35
+ def update(sub_id, subscription_attributes={})
36
+ update!(sub_id, subscription_attributes)
37
+ rescue Chargify::Error::Base => e
38
+ return false
28
39
  end
29
40
 
30
- # Returns all elements outputted by Chargify plus:
31
- # response.success? -> true if response code is 200, false otherwise
32
- def cancel(sub_id, message="")
41
+ def cancel!(sub_id, message="")
33
42
  result = api_request(:delete, "/subscriptions/#{sub_id}.json", :body => {:subscription => {:cancellation_message => message} })
34
- deleted = true if result.code == 200
35
- response = Hashie::Mash.new(result)
36
- (response.subscription || response).update(:success? => deleted)
43
+ true if result.code == 200
44
+ end
45
+
46
+ def cancel(sub_id, message="")
47
+ cancel!(sub_id, message)
48
+ rescue Chargify::Error::Base => e
49
+ return false
37
50
  end
38
51
 
39
- def reactivate(sub_id)
52
+ def reactivate!(sub_id)
40
53
  result = api_request(:put, "/subscriptions/#{sub_id}/reactivate.json", :body => "")
41
- reactivated = true if result.code == 200
42
- response = Hashie::Mash.new(result) rescue Hashie::Mash.new
43
- (response.subscription || response).update(:success? => reactivated)
54
+ response = Hashie::Mash.new(result)
55
+ response.subscription
56
+ end
57
+
58
+ def reactivate(sub_id)
59
+ reactivate!(sub_id)
60
+ rescue Chargify::Error::Base => e
61
+ return false
44
62
  end
45
63
 
46
- def charge(sub_id, subscription_attributes={})
64
+ def charge!(sub_id, subscription_attributes={})
47
65
  result = api_request(:post, "/subscriptions/#{sub_id}/charges.json", :body => { :charge => subscription_attributes })
48
- success = result.code == 201
49
- result = {} if result.code == 404
50
-
51
66
  response = Hashie::Mash.new(result)
52
- (response.charge || response).update(:success? => success)
67
+ response.charge
68
+ end
69
+
70
+ def charge(sub_id, subscription_attributes={})
71
+ charge!(sub_id, subscription_attributes)
72
+ rescue Chargify::Error::Base => e
73
+ return false
53
74
  end
54
75
 
55
- def migrate(sub_id, product_id)
76
+ def migrate!(sub_id, product_id)
56
77
  result = api_request(:post, "/subscriptions/#{sub_id}/migrations.json", :body => {:product_id => product_id })
57
- success = true if result.code == 200
58
78
  response = Hashie::Mash.new(result)
59
- (response.subscription || {}).update(:success? => success)
79
+ response.subscription
80
+ end
81
+
82
+ def migrate(sub_id, product_id)
83
+ migrate!(sub_id, product_id)
84
+ rescue Chargify::Error::Base => e
85
+ return false
60
86
  end
61
87
 
62
- def transactions(sub_id, options={})
88
+ def transactions!(sub_id, options={})
63
89
  result = api_request(:get, "/subscriptions/#{sub_id}/transactions.json", :query => options)
64
90
  result.map{|t| Hashie::Mash.new t['transaction']}
65
91
  end
92
+
93
+ def transactions(sub_id, options={})
94
+ transactions!(sub_id, options)
95
+ rescue Chargify::Error::Base => e
96
+ return false
97
+ end
66
98
 
67
- def components(subscription_id)
99
+ def components!(subscription_id)
68
100
  result = api_request(:get, "/subscriptions/#{subscription_id}/components.json")
69
101
  result.map{|c| Hashie::Mash.new c['component']}
70
102
  end
103
+
104
+ def components(subscription_id)
105
+ components!(subscription_id)
106
+ rescue Chargify::Error::Base => e
107
+ return false
108
+ end
71
109
 
72
- def find_component(subscription_id, component_id)
73
- result = get("/subscriptions/#{subscription_id}/components/#{component_id}.json")
110
+ def find_component!(subscription_id, component_id)
111
+ result = api_request(:get, "/subscriptions/#{subscription_id}/components/#{component_id}.json")
74
112
  Hashie::Mash.new(result).component
75
113
  end
114
+
115
+ def find_component(subscription_id, component_id)
116
+ find_component!(subscription_id, component_id)
117
+ rescue Chargify::Error::Base => e
118
+ return false
119
+ end
76
120
 
77
- def update_component(subscription_id, component_id, quantity)
121
+ def update_component!(subscription_id, component_id, quantity)
78
122
  result = api_request(:put, "/subscriptions/#{subscription_id}/components/#{component_id}.json", :body => {:component => {:allocated_quantity => quantity}})
79
- result[:success?] = result.code == 200
80
123
  Hashie::Mash.new(result)
81
124
  end
125
+
126
+ def update_component(subscription_id, component_id, quantity)
127
+ update_component!(subscription_id, component_id, quantity)
128
+ rescue Chargify::Error::Base => e
129
+ return false
130
+ end
82
131
 
83
- def component_usage(subscription_id, component_id)
132
+ def component_usage!(subscription_id, component_id)
84
133
  result = api_request(:get, "/subscriptions/#{subscription_id}/components/#{component_id}/usages.json")
85
- success = result.code == 200
86
134
  response = Hashie::Mash.new(result)
87
- response.update(:success? => success)
135
+ response
136
+ end
137
+
138
+ def component_usage(subscription_id, component_id)
139
+ component_usage!(subscription_id, component_id)
140
+ rescue Chargify::Error::Base => e
141
+ return false
88
142
  end
89
143
 
90
144
  end
@@ -9,37 +9,59 @@ describe Chargify::Customer do
9
9
  end
10
10
  end
11
11
 
12
- describe '.find' do
12
+ describe '.find!' do
13
13
 
14
14
  it 'should pass to Chargified::Customer.all' do
15
15
  Chargify::Customer.should_receive(:all)
16
- Chargify::Customer.find(:all)
16
+ Chargify::Customer.find!(:all)
17
17
  end
18
18
 
19
19
  it "should be able to be found by a <chargify_id>" do
20
20
  stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "customer.json"
21
- customer = Chargify::Customer.find(16)
22
- customer.success?.should == true
21
+ customer = Chargify::Customer.find!(16)
22
+ customer.should be_a(Hashie::Mash)
23
23
  end
24
24
 
25
- it "should return an empty Hash with success? set to false" do
25
+ it "should raise an error if nothing is found" do
26
26
  stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
27
- customer = Chargify::Customer.find(16)
28
- customer.success?.should == false
27
+ lambda {
28
+ Chargify::Customer.find!(16)
29
+ }.should raise_error(Chargify::Error::NotFound)
29
30
  end
30
31
 
31
32
  end
33
+
34
+ describe '.find' do
32
35
 
36
+ it "should return nil if nothing is found" do
37
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
38
+ Chargify::Customer.find(16).should == nil
39
+ end
33
40
 
34
- describe '.lookup' do
41
+ end
35
42
 
36
- before do
43
+
44
+ describe '.lookup!' do
45
+
46
+ it "should be able to be found by a <reference_id>" do
37
47
  stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "customer.json"
48
+ customer = Chargify::Customer.lookup!("bradleyjoyce")
49
+ customer.should be_a(Hashie::Mash)
38
50
  end
39
51
 
40
- it "should be able to be found by a <reference_id>" do
41
- customer = Chargify::Customer.lookup("bradleyjoyce")
42
- customer.success?.should == true
52
+ it "should raise an error if nothing is found" do
53
+ stub_get "https://OU812:x@pengwynn.chargify.com/customers/lookup.json?reference=bradleyjoyce", "", 404
54
+ lambda {
55
+ Chargify::Customer.lookup!("bradleyjoyce")
56
+ }.should raise_error(Chargify::Error::NotFound)
57
+ end
58
+
59
+ end
60
+
61
+ describe '.lookup' do
62
+
63
+ it 'should return nil if nothing is found' do
64
+ Chargify::Customer.lookup("bradleyjoyce") == nil
43
65
  end
44
66
 
45
67
  end
@@ -60,42 +82,68 @@ describe Chargify::Customer do
60
82
 
61
83
  end
62
84
 
63
- describe '.create' do
64
-
65
- before do
66
- stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "new_customer.json"
67
- end
85
+ describe '.create!' do
68
86
 
69
87
  it "should create a new customer" do
88
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "new_customer.json"
70
89
  info = {
71
90
  :first_name => "Wynn",
72
91
  :last_name => "Netherland",
73
92
  :email => "wynn@example.com"
74
93
  }
75
- customer = Chargify::Customer.create(info)
94
+ customer = Chargify::Customer.create!(info)
76
95
  customer.first_name.should == "Wynn"
77
96
  end
97
+
98
+ it "should raise an error if customer creation fails" do
99
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "", 422
100
+ lambda {
101
+ Chargify::Customer.create!
102
+ }.should raise_error(Chargify::Error::BadRequest)
103
+ end
78
104
 
79
105
  end
80
-
81
- describe '.update' do
82
-
83
- before do
84
- stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "new_customer.json"
106
+
107
+ describe '.create' do
108
+
109
+ it "should return false if creation fails" do
110
+ stub_post "https://OU812:x@pengwynn.chargify.com/customers.json", "", 422
111
+ Chargify::Customer.create.should == false
85
112
  end
113
+
114
+ end
115
+
116
+ describe '.update!' do
86
117
 
87
118
  it "should update a customer" do
119
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "new_customer.json"
88
120
  info = {
89
121
  :id => 16,
90
122
  :first_name => "Wynn",
91
123
  :last_name => "Netherland",
92
124
  :email => "wynn@example.com"
93
125
  }
94
- customer = Chargify::Customer.update(info)
126
+ customer = Chargify::Customer.update!(info)
95
127
  customer.first_name.should == "Wynn"
96
128
  end
129
+
130
+ it "should raise an error if the customer cannot be found" do
131
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
132
+ lambda {
133
+ Chargify::Customer.update!(:id => 16)
134
+ }.should raise_error(Chargify::Error::NotFound)
135
+ end
97
136
 
98
137
  end
138
+
139
+ describe '.update' do
140
+
141
+ it "should return false if the customer cannot be found" do
142
+ stub_put "https://OU812:x@pengwynn.chargify.com/customers/16.json", "", 404
143
+ Chargify::Customer.update(:id => 16).should == false
144
+ end
145
+
146
+ end
99
147
 
100
148
  describe '.subscriptions' do
101
149
 
@@ -19,6 +19,11 @@ describe Chargify::Product do
19
19
  product = Chargify::Product.find(8)
20
20
  product.accounting_code.should == 'TSMO'
21
21
  end
22
+
23
+ it "should return nil if the product does not exist for a product" do
24
+ stub_get "https://OU812:x@pengwynn.chargify.com/products/99.json", "", 404
25
+ Chargify::Product.find(99).should == nil
26
+ end
22
27
 
23
28
  end
24
29
 
@@ -9,23 +9,33 @@ describe Chargify::Subscription do
9
9
  end
10
10
  end
11
11
 
12
- describe '.find' do
12
+ describe '.find!' do
13
13
 
14
14
  it "should return info for a subscription" do
15
15
  stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/13.json", "subscription.json"
16
- subscription = Chargify::Subscription.find(13)
16
+ subscription = Chargify::Subscription.find!(13)
17
17
  subscription.customer.reference.should == 'bradleyjoyce'
18
18
  end
19
19
 
20
+ it "should throw a not found error if a subscription is not found" do
21
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404
22
+ lambda {
23
+ Chargify::Subscription.find!(18)
24
+ }.should raise_error(Chargify::Error::NotFound)
25
+ end
26
+
27
+ end
28
+
29
+ describe '.find' do
30
+
20
31
  it "should return nil if a subscription is not found" do
21
32
  stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/18.json", "subscription_not_found.json", 404
22
- subscription = Chargify::Subscription.find(18)
23
- subscription.should == nil
33
+ Chargify::Subscription.find(18).should == nil
24
34
  end
25
35
 
26
36
  end
27
37
 
28
- describe '.create' do
38
+ describe '.create!' do
29
39
 
30
40
  it "should create a customer subscription" do
31
41
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json"
@@ -38,7 +48,7 @@ describe Chargify::Subscription do
38
48
  :email => "wynn@example.com"
39
49
  }
40
50
  }
41
- subscription = Chargify::Subscription.create(options)
51
+ subscription = Chargify::Subscription.create!(options)
42
52
  subscription.customer.organization.should == 'Squeejee'
43
53
  end
44
54
 
@@ -54,11 +64,12 @@ describe Chargify::Subscription do
54
64
  },
55
65
  :coupon_code => "EARLYBIRD"
56
66
  }
57
- subscription = Chargify::Subscription.create(options)
67
+ subscription = Chargify::Subscription.create!(options)
68
+ subscription.should be_a(Hashie::Mash)
58
69
  #subscription.coupon.should == 'Squeejee'
59
70
  end
60
71
 
61
- it "should set success? to true when subscription is created successfully" do
72
+ it "should raise error to true when subscription is created successfully" do
62
73
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 201
63
74
  options = {
64
75
  :product_handle => 'monthly',
@@ -69,12 +80,12 @@ describe Chargify::Subscription do
69
80
  :email => "wynn@example.com"
70
81
  }
71
82
  }
72
- subscription = Chargify::Subscription.create(options)
73
- subscription.success?.should == true
83
+ subscription = Chargify::Subscription.create!(options)
84
+ subscription.should be_a(Hashie::Mash)
74
85
  end
75
86
 
76
- it "should set success? to nil when subscription is not created successfully" do
77
- stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 400
87
+ it "should raise error to nil when subscription is not created successfully" do
88
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422
78
89
  options = {
79
90
  :product_handle => 'monthly',
80
91
  :customer_reference => 'bradleyjoyce',
@@ -84,8 +95,10 @@ describe Chargify::Subscription do
84
95
  :email => "wynn@example.com"
85
96
  }
86
97
  }
87
- subscription = Chargify::Subscription.create(options)
88
- subscription.success?.should == nil
98
+ lambda {
99
+ Chargify::Subscription.create!(options)
100
+ }.should raise_error(Chargify::Error::BadRequest)
101
+
89
102
  end
90
103
 
91
104
  it "should raise UnexpectedResponseError when reponse is invalid JSON" do
@@ -100,12 +113,31 @@ describe Chargify::Subscription do
100
113
  }
101
114
  }
102
115
  lambda {
103
- Chargify::Subscription.create(options)
116
+ Chargify::Subscription.create!(options)
104
117
  }.should raise_error(Chargify::Error::UnexpectedResponse)
105
118
  end
119
+
120
+ end
121
+
122
+ describe '.create' do
123
+
124
+ it "should return false if creation errors out" do
125
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions.json", "subscription.json", 422
126
+ options = {
127
+ :product_handle => 'monthly',
128
+ :customer_reference => 'bradleyjoyce',
129
+ :customer_attributes => {
130
+ :first_name => "Wynn",
131
+ :last_name => "Netherland",
132
+ :email => "wynn@example.com"
133
+ }
134
+ }
135
+ Chargify::Subscription.create(options).should == false
136
+ end
137
+
106
138
  end
107
139
 
108
- describe '.update' do
140
+ describe '.update!' do
109
141
 
110
142
  it "should update a customer subscription" do
111
143
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json"
@@ -118,11 +150,11 @@ describe Chargify::Subscription do
118
150
  :email => "wynn@example.com"
119
151
  }
120
152
  }
121
- subscription = Chargify::Subscription.update(123, options)
153
+ subscription = Chargify::Subscription.update!(123, options)
122
154
  subscription.customer.organization.should == 'Squeejee'
123
155
  end
124
156
 
125
- it "should set success? to true when subscription is updated successfully" do
157
+ it "should raise error to true when subscription is updated successfully" do
126
158
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 200
127
159
  options = {
128
160
  :product_handle => 'monthly',
@@ -133,11 +165,11 @@ describe Chargify::Subscription do
133
165
  :email => "wynn@example.com"
134
166
  }
135
167
  }
136
- subscription = Chargify::Subscription.update(123, options)
137
- subscription.success?.should == true
168
+ subscription = Chargify::Subscription.update!(123, options)
169
+ subscription.should be_a(Hashie::Mash)
138
170
  end
139
171
 
140
- it "should set success? to false when subscription is not updated successfully" do
172
+ it "should raise error to false when subscription is not updated successfully" do
141
173
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500
142
174
  options = {
143
175
  :product_handle => 'monthly',
@@ -148,64 +180,91 @@ describe Chargify::Subscription do
148
180
  :email => "wynn@example.com"
149
181
  }
150
182
  }
151
- subscription = Chargify::Subscription.update(123, options)
152
- subscription.success?.should == nil
183
+
184
+ lambda {
185
+ Chargify::Subscription.update!(123, options)
186
+ }.should raise_error(Chargify::Error::ServerError)
153
187
  end
154
188
 
155
189
  end
190
+
191
+ describe '.update' do
192
+
193
+ it "should return false if subscription update fails" do
194
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "subscription.json", 500
195
+ options = {
196
+ :product_handle => 'monthly',
197
+ :customer_reference => 'bradleyjoyce',
198
+ :customer_attributes => {
199
+ :first_name => "Wynn",
200
+ :last_name => "Netherland",
201
+ :email => "wynn@example.com"
202
+ }
203
+ }
204
+ Chargify::Subscription.update(123, options)
205
+ end
206
+
207
+ end
156
208
 
157
- describe '.reactivate' do
209
+ describe '.reactivate!' do
158
210
 
159
211
  it "should reactivate a subscription" do
160
212
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
161
- subscription = Chargify::Subscription.reactivate(123)
162
-
213
+ subscription = Chargify::Subscription.reactivate!(123)
163
214
  subscription.state.should == "active"
164
215
  end
165
216
 
166
- it "should set success? to nil when subscription is not reactivated successfully" do
167
- stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 500
168
- subscription = Chargify::Subscription.reactivate(123)
169
-
170
- subscription.success?.should == nil
217
+ it "should raise error to nil when subscription is not reactivated successfully" do
218
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422
219
+ lambda {
220
+ Chargify::Subscription.reactivate!(123)
221
+ }.should raise_error(Chargify::Error::BadRequest)
171
222
  end
172
223
 
173
- it "should set success? to false when subscription is reactivated successfully" do
224
+ it "should raise error to false when subscription is reactivated successfully" do
174
225
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription.json", 200
175
- subscription = Chargify::Subscription.reactivate(123)
176
-
177
- subscription.success?.should == true
226
+ subscription = Chargify::Subscription.reactivate!(123)
227
+ subscription.should be_a(Hashie::Mash)
178
228
  end
179
229
 
180
230
  end
231
+
232
+ describe '.reactivate' do
181
233
 
182
- describe '.cancel' do
234
+ it "should return false if an error occurs" do
235
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/reactivate.json", "subscription_not_found.json", 422
236
+ Chargify::Subscription.reactivate(123).should == false
237
+ end
183
238
 
184
- it "should cancel subscription" do
185
- stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200
186
- subscription = Chargify::Subscription.cancel(123)
239
+ end
187
240
 
188
- subscription.state.should == "canceled"
189
- end
241
+ describe '.cancel!' do
190
242
 
191
- it "should set success? to nil when subscription is not cancelled successfully" do
243
+ it "should raise error to nil when subscription is not cancelled successfully" do
192
244
  stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500
193
- subscription = Chargify::Subscription.cancel(123)
194
-
195
- subscription.success?.should == nil
245
+
246
+ lambda {
247
+ Chargify::Subscription.cancel!(123)
248
+ }.should raise_error(Chargify::Error::ServerError)
196
249
  end
197
250
 
198
- it "should set success? to true when subscription is cancelled successfully" do
251
+ it "should raise error to true when subscription is cancelled successfully" do
199
252
  stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 200
200
- subscription = Chargify::Subscription.cancel(123)
201
-
202
- subscription.success?.should == true
253
+ Chargify::Subscription.cancel!(123).should == true
203
254
  end
204
255
 
205
256
  end
257
+
258
+ describe '.cancel' do
259
+
260
+ it "should return false if an error occurs" do
261
+ stub_delete "https://OU812:x@pengwynn.chargify.com/subscriptions/123.json", "deleted_subscription.json", 500
262
+ Chargify::Subscription.cancel(123).should == false
263
+ end
206
264
 
265
+ end
207
266
 
208
- describe '.charge' do
267
+ describe '.charge!' do
209
268
 
210
269
  before do
211
270
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription.json", 201
@@ -217,48 +276,75 @@ describe Chargify::Subscription do
217
276
  end
218
277
 
219
278
  it "should accept :amount as a parameter" do
220
- subscription = Chargify::Subscription.charge(123, @options)
279
+ subscription = Chargify::Subscription.charge!(123, @options)
221
280
 
281
+ subscription.should be_a(Hashie::Mash)
222
282
  subscription.amount_in_cents.should == @options[:amount]*100
223
- subscription.success?.should == true
224
283
  end
225
284
 
226
285
  it "should accept :amount_in_cents as a parameter" do
227
- subscription = Chargify::Subscription.charge(123, @options)
286
+ subscription = Chargify::Subscription.charge!(123, @options)
228
287
 
288
+ subscription.should be_a(Hashie::Mash)
229
289
  subscription.amount_in_cents.should == @options[:amount_in_cents]
230
- subscription.success?.should == true
231
290
  end
232
291
 
233
292
  it "should have success? as false if parameters are missing" do
234
293
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
235
294
 
236
- subscription = Chargify::Subscription.charge(123, {})
237
- subscription.success?.should == false
295
+ lambda {
296
+ Chargify::Subscription.charge!(123, {})
297
+ }.should raise_error(Chargify::Error::BadRequest)
238
298
  end
239
299
 
240
300
  it "should have success? as false if the subscription is not found" do
241
301
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/9999/charges.json", "", 404
242
302
 
243
- subscription = Chargify::Subscription.charge(9999, @options)
244
- subscription.success?.should == false
303
+ lambda {
304
+ Chargify::Subscription.charge!(9999, @options)
305
+ }.should raise_error(Chargify::Error::NotFound)
245
306
  end
246
307
 
247
308
  end
309
+
310
+ describe '.charge' do
311
+
312
+ it "should return false if an error occurs" do
313
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/charges.json", "charge_subscription_missing_parameters.json", 422
314
+ Chargify::Subscription.charge(123, {}).should == false
315
+ end
316
+
317
+ end
248
318
 
249
- describe '.migrate' do
319
+ describe '.migrate!' do
250
320
 
251
321
  it "should migrate a subscription from one product to another" do
252
322
  stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "migrate_subscription.json"
253
323
 
254
- subscription = Chargify::Subscription.migrate(123, 354);
255
- subscription.success?.should == true
324
+ subscription = Chargify::Subscription.migrate!(123, 354)
325
+ subscription.should be_a(Hashie::Mash)
256
326
  subscription.product.id.should == 354
257
327
  end
328
+
329
+ it "should throw an error if one occurs" do
330
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422
331
+ lambda {
332
+ Chargify::Subscription.migrate!(123, 354)
333
+ }.should raise_error(Chargify::Error::BadRequest)
334
+ end
258
335
 
259
336
  end
337
+
338
+ describe '.migrate' do
260
339
 
261
- describe '.components' do
340
+ it "should return false if an error occurs" do
341
+ stub_post "https://OU812:x@pengwynn.chargify.com/subscriptions/123/migrations.json", "", 422
342
+ Chargify::Subscription.migrate(123, 354).should == false
343
+ end
344
+
345
+ end
346
+
347
+ describe '.components!' do
262
348
 
263
349
  it "should list components" do
264
350
  stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components.json", "components.json"
@@ -267,38 +353,70 @@ describe Chargify::Subscription do
267
353
  components.last.allocated_quantity.should == 2
268
354
  end
269
355
 
356
+ it "should throw an error if one occurs"
357
+
358
+ end
359
+
360
+ describe '.components' do
361
+
362
+ it 'should return false if an error occurs'
363
+
270
364
  end
271
365
 
272
- describe '.find_component' do
366
+ describe '.find_component!' do
273
367
 
274
368
  it "should show a specific component" do
275
369
  stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
276
- component = Chargify::Subscription.find_component(123, 16)
370
+ component = Chargify::Subscription.find_component!(123, 16)
277
371
  component.name.should == "Extra Rubies"
278
372
  component.allocated_quantity.should == 42
279
373
  end
280
374
 
375
+ it "should throw an error if one occurs"
376
+
377
+ end
378
+
379
+ describe '.find_component!' do
380
+
381
+ it 'should return false if an error occurs'
382
+
281
383
  end
282
384
 
283
- describe '.update_component' do
385
+ describe '.update_component!' do
284
386
 
285
387
  it "should update the allocated_quantity for a component" do
286
388
  stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
287
- response = Chargify::Subscription.update_component(123, 16, 20_000_000)
288
- response.success?.should == true
389
+ response = Chargify::Subscription.update_component!(123, 16, 20_000_000)
390
+ response.should be_a(Hashie::Mash)
289
391
  end
392
+
393
+ it "should throw an error if one occurs"
290
394
 
291
395
  end
292
396
 
293
- describe '.component_usage' do
397
+ describe '.update_component' do
398
+
399
+ it 'should return false if an error occurs'
400
+
401
+ end
402
+
403
+ describe '.component_usage!' do
294
404
 
295
405
  it "should list usage for a subscription" do
296
406
  stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/456/usages.json", "list_metered_subscriptions.json", 200
297
407
 
298
408
  subscription = Chargify::Subscription.component_usage(123, 456)
299
- subscription.success?.should == true
409
+ subscription.should be_a(Hashie::Mash)
300
410
  end
411
+
412
+ it "should throw an error if one occurs"
301
413
 
302
414
  end
415
+
416
+ describe '.component_usage' do
417
+
418
+ it 'should return false if an error occurs'
419
+
420
+ end
303
421
 
304
422
  end
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 3
8
8
  - 0
9
- - pre5
10
- version: 0.3.0.pre5
9
+ - pre6
10
+ version: 0.3.0.pre6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Wynn Netherland