Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #67 from stephencookdev/feature/EG-3600-region-sup…
Browse files Browse the repository at this point in the history
…port

Region support
  • Loading branch information
stephencookdev authored Nov 8, 2018
2 parents f494455 + 0db9953 commit 72ea8e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
26 changes: 11 additions & 15 deletions lib/onfido/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Onfido
module Configuration
attr_accessor :api_key, :open_timeout, :read_timeout, :api_version
REGION_HOSTS = {
us: "api.us.onfido.com"
}.freeze

attr_accessor :api_key, :region, :open_timeout, :read_timeout, :api_version

def self.extended(base)
base.reset
Expand All @@ -12,6 +16,7 @@ def configure

def reset
self.api_key = nil
self.region = nil
self.open_timeout = 30
self.read_timeout = 80
self.api_version = 'v2'
Expand All @@ -30,22 +35,13 @@ def logger
RestClient.log ||= NullLogger.new
end

def region
return unless api_key

first_bit = api_key.split("_")[0]

return if %w(live test).include?(first_bit)

first_bit
end

def endpoint
if region
"https://api.#{region}.onfido.com/#{api_version}/"
else
"https://api.onfido.com/#{api_version}/"
region_host = region ? REGION_HOSTS[region.downcase.to_sym] : "api.onfido.com"
unless region_host
raise "The region \"#{region.downcase}\" is not currently supported"
end

"https://#{region_host}/#{api_version}/"
end
end
end
2 changes: 1 addition & 1 deletion lib/onfido/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Onfido
VERSION = '0.12.0'.freeze
VERSION = '0.13.0'.freeze
end
11 changes: 6 additions & 5 deletions spec/onfido_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
end
end

describe 'using a US API token' do
describe 'using the US region' do
it 'should change endpoint' do
onfido.api_key = "us_live_asdfghjkl1234567890qwertyuiop"
onfido.region = 'us'
expect(onfido.endpoint).to eq('https://api.us.onfido.com/v2/')
end
end

describe 'using a EU API token' do
describe 'using an unsupported region' do
it 'should change endpoint' do
onfido.api_key = "eu_live_asdfghjkl1234567890qwertyuiop"
expect(onfido.endpoint).to eq('https://api.eu.onfido.com/v2/')
onfido.region = 'de'
expect { onfido.endpoint }.
to raise_error('The region "de" is not currently supported')
end
end

Expand Down

0 comments on commit 72ea8e4

Please sign in to comment.