diff --git a/Gemfile.lock b/Gemfile.lock index cc37ab6..054efed 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - veryfi (1.0.0) + veryfi (1.0.1) base64 (~> 0.1) faraday (~> 1.7) openssl (>= 2.2, < 3.1) @@ -13,15 +13,15 @@ GEM public_suffix (>= 2.0.2, < 5.0) ast (2.4.2) base64 (0.1.1) - bundler-audit (0.9.0) + bundler-audit (0.9.1) bundler (>= 1.2.0, < 3) thor (~> 1.0) coderay (1.1.3) crack (0.4.5) rexml - diff-lcs (1.4.4) + diff-lcs (1.5.0) docile (1.4.0) - faraday (1.10.0) + faraday (1.10.1) faraday-em_http (~> 1.0) faraday-em_synchrony (~> 1.0) faraday-excon (~> 1.1) @@ -37,46 +37,44 @@ GEM faraday-em_synchrony (1.0.0) faraday-excon (1.1.0) faraday-httpclient (1.0.1) - faraday-multipart (1.0.3) - multipart-post (>= 1.2, < 3) + faraday-multipart (1.0.4) + multipart-post (~> 2) faraday-net_http (1.0.1) faraday-net_http_persistent (1.2.0) faraday-patron (1.0.0) faraday-rack (1.0.0) faraday-retry (1.0.3) hashdiff (1.0.1) - ipaddr (1.2.4) method_source (1.0.0) - multipart-post (2.1.1) - openssl (2.2.1) - ipaddr - parallel (1.20.1) - parser (3.0.2.0) + multipart-post (2.2.3) + openssl (3.0.0) + parallel (1.22.1) + parser (3.1.2.1) ast (~> 2.4.1) pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) - public_suffix (4.0.6) - rainbow (3.0.0) + public_suffix (4.0.7) + rainbow (3.1.1) rake (13.0.6) - regexp_parser (2.1.1) + regexp_parser (2.5.0) rexml (3.2.5) - rspec (3.10.0) - rspec-core (~> 3.10.0) - rspec-expectations (~> 3.10.0) - rspec-mocks (~> 3.10.0) - rspec-core (3.10.1) - rspec-support (~> 3.10.0) - rspec-expectations (3.10.1) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) + rspec-support (~> 3.11.0) rspec-its (1.3.0) rspec-core (>= 3.0.0) rspec-expectations (>= 3.0.0) - rspec-mocks (3.10.2) + rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) - rspec-support (3.10.2) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) rubocop (0.93.1) parallel (~> 1.10) parser (>= 2.7.1.5) @@ -86,8 +84,8 @@ GEM rubocop-ast (>= 0.6.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 2.0) - rubocop-ast (1.11.0) - parser (>= 3.0.1.1) + rubocop-ast (1.21.0) + parser (>= 3.1.1.0) rubocop-rspec (1.44.1) rubocop (~> 0.87) rubocop-ast (>= 0.7.1) @@ -99,11 +97,11 @@ GEM simplecov_json_formatter (~> 0.1) simplecov-badge (2.0.2) simplecov-html (0.12.3) - simplecov_json_formatter (0.1.3) - thor (1.1.0) - unicode-display_width (1.7.0) - vcr (6.0.0) - webmock (3.14.0) + simplecov_json_formatter (0.1.4) + thor (1.2.1) + unicode-display_width (1.8.0) + vcr (6.1.0) + webmock (3.17.1) addressable (>= 2.8.0) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) @@ -129,4 +127,4 @@ DEPENDENCIES webmock (~> 3.14) BUNDLED WITH - 2.2.26 + 2.2.33 diff --git a/coverage/coverage-badge.png b/coverage/coverage-badge.png index 2a87bd7..16ffe0b 100644 Binary files a/coverage/coverage-badge.png and b/coverage/coverage-badge.png differ diff --git a/lib/veryfi/error.rb b/lib/veryfi/error.rb index 202196e..597fdf1 100644 --- a/lib/veryfi/error.rb +++ b/lib/veryfi/error.rb @@ -2,44 +2,62 @@ module Veryfi class Error - def self.from_response(response) - case response.status + def self.from_response(status, response) + if response.empty? + VeryfiError.new(format("%d", code: status)) + else + get_error(status, response) + end + end + + def self.get_error(status, response) + case status when 400 then BadRequest when 401 then UnauthorizedAccessToken + when 404 then ResourceNotFound when 405 then UnexpectedHTTPMethod when 409 then AccessLimitReached - else InternalError + when 500 then InternalError + else VeryfiError.new(format("%d, %s", code: status, message: response["error"])) end end - class BadRequest < StandardError def to_s - "Bad Request" + "400, Bad Request" end end class UnauthorizedAccessToken < StandardError def to_s - "Unauthorized Access Token" + "401, Unauthorized Access Token" + end + end + + class ResourceNotFound < StandardError + def to_s + "404, Resource not found" end end class UnexpectedHTTPMethod < StandardError def to_s - "Unexpected HTTP Method" + "405, Unexpected HTTP Method" end end class AccessLimitReached < StandardError def to_s - "Access Limit Reached" + "409, Access Limit Reached" end end class InternalError < StandardError def to_s - "Internal Server Error" + "500, Internal Server Error" end end + + class VeryfiError < StandardError + end end end diff --git a/lib/veryfi/request.rb b/lib/veryfi/request.rb index e23b932..ce2c9e3 100644 --- a/lib/veryfi/request.rb +++ b/lib/veryfi/request.rb @@ -56,11 +56,12 @@ def make_request(http_verb, path, params = {}) headers = generate_headers(params) response = conn.public_send(http_verb, url, body, headers) + json_response = process_response(response) if response.success? - process_response(http_verb, response) + json_response else - raise Veryfi::Error.from_response(response) + raise Veryfi::Error.from_response(response.status, json_response) end end @@ -102,8 +103,8 @@ def generate_signature(params, timestamp) Veryfi::Signature.new(client_secret, params, timestamp).to_base64 end - def process_response(_http_verb, response) - # return response if http_verb == :delete + def process_response(response) + return {} if response.body.empty? JSON.parse(response.body) end diff --git a/lib/veryfi/version.rb b/lib/veryfi/version.rb index 7f9143f..5f6c3a4 100644 --- a/lib/veryfi/version.rb +++ b/lib/veryfi/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Veryfi - VERSION = "1.0.0" + VERSION = "1.0.1" end diff --git a/spec/veryfi/request_spec.rb b/spec/veryfi/request_spec.rb index 4573021..2dc5eda 100644 --- a/spec/veryfi/request_spec.rb +++ b/spec/veryfi/request_spec.rb @@ -29,65 +29,104 @@ context "when server responds with 400 error" do before do - stub_request(:get, /\.*/).to_return(status: 400) + stub_request(:get, /\.*/).to_return(status: 400, body: '{"code": 400, "error": "Bad Request"}') end it "raises error" do expect { client.document.all }.to raise_error( Veryfi::Error::BadRequest, - "Bad Request" + "400, Bad Request" ) end end context "when server responds with 401 error" do before do - stub_request(:get, /\.*/).to_return(status: 401) + stub_request(:get, /\.*/).to_return(status: 401, body: '{"error": "Unauthorized Access Token"}') end it "raises error" do expect { client.document.all }.to raise_error( Veryfi::Error::UnauthorizedAccessToken, - "Unauthorized Access Token" + "401, Unauthorized Access Token" + ) + end + end + + context "when server responds with 404 error" do + before do + stub_request(:get, /\.*/).to_return(status: 404, body: '{"error": "Resource Not Found"}') + end + + it "raises error" do + expect { client.document.all }.to raise_error( + Veryfi::Error::ResourceNotFound, + "404, Resource not found" ) end end context "when server responds with 405 error" do before do - stub_request(:get, /\.*/).to_return(status: 405) + stub_request(:get, /\.*/).to_return(status: 405, body: '{"error": "Unexpected HTTP Method"}') end it "raises error" do expect { client.document.all }.to raise_error( Veryfi::Error::UnexpectedHTTPMethod, - "Unexpected HTTP Method" + "405, Unexpected HTTP Method" ) end end context "when server responds with 409 error" do before do - stub_request(:get, /\.*/).to_return(status: 409) + stub_request(:get, /\.*/).to_return(status: 409, body: '{"error": "Access Limit Reached"}') end it "raises error" do expect { client.document.all }.to raise_error( Veryfi::Error::AccessLimitReached, - "Access Limit Reached" + "409, Access Limit Reached" ) end end context "when server responds with 500 error" do before do - stub_request(:get, /\.*/).to_return(status: 500) + stub_request(:get, /\.*/).to_return(status: 500, body: '{"error": "Internal Server Error"}') end it "raises error" do expect { client.document.all }.to raise_error( Veryfi::Error::InternalError, - "Internal Server Error" + "500, Internal Server Error" + ) + end + end + + context "when server responds with empty body" do + before do + stub_request(:get, /\.*/).to_return(status: 501, body: "") + end + + it "raises error" do + expect { client.document.all }.to raise_error( + Veryfi::Error::VeryfiError, + "501" + ) + end + end + + context "when server responds with unknown error and body" do + before do + stub_request(:get, /\.*/).to_return(status: 504, body: '{"error": "Gateway Timeout"}') + end + + it "raises error" do + expect { client.document.all }.to raise_error( + Veryfi::Error::VeryfiError, + "504, Gateway Timeout" ) end end