class InvestecOpenApi::Client

Public Instance Methods

accounts() click to toggle source
# File lib/investec_open_api/client.rb, line 11
def accounts
  response = connection.get("za/pb/v1/accounts")
  response.body["data"]["accounts"].map do |account_raw|
    InvestecOpenApi::Models::Account.from_api(account_raw)
  end
end
authenticate!() click to toggle source
# File lib/investec_open_api/client.rb, line 7
def authenticate!
  @token = get_oauth_token["access_token"]
end
transactions(account_id) click to toggle source
# File lib/investec_open_api/client.rb, line 18
def transactions(account_id)
  response = connection.get("za/pb/v1/accounts/#{account_id}/transactions")
  response.body["data"]["transactions"].map do |transaction_raw|
    InvestecOpenApi::Models::Transaction.from_api(transaction_raw)
  end
end

Private Instance Methods

connection() click to toggle source
# File lib/investec_open_api/client.rb, line 44
def connection
  @_connection ||= Faraday.new(url: InvestecOpenApi.api_url) do |builder|
    if @token
      builder.headers["Authorization"] = "Bearer #{@token}"
    end

    builder.headers["Accept"] = "application/json"
    builder.request :json

    builder.response :raise_error
    builder.response :json

    builder.adapter Faraday.default_adapter
  end
end
get_oauth_token() click to toggle source
# File lib/investec_open_api/client.rb, line 27
def get_oauth_token
  auth_connection = Faraday.new(url: InvestecOpenApi.api_url) do |builder|
    builder.headers["Accept"] = "application/json"
    builder.basic_auth(InvestecOpenApi.api_username, InvestecOpenApi.api_password)
    builder.response :raise_error
    builder.response :json
    builder.adapter Faraday.default_adapter
  end

  response = auth_connection.post("identity/v2/oauth2/token", {
    grant_type: "client_credentials",
    scope: "accounts"
  }.to_query)

  response.body
end