Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/flinks/api/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ def accounts_detail(request_id:, options: {})
# @param request_id [String]
# @return [Hash]
def accounts_summary_async(request_id:)
get("#{customer_id}/BankingServices/GetAccountsSummaryAsync/#{request_id}")
get("#{customer_id}/BankingServices/GetAccountsSummaryAsync/#{request_id}", params: {}, async: true)
end

# @see https://sandbox-api.flinks.io/Readme/#get-accounts-detail
# @param request_id [String]
# @return [Hash]
def accounts_detail_async(request_id:)
get("#{customer_id}/BankingServices/GetAccountsDetailAsync/#{request_id}")
get("#{customer_id}/BankingServices/GetAccountsDetailAsync/#{request_id}", params: {}, async: true)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flinks/api/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Card
# @param card_id [String]
# @return [Hash]
def delete_card(card_id:)
get("#{customer_id}/BankingServices/DeleteCard/#{card_id}")
delete("#{customer_id}/BankingServices/DeleteCard/#{card_id}")
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/flinks/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Flinks
class Error < StandardError
attr_reader :response, :code
attr_reader :response, :code, :flinks_code

# @param response [HTTP::Response]
# @return [Flinks::Error]
Expand Down Expand Up @@ -65,17 +65,18 @@ def self.error_for_403(response)
# @return [Flinks::Error]
def initialize(response)
@response = response
@code = response.code
@code = response.code if response.try(:code).present?
@flinks_code = response&.try(:parse)&.dig('FlinksCode')

super(build_message)
end

# @return [String]
def build_message
message = response.parse['Message']
message << " - FlinksCode: #{response.parse['FlinksCode']}"
rescue HTTP::Error
response.reason
message = response.parse['Message'] || "Error"
message + " - FlinksCode: #{response.parse['FlinksCode']}"
rescue StandardError
response.try(:reason)
end
end

Expand Down
40 changes: 29 additions & 11 deletions lib/flinks/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,41 @@ module Request
#
# @param path [String]
# @param params [Hash]
def get(path, params: {})
request(:get, path, params: params)
def get(path, params: {}, async: false)
request(:get, path, params: params, async: async)
end

# Performs a HTTP Delete request
#
# @param path [String]
# @param params [Hash]
def delete(path, params: {}, async: false)
request(:delete, path, params: params, async: async)
end

# Performs a HTTP Post request
#
# @param path [String]
# @param params [Hash]
# @param body [Hash]
def post(path, params: {}, body: {})
request(:post, path, params: params, body: body)
def post(path, params: {}, body: {}, async: false)
request(:post, path, params: params, body: body, async: async)
end

# Performs a HTTP Patch request
#
# @param path [String]
# @param params [Hash]
# @param body [Hash]
def patch(path, params: {}, body: {})
request(:patch, path, params: params, body: body)
def patch(path, params: {}, body: {}, async: false)
request(:patch, path, params: params, body: body, async: async)
end


private

# @return [HTTP::Client]
# @raise [Flinks::Error]
def request(method, path, params: {}, body: {})
def request(method, path, params: {}, body: {}, async: false)
headers = {
'Content-Type' => "application/json",
'Accept' => "application/json",
Expand All @@ -52,14 +59,22 @@ def request(method, path, params: {}, body: {})
# Build payload
payload = body.transform_keys { |k| k.to_s.camelize }

# Perform request
response = Http.headers(headers).send(method, url, params: params, json: payload)

if debug
p "Method: #{method}"
p "Url: #{url}"
p "Headers: #{headers}"
p "Payload: #{payload}"
p "Response: loading..."
end

# Perform request
if async
response = Http.headers(headers).send(method, url)
else
response = Http.headers(headers).send(method, url, params: params, json: payload)
end

if debug
p "Response: #{response}"
end

Expand All @@ -75,6 +90,9 @@ def request(method, path, params: {}, body: {})

# Transform data
data.deep_transform_keys { |k| k.underscore }.with_indifferent_access

rescue JSON::ParserError
response
end
end
end