lib/nubank_sdk/auth.rb in nubank_sdk-0.6.1 vs lib/nubank_sdk/auth.rb in nubank_sdk-0.7.0
- old
+ new
@@ -1,10 +1,13 @@
# frozen_string_literal: true
require 'securerandom'
module NubankSdk
+ #
+ # Auth method to connect with the nubank api
+ #
class Auth
attr_reader :refresh_token, :refresh_before, :access_token
#
# Auth method to connect with the nubank api
@@ -14,21 +17,21 @@
# @param [NubankSdk::ApiRoutes] api_routes the api routes to connect
# @param [[Symbol, Faraday::Adapter::Test::Stubs]] adapter the adapter to connect
def initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil)
@cpf = cpf
@device_id = device_id || generate_device_id
- @api_routes = api_routes || NubankSdk::ApiRoutes.new
+ @api_routes = api_routes || ApiRoutes.new
@connection_adapter = connection_adapter
end
#
# Return the instance of user certificate
#
# @return [NubankSdk::Certificate] the certificate instance
def certificate
- @certificate ||= NubankSdk::Certificate.new(@cpf)
+ @certificate ||= Certificate.new(@cpf)
end
#
# Authenticate with the nubank api to get a new access token
#
@@ -39,13 +42,12 @@
token_url = @api_routes.entrypoint(path: :app, entrypoint: :token)
response = ssl_connection.post(token_url, token_payload(password))
response_hash = Client.get_body(response)
- @refresh_token = response_hash[:refresh_token]
- @refresh_before = response_hash[:refresh_before]
- @access_token = response_hash[:access_token]
+ @refresh_token, @refresh_before, @access_token = response_hash.values_at(:refresh_token, :refresh_before,
+ :access_token)
update_api_routes(response_hash[:_links])
end
#
@@ -66,14 +68,13 @@
#
# Verify communication with the nubank api
#
# @return [File] the certificate file
def exchange_certs(email_code, password)
- response = default_connection.post(@gen_certificate_path, payload(password).merge({
- code: email_code,
- 'encrypted-code': @encrypted_code
- }))
+ new_payload = payload(password)
+ .merge({ code: email_code, 'encrypted-code': @encrypted_code })
+ response = default_connection.post(@gen_certificate_path, new_payload)
response_data = Client.get_body(response)
certificate.process_decoded(key, response_data[:certificate])
end
@@ -144,19 +145,15 @@
#
# @param [Hash] links the new links to add
#
# @return [NubankSdk::ApiRoutes] the api routes with the new links
def update_api_routes(links)
- feed_url_keys = %i[events magnitude]
- bills_url_keys = [:bills_summary]
- customer_url_keys = [:customer]
- account_url_keys = [:account]
@api_routes.add_entrypoint(path: :ssl, entrypoint: :revoke_token, url: links[:revoke_token][:href])
@api_routes.add_entrypoint(path: :ssl, entrypoint: :query, url: links[:ghostflame][:href])
- @api_routes.add_entrypoint(path: :ssl, entrypoint: :feed, url: find_url(feed_url_keys, links))
- @api_routes.add_entrypoint(path: :ssl, entrypoint: :bills, url: find_url(bills_url_keys, links))
- @api_routes.add_entrypoint(path: :ssl, entrypoint: :customer, url: find_url(customer_url_keys, links))
- @api_routes.add_entrypoint(path: :ssl, entrypoint: :account, url: find_url(account_url_keys, links))
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :feed, url: find_url(%i[events magnitude], links))
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :bills, url: find_url([:bills_summary], links))
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :customer, url: find_url([:customer], links))
+ @api_routes.add_entrypoint(path: :ssl, entrypoint: :account, url: find_url([:account], links))
@api_routes
end
# @!visibility private
# Return the url of the first key found in the links