hello_sign 1.0.1 → 1.1.0
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.
- data/lib/hello_sign/error.rb +16 -0
- data/lib/hello_sign/middleware/raise_error.rb +29 -11
- data/lib/hello_sign/version.rb +1 -1
- data/spec/unit/error_spec.rb +9 -0
- data/spec/unit/middleware/raise_error_spec.rb +36 -15
- metadata +3 -3
    
        data/lib/hello_sign/error.rb
    CHANGED
    
    | @@ -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 | 
            -
                     | 
| 13 | 
            -
                       | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 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 |  | 
    
        data/lib/hello_sign/version.rb
    CHANGED
    
    
    
        data/spec/unit/error_spec.rb
    CHANGED
    
    | @@ -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( | 
| 6 | 
            -
             | 
| 7 | 
            -
               | 
| 8 | 
            -
             | 
| 9 | 
            -
               | 
| 10 | 
            -
             | 
| 11 | 
            -
               | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 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 = { | 
| 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 | 
| 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- | 
| 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. | 
| 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
         |