Sha256: a15f3e24ea3bcdaa49c73cb23c9f468da0d6ca05d1033d384a84806b88a9ba2e

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require "faraday"
require "faraday_middleware"
require "investec_open_api/models/account"
require "investec_open_api/models/transaction"

class InvestecOpenApi::Client
  def authenticate!
    @token = get_oauth_token["access_token"]
  end

  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

  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

  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

  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
investec_open_api-1.0.1 lib/investec_open_api/client.rb
investec_open_api-1.0.0 lib/investec_open_api/client.rb