onfido 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c63f7c25eb6a4d6a774be396edf561175a9ab3f1
4
- data.tar.gz: b1dadefd0aaf4971077671d52b816b699e7217f1
3
+ metadata.gz: dc0898839314c34c40e417562dee6591d2972a96
4
+ data.tar.gz: 587d19739d05918ce9346af1a8adef01a8703207
5
5
  SHA512:
6
- metadata.gz: 77a4f0fe6dbd99bb4c93c57cfb8bce13a2c4172fc767cdb446cb6694357fbef51b639cd1cfc7cd3120b2890634b701a249704dcfc93333e3c39fcf30724a1c32
7
- data.tar.gz: ea7725d553e7ad385c24d8ba1e52ba3b5b08b9bc10c11ac871de5df87d3c8f066b976b7f82efbde071eb84d494a2dd3e79ae2f184730da8a7b1297c1963893e2
6
+ metadata.gz: 9c5e9a7b81593a84b9dc420aed0e07436d49950b3424dde6daa53875c9a41e2d55a3640ccaebf6904d2ca51d9d809d09e9fb20429e0613fd0bb5199863642ab1
7
+ data.tar.gz: e554fbca86a69f397d14fb6d82b7cefab9f524a96c27623823bae25d2e40bf9c1609b26a5739836d6b4a366ced3824660e31193b93afa035afa6665d67021326
@@ -1,3 +1,10 @@
1
+ ## v0.7.0, 8 August 2016
2
+
3
+ - Add support for dynamic API keys
4
+ (see https://github.com/hvssle/onfido#usage)
5
+ - Fix `Onfido::Resource` so it raises `Onfido::ConnectionError` on
6
+ HTTP Error 408 Request Timeout
7
+
1
8
  ## v0.6.1, 15 July 2016
2
9
 
3
10
  - Fix `Onfido::API.live_photo` so it returns an `Onfido::LivePhoto` instance
data/README.md CHANGED
@@ -37,6 +37,12 @@ You can make API calls by using an instance of the `API` class:
37
37
  api = Onfido::API.new
38
38
  ```
39
39
 
40
+ Alternatively, you can set an API key here instead of in the initializer:
41
+
42
+ ```ruby
43
+ api = Onfido::API.new(api_key: 'API_KEY')
44
+ ```
45
+
40
46
  ### Resources
41
47
 
42
48
  All resources share the same interface when making API calls. Use `.create` to create a resource, `.find` to find one, and `.all` to fetch all resources.
@@ -1,8 +1,12 @@
1
1
  module Onfido
2
2
  class API
3
+ def initialize(options = {})
4
+ @api_key = options[:api_key]
5
+ end
6
+
3
7
  def method_missing(method, *args)
4
8
  klass = method.to_s.split('_').collect(&:capitalize).join
5
- Object.const_get("Onfido::#{klass}").new
9
+ Object.const_get("Onfido::#{klass}").new(@api_key)
6
10
  rescue NameError
7
11
  super
8
12
  end
@@ -1,6 +1,11 @@
1
1
  module Onfido
2
2
  class Resource
3
3
  VALID_HTTP_METHODS = %i(get post).freeze
4
+ REQUEST_TIMEOUT_HTTP_CODE = 408
5
+
6
+ def initialize(api_key = nil)
7
+ @api_key = api_key || Onfido.api_key
8
+ end
4
9
 
5
10
  def url_for(path)
6
11
  Onfido.endpoint + path
@@ -36,7 +41,7 @@ module Onfido
36
41
 
37
42
  parse(response)
38
43
  rescue RestClient::ExceptionWithResponse => error
39
- if error.response
44
+ if error.response && !timeout_response?(error.response)
40
45
  handle_api_error(error.response)
41
46
  else
42
47
  handle_restclient_error(error, url)
@@ -51,9 +56,13 @@ module Onfido
51
56
  general_api_error(response.code, response.body)
52
57
  end
53
58
 
59
+ def timeout_response?(response)
60
+ response.code.to_i == REQUEST_TIMEOUT_HTTP_CODE
61
+ end
62
+
54
63
  def headers
55
64
  {
56
- 'Authorization' => "Token token=#{Onfido.api_key}",
65
+ 'Authorization' => "Token token=#{@api_key}",
57
66
  'Accept' => "application/json"
58
67
  }
59
68
  end
@@ -1,3 +1,3 @@
1
1
  module Onfido
2
- VERSION = '0.6.1'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
@@ -12,4 +12,22 @@ describe Onfido::API do
12
12
  describe 'given an unknown resource' do
13
13
  specify { expect { api.blood_test }.to raise_error(NameError) }
14
14
  end
15
+
16
+ describe 'given no API key' do
17
+ it 'uses nil for the resource API key' do
18
+ expect(Onfido::Address).to receive(:new).with(nil)
19
+ api.address
20
+ end
21
+ end
22
+
23
+ describe 'given an API key' do
24
+ let(:api_key) { 'some_key' }
25
+
26
+ subject(:api) { described_class.new(api_key: api_key) }
27
+
28
+ it 'uses that key to create the resource' do
29
+ expect(Onfido::Address).to receive(:new).with(api_key)
30
+ api.address
31
+ end
32
+ end
15
33
  end
@@ -1,8 +1,26 @@
1
+ require 'onfido/errors/connection_error'
2
+
1
3
  describe Onfido::Resource do
2
4
  subject(:resource) { described_class.new }
5
+
3
6
  let(:endpoint) { 'https://api.onfido.com/v2/' }
4
- let(:path) { 'addresses/pick' }
5
- let(:api_key) { 'some_key' }
7
+ let(:path) { 'addresses/pick' }
8
+ let(:url) { endpoint + path }
9
+ let(:payload) { { postcode: 'SE1 4NG' } }
10
+ let(:api_key) { 'some_key' }
11
+
12
+ let(:response) do
13
+ {
14
+ 'addresses' => [
15
+ {
16
+ 'street' => 'Main Street',
17
+ 'town' => 'London',
18
+ 'postcode' => 'SW4 6EH',
19
+ 'country' => 'GBR'
20
+ }
21
+ ]
22
+ }
23
+ end
6
24
 
7
25
  before { allow(Onfido).to receive(:endpoint).and_return(endpoint) }
8
26
  before { allow(Onfido).to receive(:api_key).and_return(api_key) }
@@ -25,42 +43,88 @@ describe Onfido::Resource do
25
43
  end
26
44
  end
27
45
 
46
+ describe "API key" do
47
+ subject(:resource) { described_class.new(specific_api_key) }
48
+
49
+ before do
50
+ expect(RestClient::Request).to receive(:execute).
51
+ with(
52
+ url: url,
53
+ payload: Rack::Utils.build_query(payload),
54
+ method: :get,
55
+ headers: resource.send(:headers),
56
+ open_timeout: 30,
57
+ timeout: 80
58
+ ).and_call_original
59
+
60
+ WebMock.stub_request(:get, url).
61
+ to_return(body: response.to_json, status: 200)
62
+ end
63
+
64
+ context "when using a specific key" do
65
+ let(:specific_api_key) { "specific_key" }
66
+
67
+ it "uses that key when making the request" do
68
+ resource.get(url: url, payload: payload)
69
+
70
+ expect(WebMock).to have_requested(:get, url).with(headers: {
71
+ 'Authorization' => "Token token=#{specific_api_key}",
72
+ 'Accept' => "application/json"
73
+ })
74
+ end
75
+ end
76
+
77
+ context "when not using a specific key" do
78
+ let(:specific_api_key) { nil }
79
+
80
+ it "uses the general config key when making the request" do
81
+ resource.get(url: url, payload: payload)
82
+
83
+ expect(WebMock).to have_requested(:get, url).with(headers: {
84
+ 'Authorization' => "Token token=#{api_key}",
85
+ 'Accept' => "application/json"
86
+ })
87
+ end
88
+ end
89
+ end
90
+
28
91
  describe "valid http methods" do
29
92
  %i(get post).each do |method|
30
93
  context "for supported HTTP method: #{method}" do
31
- let(:url) { endpoint + path }
32
- let(:payload) { { postcode: 'SE1 4NG' } }
33
- let(:response) do
34
- {
35
- 'addresses' => [
36
- {
37
- 'street' => 'Main Street',
38
- 'town' => 'London',
39
- 'postcode' => 'SW4 6EH',
40
- 'country' => 'GBR'
41
- }
42
- ]
43
- }
44
- end
94
+ context "with a success response" do
95
+ before do
96
+ expect(RestClient::Request).to receive(:execute).
97
+ with(
98
+ url: url,
99
+ payload: Rack::Utils.build_query(payload),
100
+ method: method,
101
+ headers: resource.send(:headers),
102
+ open_timeout: 30,
103
+ timeout: 80
104
+ ).and_call_original
45
105
 
46
- before do
47
- expect(RestClient::Request).to receive(:execute).
48
- with(
49
- url: url,
50
- payload: Rack::Utils.build_query(payload),
51
- method: method,
52
- headers: resource.send(:headers),
53
- open_timeout: 30,
54
- timeout: 80
55
- ).and_call_original
56
-
57
- WebMock.stub_request(method, url).
58
- to_return(body: response.to_json, status: 200)
106
+ WebMock.stub_request(method, url).
107
+ to_return(body: response.to_json, status: 200)
108
+ end
109
+
110
+ it 'makes a request to an endpoint' do
111
+ expect(resource.public_send(method, url: url, payload: payload)).
112
+ to eq(response)
113
+ end
59
114
  end
60
115
 
61
- it 'makes a request to an endpoint' do
62
- expect(resource.public_send(method, url: url, payload: payload)).
63
- to eq(response)
116
+ context "with a timeout error response" do
117
+ before do
118
+ allow_any_instance_of(RestClient::ExceptionWithResponse).
119
+ to receive(:response).and_return(double(body: "", code: "408"))
120
+ expect(RestClient::Request).to receive(:execute).
121
+ and_raise(RestClient::ExceptionWithResponse)
122
+ end
123
+
124
+ it "raises a ConnectionError" do
125
+ expect { resource.public_send(method, url: url, payload: payload) }.
126
+ to raise_error(Onfido::ConnectionError)
127
+ end
64
128
  end
65
129
  end
66
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onfido
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pericles Theodorou
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-15 00:00:00.000000000 Z
12
+ date: 2016-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler