Skip to content

Commit

Permalink
Merge pull request #2 from yellowme/fix/capture-html-errors
Browse files Browse the repository at this point in the history
Fix/capture html errors
  • Loading branch information
jerolan authored Feb 4, 2020
2 parents 1b199d7 + aa1b255 commit 5dfacf9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
xhash_client (0.3.2)
xhash_client (0.3.3)
httparty (~> 0.16.0)
json (~> 2.0)

Expand Down
21 changes: 19 additions & 2 deletions lib/xhash/client/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})
Expand All @@ -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: {})
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/xhash/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ def initialize(options = {})
class MissingRequiredFieldError < Error; end

class InvalidFieldError < Error; end

class MalformedResponse < Error; end
end
2 changes: 1 addition & 1 deletion lib/xhash/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Xhash
VERSION = '0.3.2'
VERSION = '0.3.3'
end
13 changes: 13 additions & 0 deletions spec/xhash/database_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<p>bad</p>', 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'

Expand Down
9 changes: 6 additions & 3 deletions spec/xhash/ocr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 5dfacf9

Please sign in to comment.