rspreedly 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -69,6 +69,12 @@ Raise an invoice, and pay it
69
69
  payment = RSpreedly::PaymentMethod::CreditCard(:card_type => 'visa', :number => '123456', ...)
70
70
  invoice.pay(payment)
71
71
 
72
+ Error messages
73
+
74
+ invoice = RSpreedly::Invoice.new
75
+ invoice.save => false
76
+ invoice.errors => ["You must specify both the subscription plan id and subscriber information."]
77
+
72
78
  See the specs and API docs at Spreedly for more details and the rest of the things it can do.
73
79
 
74
80
  https://www.spreedly.com/manual/integration-reference/url-reference/
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,6 +5,8 @@ module RSpreedly
5
5
  format :xml
6
6
  base_uri "https://spreedly.com/api/v4"
7
7
 
8
+ attr_reader :errors
9
+
8
10
  def self.api_request(type, path, options={})
9
11
  path = "/#{::RSpreedly::Config.site_name}#{path}"
10
12
  options.merge!({
@@ -20,21 +22,22 @@ module RSpreedly
20
22
 
21
23
  case response.code.to_i
22
24
  when 401
23
- raise(RSpreedly::Error::AccessDenied.new, message)
25
+ raise(RSpreedly::Error::AccessDenied.new(response), message)
24
26
  when 403
25
- raise(RSpreedly::Error::Forbidden.new, message)
27
+ raise(RSpreedly::Error::Forbidden.new(response), message)
26
28
  when 422
27
- raise(RSpreedly::Error::BadRequest.new, message)
29
+ raise(RSpreedly::Error::BadRequest.new(response), message)
28
30
  when 404
29
- raise(RSpreedly::Error::NotFound.new, message)
31
+ raise(RSpreedly::Error::NotFound.new(response), message)
30
32
  when 504
31
- raise(RSpreedly::Error::GatewayTimeout.new, message)
33
+ raise(RSpreedly::Error::GatewayTimeout.new(response), message)
32
34
  end
33
35
 
34
36
  response
35
37
  end
36
38
 
37
39
  def initialize(attrs={})
40
+ @errors = []
38
41
  self.attributes = attrs
39
42
  end
40
43
 
@@ -45,7 +48,19 @@ module RSpreedly
45
48
  end
46
49
 
47
50
  def api_request(type, path, options={})
48
- self.class.api_request(type, path, options)
51
+ @errors = []
52
+ begin
53
+ self.class.api_request(type, path, options)
54
+ rescue RSpreedly::Error::Base => e
55
+ if e.response.is_a?(Hash)
56
+ if e.response.has_key?("errors")
57
+ @errors = [*e.response["errors"]["error"]]
58
+ else
59
+ @errors = [e.response.body]
60
+ end
61
+ end
62
+ raise
63
+ end
49
64
  end
50
65
 
51
66
  # TODO - do this nicer
@@ -1,6 +1,14 @@
1
1
  module RSpreedly
2
2
  module Error
3
- class Base < StandardError; end
3
+
4
+ class Base < StandardError;
5
+ attr_reader :response
6
+
7
+ def initialize(response=nil)
8
+ @response = response
9
+ end
10
+ end
11
+
4
12
  class AccessDenied < Base; end # 401 errors
5
13
  class Forbidden < Base; end # 403 errors
6
14
  class BadRequest < Base; end # 422 errors
@@ -68,6 +68,10 @@ module RSpreedly
68
68
  self.new_record? ? self.create : self.update
69
69
  end
70
70
 
71
+ def save!
72
+ self.new_record? ? self.create! : self.update!
73
+ end
74
+
71
75
  # Create a subscriber (more)
72
76
  # POST /api/v4/[short site name]/subscribers.xml
73
77
  def create!
data/rspreedly.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rspreedly}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Richard Livsey"]
9
- s.date = %q{2009-11-03}
9
+ s.date = %q{2009-11-04}
10
10
  s.email = %q{richard@livsey.org}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -38,6 +38,9 @@ Gem::Specification.new do |s|
38
38
  "spec/fixtures/complimentary_not_valid.xml",
39
39
  "spec/fixtures/complimentary_success.xml",
40
40
  "spec/fixtures/create_subscriber.xml",
41
+ "spec/fixtures/error.xml",
42
+ "spec/fixtures/error_string.txt",
43
+ "spec/fixtures/errors.xml",
41
44
  "spec/fixtures/existing_subscriber.xml",
42
45
  "spec/fixtures/free_plan_not_elligable.xml",
43
46
  "spec/fixtures/free_plan_not_free.xml",
data/spec/base_spec.rb CHANGED
@@ -1,13 +1,68 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class TestAPI < RSpreedly::Base; end
4
+
2
5
  describe RSpreedly::Base do
3
6
 
4
- describe "#api_request" do
7
+ describe ".api_request" do
5
8
  it "should raise AccessDenied if a 401 is received" do
6
9
  stub_http_with_code(401)
7
10
  lambda{
8
11
  RSpreedly::Base.api_request(:put, '/')
9
12
  }.should raise_error(RSpreedly::Error::AccessDenied)
10
13
  end
14
+ end
15
+
16
+ describe "error messages" do
17
+
18
+ before(:each) do
19
+ @api = TestAPI.new
20
+ end
21
+
22
+ def do_request
23
+ @api.api_request(:post, "/")
24
+ end
25
+
26
+ def failing_request
27
+ lambda{
28
+ do_request
29
+ }.should raise_error
30
+ end
31
+
32
+ it "should not set any errors on a successful request" do
33
+ stub_http_with_code(200)
34
+ do_request
35
+ @api.errors.should be_empty
36
+ end
37
+
38
+ it "should set one error in the error array if a string is return " do
39
+ stub_http_with_fixture("error_string.txt", 422)
40
+ failing_request
41
+ @api.errors.should == ["Some error back from the response as a string"]
42
+ end
43
+
44
+ it "should set one error in the error array if an xml error with one item is returned" do
45
+ stub_http_with_fixture("error.xml", 422)
46
+ failing_request
47
+ @api.errors.should == ["Email can't be blank"]
48
+ end
49
+
50
+ it "should set multiple errors in the error array if an xml error with multiple items is returned" do
51
+ stub_http_with_fixture("errors.xml", 422)
52
+ failing_request
53
+ @api.errors.should == ["Email can't be blank", "Name can't be blank"]
54
+ end
11
55
 
56
+ it "should reset errors on each call" do
57
+ # failing one first, which will generate some errors
58
+ stub_http_with_fixture("errors.xml", 422)
59
+ failing_request
60
+ @api.errors.should_not be_empty
61
+
62
+ # now a successful one should clear the errors
63
+ stub_http_with_code(200)
64
+ do_request
65
+ @api.errors.should be_empty
66
+ end
12
67
  end
13
- end
68
+ end
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <errors>
3
+ <error>Email can't be blank</error>
4
+ </errors>
@@ -0,0 +1 @@
1
+ Some error back from the response as a string
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <errors>
3
+ <error>Email can't be blank</error>
4
+ <error>Name can't be blank</error>
5
+ </errors>
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
- require 'net-http-spy'
4
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
5
  require 'rspreedly'
@@ -71,6 +71,23 @@ describe RSpreedly::Subscriber do
71
71
  @subscriber.save
72
72
  end
73
73
  end
74
+
75
+ describe "#save!" do
76
+ before(:each) do
77
+ @subscriber = RSpreedly::Subscriber.new
78
+ end
79
+
80
+ it "should call #create! for a non existing subscriber" do
81
+ @subscriber.should_receive(:create!)
82
+ @subscriber.save!
83
+ end
84
+
85
+ it "should call update! for an existing subscriber" do
86
+ @subscriber.token = "something"
87
+ @subscriber.should_receive(:update!)
88
+ @subscriber.save!
89
+ end
90
+ end
74
91
 
75
92
  describe "#create!" do
76
93
  it "should return true if successful" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspreedly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Livsey
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-03 00:00:00 +01:00
12
+ date: 2009-11-04 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -48,6 +48,9 @@ files:
48
48
  - spec/fixtures/complimentary_not_valid.xml
49
49
  - spec/fixtures/complimentary_success.xml
50
50
  - spec/fixtures/create_subscriber.xml
51
+ - spec/fixtures/error.xml
52
+ - spec/fixtures/error_string.txt
53
+ - spec/fixtures/errors.xml
51
54
  - spec/fixtures/existing_subscriber.xml
52
55
  - spec/fixtures/free_plan_not_elligable.xml
53
56
  - spec/fixtures/free_plan_not_free.xml