onfido 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ade11756020fd962ad312d8ebd50cca72b47df9
4
- data.tar.gz: 08d2c3429fb9acff54175a1cb80f9a58fc1357b0
3
+ metadata.gz: c107bba12384d55217719e5283953f0a0111f927
4
+ data.tar.gz: 4d77e8f946b6d035e3b126b4d9b01b3807a4c295
5
5
  SHA512:
6
- metadata.gz: 863ada612067a0e826ead5d97b0eb98043c53bc4fd1d174b00a8b2d173a2ec86a1c3411be7c1721d671131e0c703baa73116a06bdf056158b93f2d8394062fdd
7
- data.tar.gz: 51e5078edb94d2457833aed52f4e4f7b249085f2fe5730d7d743f024c8e01c39cfdd584ad3f0560896958676ac88c607a819acbce589d2be4262f38757e74fa0
6
+ metadata.gz: cef79ba60503372169905e3abf66b2ac4d55b46141a9758abce82cf8a91ed72aa02e6635d7675d7a37280818605b5843f62d47d99a5c72164a850b15282c9559
7
+ data.tar.gz: adc95bedaa0ef4e1d75e3ee87a21a765383d9e39b4f19b0f7779741c1e65c7b514934d243c7644567933bc25139b61f5eee596a29456bf8d58fcd2fef732fec3
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ matrix:
4
+ allow_failures:
5
+ - rvm: jruby
6
+ - rvm: rbx-2
7
+
8
+ rvm:
9
+ - 2.0
10
+ - 2.1
11
+ - 2.2
12
+ - jruby
13
+ - rbx-2
14
+
15
+ script:
16
+ - bundle exec rspec spec
@@ -0,0 +1,3 @@
1
+ ## v0.0.4 10 November 2015
2
+
3
+ - Split out connection errors so they can be automatically retried
data/README.md CHANGED
@@ -3,7 +3,10 @@
3
3
  A wrapper for Onfido's [API](https://onfido.com/documentation#introduction). You should always refer to the documentation for valid API calls.
4
4
 
5
5
  [![Build Status](https://snap-ci.com/hvssle/onfido/branch/master/build_image)](https://snap-ci.com/hvssle/onfido/branch/master)
6
- [![Gem Version](https://badge.fury.io/rb/onfido.svg)](http://badge.fury.io/rb/onfido) [!['gitter room'][2]][1]
6
+ [![Gem Version](https://badge.fury.io/rb/onfido.svg)](http://badge.fury.io/rb/onfido)
7
+ [![Build Status](https://travis-ci.org/hvssle/onfido.svg?branch=master)](https://travis-ci.org/hvssle/onfido)
8
+ [!['gitter room'][2]][1]
9
+
7
10
 
8
11
  [1]: https://gitter.im/hvssle/onfido
9
12
  [2]: https://badges.gitter.im/gitterHQ/developers.png
@@ -27,13 +30,15 @@ Or install it yourself as:
27
30
 
28
31
  ## Usage
29
32
 
30
- There are 3 configuration options for Onfido, including your `api_key`, throwing exceptions for failed responses and logging the requests. By default, `throws_exceptions` is set to `true`. If set to `false`, the response body will be returned instead.
33
+ There are 5 configuration options for Onfido, including your `api_key`, throwing exceptions for failed responses and logging the requests. By default, `throws_exceptions` is set to `true`. If set to `false` and Onfido responds with a valid JSON body the response body will be returned instead.
31
34
 
32
35
  ```ruby
33
36
  Onfido.configure do |config|
34
37
  config.api_key = 'MY_API_KEY'
35
38
  config.logger = Logger.new(STDOUT)
36
39
  config.throws_exceptions = false
40
+ config.open_timeout = 30
41
+ config.read_timeout = 80
37
42
  end
38
43
  ```
39
44
 
@@ -122,13 +127,31 @@ To search for addresses by postcode
122
127
  api.address.all('SE1 4NG')
123
128
  ```
124
129
 
130
+ ### Pagination
131
+
132
+ Currently, you can paginate over the *applicant* and *check* resources. However, since you can only create 1 check per applicant therefore paginating on the check resource might prove impractical.
133
+
134
+ By default, both endpoints are fetching records the first 20 records. That is the maximum amount of records you can request per page.
135
+
136
+ To paginate over *applicants*:
137
+ ```ruby
138
+ api = Onfido::API.new
139
+ api.applicant.all(page: 2, per_page: 10)
140
+ ```
141
+
142
+ To paginate over *checks*:
143
+ ```
144
+ api = Onfido::API.new
145
+ api.check.all('applicant_id', page: 2, per_page: 10)
146
+ ```
147
+
125
148
  ## Error Handling
126
149
 
127
150
  By default, Onfido will attempt to raise errors returned by the API automatically.
128
151
 
129
152
  If you set the `throws_exceptions` to false, it will simply return the response body. This allows you to handle errors manually.
130
153
 
131
- If you rescue Onfido::RequestError, you are provided with the error message itself as well as the type of error, response code and fields that have errored. Here's how you might do that:
154
+ If you rescue `Onfido::RequestError`, you are provided with the error message itself as well as the type of error, response code and fields that have errored. Here's how you might do that:
132
155
 
133
156
  ```ruby
134
157
  def create_applicant
@@ -140,6 +163,8 @@ If you rescue Onfido::RequestError, you are provided with the error message itse
140
163
  end
141
164
  ```
142
165
 
166
+ You can also rescue `Onfido::ConnectionError`, which is raised whenever something goes wrong with the connection to Onfido - you may wish to automatically retry these requests.
167
+
143
168
  ### Roadmap
144
169
 
145
170
  - Improve test coverage with more scenarios
@@ -5,10 +5,9 @@ require 'open-uri'
5
5
 
6
6
  require 'onfido/version'
7
7
  require 'onfido/configuration'
8
- require 'onfido/request_error'
9
- require 'onfido/response_handler'
8
+ require 'onfido/errors/request_error'
9
+ require 'onfido/errors/connection_error'
10
10
  require 'onfido/null_logger'
11
- require 'onfido/requestable'
12
11
  require 'onfido/resource'
13
12
  require 'onfido/address'
14
13
  require 'onfido/applicant'
@@ -1,6 +1,6 @@
1
1
  module Onfido
2
2
  module Configuration
3
- attr_accessor :api_key, :throws_exceptions
3
+ attr_accessor :api_key, :throws_exceptions, :open_timeout, :read_timeout
4
4
 
5
5
  def self.extended(base)
6
6
  base.reset
@@ -13,6 +13,8 @@ module Onfido
13
13
  def reset
14
14
  self.api_key = nil
15
15
  self.throws_exceptions = true
16
+ self.open_timeout = 30
17
+ self.read_timeout = 80
16
18
  RestClient.log = nil
17
19
  end
18
20
 
@@ -0,0 +1,4 @@
1
+ module Onfido
2
+ class ConnectionError < StandardError
3
+ end
4
+ end
@@ -19,5 +19,13 @@ specified in the Onfido documentation e.g.
19
19
  module Onfido
20
20
  class RequestError < StandardError
21
21
  attr_accessor :type, :fields, :response_code
22
+
23
+ def initialize(message=nil, type: nil, fields: nil, response_code: nil)
24
+ @type = type
25
+ @fields = fields
26
+ @response_code = response_code
27
+
28
+ super(message)
29
+ end
22
30
  end
23
31
  end
@@ -2,26 +2,53 @@ module Onfido
2
2
  class Resource
3
3
  VALID_HTTP_METHODS = %i(get post)
4
4
 
5
- include Requestable
6
-
7
5
  def url_for(path)
8
6
  Onfido.endpoint + path
9
7
  end
10
8
 
11
- def method_missing(method, *args)
12
- if VALID_HTTP_METHODS.include?(method.to_sym)
9
+ VALID_HTTP_METHODS.each do |method|
10
+ define_method method do |*args|
13
11
  make_request(
12
+ method: method.to_sym,
14
13
  url: args.first.fetch(:url),
15
- payload: build_query(args.first.fetch(:payload)),
16
- method: method.to_sym
14
+ payload: build_query(args.first.fetch(:payload))
17
15
  )
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def make_request(options)
22
+ url = options.fetch(:url)
23
+ payload = options.fetch(:payload)
24
+ method = options.fetch(:method)
25
+
26
+ request_options = {
27
+ url: url,
28
+ payload: payload,
29
+ method: method,
30
+ headers: headers,
31
+ open_timeout: Onfido.open_timeout,
32
+ timeout: Onfido.read_timeout
33
+ }
34
+
35
+ response = RestClient::Request.execute(request_options)
36
+
37
+ parse(response)
38
+ rescue RestClient::ExceptionWithResponse => e
39
+ if e.response
40
+ handle_api_error(e.response)
18
41
  else
19
- super
42
+ handle_restclient_error(e, url)
20
43
  end
44
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
45
+ handle_restclient_error(e, url)
21
46
  end
22
47
 
23
- def respond_to_missing?(method, include_private = false)
24
- VALID_HTTP_METHODS.include?(method.to_sym) || super
48
+ def parse(response)
49
+ JSON.parse(response.body)
50
+ rescue JSON::ParserError
51
+ general_api_error(response.code, response.body)
25
52
  end
26
53
 
27
54
  def headers
@@ -31,8 +58,6 @@ module Onfido
31
58
  }
32
59
  end
33
60
 
34
- private
35
-
36
61
  # There seems to be a serialization issue with the HTTP client
37
62
  # which does not serialize the payload properly.
38
63
  # Have a look here https://gist.github.com/PericlesTheo/cb35139c57107ab3c84a
@@ -44,5 +69,66 @@ module Onfido
44
69
  Rack::Utils.build_nested_query(payload)
45
70
  end
46
71
  end
72
+
73
+ def handle_api_error(response)
74
+ parsed_response = parse(response)
75
+
76
+ unless parsed_response["error"]
77
+ general_api_error(response.code, response.body)
78
+ end
79
+
80
+ if Onfido.throws_exceptions
81
+ raise RequestError.new(
82
+ parsed_response["error"]['message'],
83
+ type: parsed_response["error"]["type"],
84
+ fields: parsed_response["error"]["fields"],
85
+ response_code: response.code)
86
+ else
87
+ parsed_response
88
+ end
89
+ end
90
+
91
+ def general_api_error(response_code, response_body)
92
+ raise RequestError.new(
93
+ "Invalid response object from API: #{response_body} " \
94
+ "(HTTP response code was #{response_code})",
95
+ response_code: response_code
96
+ )
97
+ end
98
+
99
+ def handle_restclient_error(e, url)
100
+ connection_message =
101
+ "Please check your internet connection and try again. " \
102
+ "If this problem persists, you should let us know at info@onfido.com."
103
+
104
+ message =
105
+ case e
106
+ when RestClient::RequestTimeout
107
+ "Could not connect to Onfido (#{url}). #{connection_message}"
108
+
109
+ when RestClient::ServerBrokeConnection
110
+ "The connection to the server (#{url}) broke before the " \
111
+ "request completed. #{connection_message}"
112
+
113
+ when RestClient::SSLCertificateNotVerified
114
+ "Could not verify Onfido's SSL certificate. Please make sure " \
115
+ "that your network is not intercepting certificates. " \
116
+ "(Try going to #{Onfido.endpoint} in your browser.) " \
117
+ "If this problem persists, let us know at info@onfido.com."
118
+
119
+ when SocketError
120
+ "Unexpected error when trying to connect to Onfido. " \
121
+ "You may be seeing this message because your DNS is not working. " \
122
+ "To check, try running 'host onfido.com' from the command line."
123
+
124
+ else
125
+ "Unexpected error communicating with Onfido. " \
126
+ "If this problem persists, let us know at info@onfido.com."
127
+ end
128
+
129
+ full_message = message + "\n\n(Network error: #{e.message})"
130
+
131
+ raise ConnectionError.new(full_message)
132
+ end
47
133
  end
48
134
  end
@@ -1,3 +1,3 @@
1
1
  module Onfido
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.7'
22
22
  spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'webmock', '~> 1.22'
23
24
 
24
25
  spec.add_dependency 'rest-client', '~> 1.8.0'
25
26
  end
@@ -0,0 +1,116 @@
1
+ describe Onfido::Resource do
2
+ let(:resource) { described_class.new }
3
+ let(:url) { Onfido.endpoint + path }
4
+ let(:api_key) { 'some_key' }
5
+ let(:payload) { { postcode: 'SE1 4NG' } }
6
+
7
+
8
+ before do
9
+ allow(Onfido).to receive(:api_key).and_return(api_key)
10
+ end
11
+
12
+ context '4xx response' do
13
+ let(:path) { '4xx_response' }
14
+
15
+ context "when 'throws_exceptions' is true" do
16
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(true) }
17
+
18
+ it 'raises a custom error' do
19
+ expect { resource.get({ url: url, payload: payload }) }.
20
+ to raise_error(Onfido::RequestError, 'Something went wrong')
21
+ end
22
+ end
23
+
24
+ context "when 'throws_exceptions' is false" do
25
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(false) }
26
+
27
+ it 'returns the body as a hash' do
28
+ response = resource.get({ url: url, payload: payload })
29
+ expect(response['error']).not_to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ context 'unexpected error format' do
35
+ let(:path) { 'unexpected_error_format' }
36
+
37
+ context "when 'throws_exceptions' is true" do
38
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(true) }
39
+
40
+ it 'raises a custom error' do
41
+ expect { resource.get({ url: url, payload: payload }) }.
42
+ to raise_error(Onfido::RequestError, /response code was 400/)
43
+ end
44
+ end
45
+
46
+ context "when 'throws_exceptions' is false" do
47
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(false) }
48
+
49
+ it 'still raises a custom error' do
50
+ expect { resource.get({ url: url, payload: payload }) }.
51
+ to raise_error(Onfido::RequestError, /response code was 400/)
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'unparseable JSON' do
57
+ let(:path) { 'unparseable_response' }
58
+
59
+ context "when 'throws_exceptions' is true" do
60
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(true) }
61
+
62
+ it 'raises a custom error' do
63
+ expect { resource.get({ url: url, payload: payload }) }.
64
+ to raise_error(Onfido::RequestError, /response code was 504/)
65
+ end
66
+ end
67
+
68
+ context "when 'throws_exceptions' is false" do
69
+ before { allow(Onfido).to receive(:throws_exceptions).and_return(false) }
70
+
71
+ it 'still raises a custom error' do
72
+ expect { resource.get({ url: url, payload: payload }) }.
73
+ to raise_error(Onfido::RequestError, /response code was 504/)
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'timeout' do
79
+ before do
80
+ allow(RestClient::Request).
81
+ to receive(:execute).
82
+ and_raise(RestClient::RequestTimeout)
83
+ end
84
+
85
+ it 'raises a ConnectionError' do
86
+ expect { resource.get({ url: Onfido.endpoint, payload: payload }) }.
87
+ to raise_error(Onfido::ConnectionError, /Could not connect/)
88
+ end
89
+ end
90
+
91
+ context 'broken connection' do
92
+ before do
93
+ allow(RestClient::Request).
94
+ to receive(:execute).
95
+ and_raise(RestClient::ServerBrokeConnection)
96
+ end
97
+
98
+ it 'raises a ConnectionError' do
99
+ expect { resource.get({ url: Onfido.endpoint, payload: payload }) }.
100
+ to raise_error(Onfido::ConnectionError, /connection to the server/)
101
+ end
102
+ end
103
+
104
+ context "bad SSL certificate" do
105
+ before do
106
+ allow(RestClient::Request).
107
+ to receive(:execute).
108
+ and_raise(RestClient::SSLCertificateNotVerified.new(nil))
109
+ end
110
+
111
+ it 'raises a ConnectionError' do
112
+ expect { resource.get({ url: Onfido.endpoint, payload: payload }) }.
113
+ to raise_error(Onfido::ConnectionError, /SSL certificate/)
114
+ end
115
+ end
116
+ end
@@ -25,7 +25,9 @@ describe Onfido::Resource do
25
25
  end
26
26
  end
27
27
  end
28
+ end
28
29
 
30
+ describe "valid http methods" do
29
31
  %i(get post).each do |method|
30
32
  context "for supported HTTP method: #{method}" do
31
33
  let(:url) { endpoint + path }
@@ -49,13 +51,18 @@ describe Onfido::Resource do
49
51
  url: url,
50
52
  payload: Rack::Utils.build_query(payload),
51
53
  method: method,
52
- headers: resource.headers
53
- )
54
- .and_return(response)
54
+ headers: resource.send(:headers),
55
+ open_timeout: 30,
56
+ timeout: 80
57
+ ).and_call_original
58
+
59
+ WebMock.stub_request(method, url).
60
+ to_return(body: response.to_json, status: 200)
55
61
  end
56
62
 
57
63
  it 'makes a request to an endpoint' do
58
- expect(resource.public_send(method, {url: url, payload: payload})).to eq(response)
64
+ expect(resource.public_send(method, {url: url, payload: payload})).
65
+ to eq(response)
59
66
  end
60
67
  end
61
68
  end
@@ -47,6 +47,16 @@ class FakeOnfidoAPI < Sinatra::Base
47
47
  json_response(422, '4xx_response.json')
48
48
  end
49
49
 
50
+ get '/v1/unexpected_error_format' do
51
+ json_response(400, 'unexpected_error_format.json')
52
+ end
53
+
54
+ get '/v1/unparseable_response' do
55
+ content_type :json
56
+ status 504
57
+ ''
58
+ end
59
+
50
60
  private
51
61
 
52
62
  def json_response(response_code, file_name)
@@ -0,0 +1,3 @@
1
+ {
2
+ "unexpected": "format"
3
+ }
metadata CHANGED
@@ -1,55 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onfido
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pericles Theodorou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.22'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.22'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rest-client
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ~>
46
60
  - !ruby/object:Gem::Version
47
61
  version: 1.8.0
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ~>
53
67
  - !ruby/object:Gem::Version
54
68
  version: 1.8.0
55
69
  description: A wrapper for Onfido API 1.0
@@ -59,9 +73,10 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".ruby-version"
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - CHANGELOG.md
65
80
  - Gemfile
66
81
  - LICENSE
67
82
  - LICENSE.txt
@@ -74,23 +89,21 @@ files:
74
89
  - lib/onfido/check.rb
75
90
  - lib/onfido/configuration.rb
76
91
  - lib/onfido/document.rb
92
+ - lib/onfido/errors/connection_error.rb
93
+ - lib/onfido/errors/request_error.rb
77
94
  - lib/onfido/null_logger.rb
78
95
  - lib/onfido/report.rb
79
- - lib/onfido/request_error.rb
80
- - lib/onfido/requestable.rb
81
96
  - lib/onfido/resource.rb
82
- - lib/onfido/response_handler.rb
83
97
  - lib/onfido/version.rb
84
98
  - onfido.gemspec
85
- - spec/integrations/4xx_response_spec.rb
86
99
  - spec/integrations/address_spec.rb
87
100
  - spec/integrations/applicant_spec.rb
88
101
  - spec/integrations/check_spec.rb
89
102
  - spec/integrations/document_spec.rb
103
+ - spec/integrations/exceptions_spec.rb
90
104
  - spec/integrations/report_spec.rb
91
105
  - spec/onfido/request_error_spec.rb
92
106
  - spec/onfido/resource_spec.rb
93
- - spec/onfido/response_handler_spec.rb
94
107
  - spec/onfido_spec.rb
95
108
  - spec/spec_helper.rb
96
109
  - spec/support/fake_onfido_api.rb
@@ -103,6 +116,7 @@ files:
103
116
  - spec/support/fixtures/document.json
104
117
  - spec/support/fixtures/report.json
105
118
  - spec/support/fixtures/reports.json
119
+ - spec/support/fixtures/unexpected_error_format.json
106
120
  homepage: http://github.com/hvssle/onfido
107
121
  licenses:
108
122
  - MIT
@@ -113,12 +127,12 @@ require_paths:
113
127
  - lib
114
128
  required_ruby_version: !ruby/object:Gem::Requirement
115
129
  requirements:
116
- - - ">="
130
+ - - '>='
117
131
  - !ruby/object:Gem::Version
118
132
  version: '0'
119
133
  required_rubygems_version: !ruby/object:Gem::Requirement
120
134
  requirements:
121
- - - ">="
135
+ - - '>='
122
136
  - !ruby/object:Gem::Version
123
137
  version: '0'
124
138
  requirements: []
@@ -128,15 +142,14 @@ signing_key:
128
142
  specification_version: 4
129
143
  summary: A wrapper for Onfido API 1.0
130
144
  test_files:
131
- - spec/integrations/4xx_response_spec.rb
132
145
  - spec/integrations/address_spec.rb
133
146
  - spec/integrations/applicant_spec.rb
134
147
  - spec/integrations/check_spec.rb
135
148
  - spec/integrations/document_spec.rb
149
+ - spec/integrations/exceptions_spec.rb
136
150
  - spec/integrations/report_spec.rb
137
151
  - spec/onfido/request_error_spec.rb
138
152
  - spec/onfido/resource_spec.rb
139
- - spec/onfido/response_handler_spec.rb
140
153
  - spec/onfido_spec.rb
141
154
  - spec/spec_helper.rb
142
155
  - spec/support/fake_onfido_api.rb
@@ -149,3 +162,4 @@ test_files:
149
162
  - spec/support/fixtures/document.json
150
163
  - spec/support/fixtures/report.json
151
164
  - spec/support/fixtures/reports.json
165
+ - spec/support/fixtures/unexpected_error_format.json
@@ -1 +0,0 @@
1
- 2.1.3
@@ -1,12 +0,0 @@
1
- module Onfido
2
- module Requestable
3
- def make_request(options)
4
- url = options.fetch(:url)
5
- payload = options.fetch(:payload)
6
- method = options.fetch(:method)
7
- RestClient::Request.execute(url: url, payload: payload, method: method, headers: headers) do |response, *|
8
- ResponseHandler.new(response).parse!
9
- end
10
- end
11
- end
12
- end
@@ -1,38 +0,0 @@
1
- module Onfido
2
- class ResponseHandler
3
- attr_reader :response
4
-
5
- def initialize(response)
6
- @response = response
7
- end
8
-
9
- def parse!
10
- if Onfido.throws_exceptions && parsed_response["error"]
11
- raise request_error
12
- else
13
- parsed_response
14
- end
15
- end
16
-
17
- private
18
-
19
- def parsed_response
20
- @parsed_response ||=
21
- begin
22
- JSON.parse(response)
23
- rescue JSON::ParserError
24
- {
25
- 'error' => {"message" => "Unparseable response: #{response}"}
26
- }
27
- end
28
- end
29
-
30
- def request_error
31
- RequestError.new(parsed_response['error']['message']).tap do |error|
32
- error.type = parsed_response['error']["type"]
33
- error.fields = parsed_response['error']["fields"]
34
- error.response_code = response.code if response.respond_to?(:code)
35
- end
36
- end
37
- end
38
- end
@@ -1,35 +0,0 @@
1
- describe Onfido::Resource do
2
- let(:resource) { described_class.new }
3
- let(:url) { Onfido.endpoint + path }
4
- let(:api_key) { 'some_key' }
5
- let(:payload) { {postcode: 'SE1 4NG'} }
6
-
7
-
8
- before do
9
- allow(Onfido).to receive(:api_key).and_return(api_key)
10
- end
11
-
12
- context 'when a response is a 4xx' do
13
- let(:path) { '4xx_response' }
14
-
15
- context "when 'throws_exceptions' is true" do
16
- it 'raises a custom error' do
17
- expect {
18
- resource.get({url: url, payload: payload})
19
- }.to raise_error(
20
- Onfido::RequestError, 'Something went wrong')
21
- end
22
- end
23
-
24
- context "when 'throws_exceptions' is false" do
25
- before do
26
- allow(Onfido).to receive(:throws_exceptions).and_return(false)
27
- end
28
-
29
- it 'returns the body as a hash' do
30
- response = resource.get({url: url, payload: payload})
31
- expect(response['error']).not_to be_nil
32
- end
33
- end
34
- end
35
- end
@@ -1,79 +0,0 @@
1
- describe Onfido::ResponseHandler do
2
- subject(:handler) { described_class.new(response) }
3
-
4
- describe '#parse!' do
5
- before do
6
- allow(Onfido).to receive(:throws_exceptions).and_return(throws_exceptions)
7
- end
8
-
9
- let(:throws_exceptions) { true }
10
-
11
- context 'when the response is successful' do
12
- let(:response) { hashified_response.to_json }
13
- let(:hashified_response) do
14
- {'success' => 'applicant was successfully created'}
15
- end
16
-
17
- it 'parses the body and returns it' do
18
- expect(handler.parse!).to eq(hashified_response)
19
- end
20
- end
21
-
22
- context 'when the response has gracefully errored' do
23
- let(:response) { hashified_response.to_json }
24
- let(:hashified_response) do
25
- {
26
- 'error' => {
27
- 'message' => 'Authorization error: please re-check your credentials'
28
- }
29
- }
30
- end
31
-
32
- context 'when throw_exceptions configuration is set to true' do
33
- it 'raises an error' do
34
- expect { handler.parse! }.to raise_error(
35
- Onfido::RequestError,
36
- 'Authorization error: please re-check your credentials'
37
- )
38
- end
39
- end
40
-
41
- context 'when throw_exceptions configuration is set to false' do
42
- let(:throws_exceptions) { false }
43
-
44
-
45
- it 'parses the body and returns it' do
46
- expect(handler.parse!).to eq(hashified_response)
47
- end
48
- end
49
- end
50
-
51
- context 'when the response is unparseable' do
52
- let(:response) { 'something : wrong' }
53
-
54
- context 'when throw_exceptions configuration is set to true' do
55
- it 'raises an error' do
56
- expect { handler.parse! }.to raise_error(
57
- Onfido::RequestError,
58
- 'Unparseable response: something : wrong'
59
- )
60
- end
61
- end
62
-
63
- context 'when throw_exceptions configuration is set to false' do
64
- let(:throws_exceptions) { false }
65
-
66
-
67
- it 'parses the body and returns it' do
68
- expect(handler.parse!).to eq(
69
- {
70
- 'error' => {
71
- 'message' => 'Unparseable response: something : wrong'
72
- }
73
- }
74
- )
75
- end
76
- end
77
- end
78
- end
79
- end