hello_sign 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,21 @@
1
1
  module HelloSign
2
2
  class Error < StandardError
3
+ attr_reader :message, :status_code
4
+
5
+ def initialize(message = nil, status_code = nil)
6
+ @message, @status_code = message, status_code
7
+ end
8
+
9
+ def to_s
10
+ status_code ? message_with_status_code : message
11
+ end
12
+
13
+ private
14
+
15
+ def message_with_status_code
16
+ "[Status code: #{status_code}] #{message}"
17
+ end
18
+
3
19
  class BadRequest < HelloSign::Error; end
4
20
  class Unauthorized < HelloSign::Error; end
5
21
  class Forbidden < HelloSign::Error; end
@@ -9,18 +9,36 @@ module HelloSign
9
9
  def on_complete(env)
10
10
  body = env[:body] or return
11
11
 
12
- if error = body[:error]
13
- case error[:error_name]
14
- when 'bad_request' then raise HelloSign::Error::BadRequest
15
- when 'unauthorized' then raise HelloSign::Error::Unauthorized
16
- when 'forbidden' then raise HelloSign::Error::Forbidden
17
- when 'not_found' then raise HelloSign::Error::NotFound
18
- when 'unknown' then raise HelloSign::Error::Unknown
19
- when 'team_invite_failed' then raise HelloSign::Error::TeamInviteFailed
20
- when 'invalid_recipient' then raise HelloSign::Error::InvalidRecipient
21
- when 'convert_failed' then raise HelloSign::Error::ConvertFailed
22
- when 'signature_request_cancel_failed' then raise HelloSign::Error::SignatureRequestCancelFailed
12
+ error = body[:error] and begin
13
+ exception = begin
14
+ case error[:error_name]
15
+ when 'bad_request'
16
+ HelloSign::Error::BadRequest
17
+ when 'unauthorized'
18
+ HelloSign::Error::Unauthorized
19
+ when 'forbidden'
20
+ HelloSign::Error::Forbidden
21
+ when 'not_found'
22
+ HelloSign::Error::NotFound
23
+ when 'unknown'
24
+ HelloSign::Error::Unknown
25
+ when 'team_invite_failed'
26
+ HelloSign::Error::TeamInviteFailed
27
+ when 'invalid_recipient'
28
+ HelloSign::Error::InvalidRecipient
29
+ when 'convert_failed'
30
+ HelloSign::Error::ConvertFailed
31
+ when 'signature_request_cancel_failed'
32
+ HelloSign::Error::SignatureRequestCancelFailed
33
+ else
34
+ HelloSign::Error
35
+ end
23
36
  end
37
+
38
+ message = error[:error_msg]
39
+ status_code = env[:response][:status]
40
+
41
+ raise exception.new(message, status_code)
24
42
  end
25
43
  end
26
44
 
@@ -1,3 +1,3 @@
1
1
  module HelloSign
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -12,4 +12,13 @@ describe HelloSign::Error do
12
12
  specify { expect { raise HelloSign::Error::InvalidRecipient }.to raise_error HelloSign::Error }
13
13
  specify { expect { raise HelloSign::Error::ConvertFailed }.to raise_error HelloSign::Error }
14
14
  specify { expect { raise HelloSign::Error::SignatureRequestCancelFailed }.to raise_error HelloSign::Error }
15
+
16
+ describe "#to_s" do
17
+ let(:error) { HelloSign::Error.new('This is why an error was received.', 401) }
18
+ let(:expected) { '[Status code: 401] This is why an error was received.' }
19
+
20
+ it "displays a helpful message" do
21
+ expect(error.to_s).to eq expected
22
+ end
23
+ end
15
24
  end
@@ -2,26 +2,47 @@ require 'helper'
2
2
  require 'hello_sign/middleware/raise_error'
3
3
 
4
4
  describe HelloSign::Middleware::RaiseError do
5
- let(:middleware) { HelloSign::Middleware::RaiseError.new(lambda { |env| Faraday::Response.new(env) }) }
6
-
7
- specify { expect { call_with_error('bad_request') }.to raise_error HelloSign::Error::BadRequest }
8
- specify { expect { call_with_error('unauthorized') }.to raise_error HelloSign::Error::Unauthorized }
9
- specify { expect { call_with_error('forbidden') }.to raise_error HelloSign::Error::Forbidden }
10
- specify { expect { call_with_error('not_found') }.to raise_error HelloSign::Error::NotFound }
11
- specify { expect { call_with_error('unknown') }.to raise_error HelloSign::Error::Unknown }
12
- specify { expect { call_with_error('team_invite_failed') }.to raise_error HelloSign::Error::TeamInviteFailed }
13
- specify { expect { call_with_error('invalid_recipient') }.to raise_error HelloSign::Error::InvalidRecipient }
14
- specify { expect { call_with_error('convert_failed') }.to raise_error HelloSign::Error::ConvertFailed }
15
- specify { expect { call_with_error('signature_request_cancel_failed') }.to raise_error HelloSign::Error::SignatureRequestCancelFailed }
16
-
17
- it 'does not blow up if there is not a body' do
5
+ let(:middleware) { HelloSign::Middleware::RaiseError.new(->(env) { Faraday::Response.new(env) }) }
6
+
7
+ def self.it_raises_the_proper_exception(error, exception)
8
+ specify { expect { call_with_error(error) }.to raise_error exception }
9
+ end
10
+
11
+ {
12
+ 'unauthorized' => HelloSign::Error::Unauthorized,
13
+ 'forbidden' => HelloSign::Error::Forbidden,
14
+ 'not_found' => HelloSign::Error::NotFound,
15
+ 'unknown' => HelloSign::Error::Unknown,
16
+ 'team_invite_failed' => HelloSign::Error::TeamInviteFailed,
17
+ 'invalid_recipient' => HelloSign::Error::InvalidRecipient,
18
+ 'convert_failed' => HelloSign::Error::ConvertFailed,
19
+ 'signature_request_cancel_failed' => HelloSign::Error::SignatureRequestCancelFailed,
20
+ 'unidentified_error' => HelloSign::Error
21
+ }.each { |error_pair| it_raises_the_proper_exception(*error_pair) }
22
+
23
+ it "does not blow up if there is not a body" do
18
24
  expect { middleware.call({}) }.to_not raise_error
19
25
  end
20
26
 
27
+ context "when raising an exception" do
28
+ let(:expected_message) { "[Status code: 418] I'm a teapot." }
29
+
30
+ it "returns the proper message" do
31
+ begin
32
+ call_with_error('bad_request', message: "I'm a teapot.", status_code: 418)
33
+ rescue HelloSign::Error::BadRequest => e
34
+ expect("#{e}").to match /#{Regexp.escape(expected_message)}/
35
+ end
36
+ end
37
+ end
38
+
21
39
  private
22
40
 
23
- def call_with_error(error)
24
- env = {body: {error: {error_name: error}}}
41
+ def call_with_error(error, options = {})
42
+ env = {
43
+ body: {error: {error_name: error, error_msg: options[:message]}},
44
+ response: {status: options[:status_code]}
45
+ }
25
46
  middleware.call(env)
26
47
  end
27
48
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hello_sign
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  version: '0'
180
180
  requirements: []
181
181
  rubyforge_project:
182
- rubygems_version: 1.8.23
182
+ rubygems_version: 1.8.25
183
183
  signing_key:
184
184
  specification_version: 3
185
185
  summary: A Ruby interface to the HelloSign API