diff --git a/Gemfile.lock b/Gemfile.lock index 09ef113..337aaca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - xhash_client (0.3.2) + xhash_client (0.3.3) httparty (~> 0.16.0) json (~> 2.0) diff --git a/lib/xhash/client/json_api.rb b/lib/xhash/client/json_api.rb index 55e5ed0..50d6027 100644 --- a/lib/xhash/client/json_api.rb +++ b/lib/xhash/client/json_api.rb @@ -13,7 +13,13 @@ def api_get(url:, headers: {}) custom_headers = headers.merge(default_headers) response = HTTParty.get(Xhash.api_base + url, headers: custom_headers) - JSON.parse(response.body, symbolize_names: true) + raise Xhash::Error.new(response) unless response_ok?(response) + + begin + JSON.parse(response.body, symbolize_names: true) + rescue => exception + raise Xhash::MalformedResponse.new + end end def api_post(url:, body: {}, headers: {}) @@ -25,7 +31,13 @@ def api_post(url:, body: {}, headers: {}) body: body.to_json, headers: custom_headers ) - JSON.parse(response.body, symbolize_names: true) + raise Xhash::Error.new(response) unless response_ok?(response) + + begin + JSON.parse(response.body, symbolize_names: true) + rescue => exception + raise Xhash::MalformedResponse.new + end end def api_post_multipart(url:, body: {}, headers: {}) @@ -37,8 +49,13 @@ def api_post_multipart(url:, body: {}, headers: {}) multipart: true, body: body, headers: custom_headers ) + raise Xhash::Error.new(response) unless response_ok?(response) response.body end + + def response_ok?(response) + !(response.code == 404 || response.code >= 500) + end end def self.included(base) diff --git a/lib/xhash/error.rb b/lib/xhash/error.rb index 9646eeb..a819153 100644 --- a/lib/xhash/error.rb +++ b/lib/xhash/error.rb @@ -12,4 +12,6 @@ def initialize(options = {}) class MissingRequiredFieldError < Error; end class InvalidFieldError < Error; end + + class MalformedResponse < Error; end end diff --git a/lib/xhash/version.rb b/lib/xhash/version.rb index 246de65..36a097e 100644 --- a/lib/xhash/version.rb +++ b/lib/xhash/version.rb @@ -1,3 +1,3 @@ module Xhash - VERSION = '0.3.2' + VERSION = '0.3.3' end diff --git a/spec/xhash/database_lookup_spec.rb b/spec/xhash/database_lookup_spec.rb index b7c3b90..b235073 100644 --- a/spec/xhash/database_lookup_spec.rb +++ b/spec/xhash/database_lookup_spec.rb @@ -76,6 +76,19 @@ end end + it 'fails with html error' do + curp = 'JIQJ890611HDFXNN04' + + stub_request(:post, 'https://xhash.dev/api/database-lookup/renapo') + .to_return(body: '

bad

', status: 200) + + begin + Xhash::DatabaseLookup.renapo(curp) + rescue => exception + expect(exception).to be_a(Xhash::MalformedResponse) + end + end + it 'fails with malformed CURP' do curp = 'asdf' diff --git a/spec/xhash/ocr_spec.rb b/spec/xhash/ocr_spec.rb index 1e06eb1..3815ada 100644 --- a/spec/xhash/ocr_spec.rb +++ b/spec/xhash/ocr_spec.rb @@ -88,7 +88,8 @@ payload: { full_name: 'Prof Francesco Reichert', neighborhood: 'Parkerchester', - full_address: 'CLL 59 E DIAGONAL 623\n76 Y 74 A Y RED CANALIZA\nLAS AMERICAS\nMERIDA, MERIDA YU\nC.P. 97302-CR -97111', + full_address: + 'CLL 59 E DIAGONAL 623\n76 Y 74 A Y RED CANALIZA\nLAS AMERICAS\nMERIDA, MERIDA YU\nC.P. 97302-CR -97111', zip_code: '97302', province: 'Port Rosemarieview,VT', date: '2019-09-01', @@ -112,7 +113,9 @@ expect(proof_of_address.type).to eq('TELMEX') expect(proof_of_address.date).to eq('2019-09-01') expect(proof_of_address.zip_code).to eq('97302') - expect(proof_of_address.full_address).to eq('CLL 59 E DIAGONAL 623\n76 Y 74 A Y RED CANALIZA\nLAS AMERICAS\nMERIDA, MERIDA YU\nC.P. 97302-CR -97111') + expect(proof_of_address.full_address).to eq( + 'CLL 59 E DIAGONAL 623\n76 Y 74 A Y RED CANALIZA\nLAS AMERICAS\nMERIDA, MERIDA YU\nC.P. 97302-CR -97111' + ) end it 'successfully serialize proof of address to document by file' do @@ -158,7 +161,7 @@ 'https://kyc-xhash.s3-us-west-2.amazonaws.com/documents/7cd6994d9ad52e8943be1ae00bac60c461430cdf2af6159afa4b9be749706472.png' ) rescue => exception - expect(exception).to be_a(JSON::ParserError) + expect(exception).to be_a(Xhash::MalformedResponse) end end