Created
November 6, 2018 14:46
-
-
Save reggieb/867a1b56bfb68a49ef8303cd7755bdba to your computer and use it in GitHub Desktop.
Example of service to gather data from TrueLayer with token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TrueLayer | |
class IdentifyApplicant | |
TRUE_LAYER_URL = 'https://api.truelayer.com'.freeze | |
attr_reader :token, :error | |
def initialize(token) | |
@token = token | |
end | |
def emails | |
user_data[:emails] | |
end | |
def user_data | |
results[:results].first | |
end | |
def results | |
JSON.parse(response.body).deep_symbolize_keys | |
end | |
def response | |
@response ||= conn.get '/data/v1/info'.freeze | |
end | |
def conn | |
@conn ||= Faraday.new(url: TRUE_LAYER_URL) do |conn| | |
conn.authorization :Bearer, token | |
conn.response :logger if Rails.env.development? | |
conn.adapter Faraday.default_adapter | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
module TrueLayer | |
RSpec.describe IdentifyApplicant do | |
let(:token) { SecureRandom.uuid } | |
let(:address) { build :address } | |
let(:applicant) { build :applicant } | |
subject { described_class.new(token) } | |
let(:user_data) do | |
{ | |
results: [ | |
{ | |
addresses: [ | |
{ | |
address: "#{address.address_line_one} #{address.address_line_two}", | |
city: address.city, | |
country: 'GB', | |
zip: address.postcode | |
} | |
], | |
emails: [applicant.email], | |
full_name: "#{applicant.first_name} #{applicant.last_name}", | |
phones: ['+14151234567'], | |
update_timestamp: '0001-01-01T00:00:00Z' | |
} | |
] | |
} | |
end | |
let(:data) { user_data } | |
let(:status) { 200 } | |
def stub_request_to_return(json) | |
stub_request(:get, 'https://api.truelayer.com/data/v1/info') | |
.with( | |
headers: { | |
'Authorization' => "Bearer #{token}" | |
} | |
) | |
.to_return(status: status, body: json, headers: {}) | |
end | |
before do | |
stub_request_to_return(data.to_json) | |
end | |
describe '#response' do | |
it 'returns response from end point' do | |
expect(subject.response).to be_success | |
end | |
context 'with True Layer error' do | |
let(:status) { 401 } | |
let(:date) { {} } | |
it 'does not return success' do | |
expect(subject.response).not_to be_success | |
end | |
end | |
end | |
describe '#emails' do | |
it 'returns the applicant email' do | |
expect(subject.emails).to eq([applicant.email]) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment