Class: CurrencyLayerApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/currency_layer_api/client.rb

Overview

Client having API methods and access_keys

Constant Summary collapse

BASE_URL =
"api.currencylayer.com/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key:, ssl: true, adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

#### params: access_key: String (Required) ssl: Boolean (Optional) (Default: true) adapter: Symbol (Optional) (Default: :net_http) stubs: Faraday::Adapter::Test::Stubs (Optional) (Default: nil) #### Example: CurrencyLayerApi::Client.new(access_key: “fake”, ssl: false, adapter: :test, stubs: stub) CurrencyLayerApi::Client.new(access_key: “fake”) #### Description: Creates a new instance of CurrencyLayerApi::Client with access_key provided by currencylayer.com



21
22
23
24
25
26
27
28
# File 'lib/currency_layer_api/client.rb', line 21

def initialize(access_key:, ssl: true, adapter: Faraday.default_adapter, stubs: nil)
  @access_key = access_key
  @adapter = adapter
  @ssl = ssl

  # Test stubs for requests
  @stubs = stubs
end

Instance Attribute Details

#access_keyObject (readonly)

Returns the value of attribute access_key.



9
10
11
# File 'lib/currency_layer_api/client.rb', line 9

def access_key
  @access_key
end

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/currency_layer_api/client.rb', line 9

def adapter
  @adapter
end

Instance Method Details

#convert(to:, from:, amount: 1, date: nil) ⇒ Object

#### Params: to: String (Required) (Currency code) from: String (Required) (Currency code) amount: Int (Required) (Default: 1) date: String (Optional) (‘YYYY-MM-DD’) #### Example: CurrencyLayerApi::Client.new(access_key: “fake”).convert(to: “AED”, from: “USD”, amount: 1) CurrencyLayerApi::Client.new(access_key: “fake”).convert(to: “AED”, from: “USD”, amount: 1, date: “2019-01-01”) #### Response: 3.6725 #### Description: Returns the exchange rate for a given date, from currency and to currency



95
96
97
# File 'lib/currency_layer_api/client.rb', line 95

def convert(to:, from:, amount: 1, date: nil)
  get("convert", { from: from, to: to, amount: amount, date: date }.compact).body["result"]
end

#historical(date:, from: "USD", to: nil) ⇒ Object

#### Params: date: String (Required) (‘YYYY-MM-DD’) from: String (Required) (Default: “USD”) to: Array (Optional) (Default: nil) #### Example: CurrencyLayerApi::Client.new(access_key: “fake”).historical(date: “2019-01-01”, from: “USD”, to: [“AED”, “EUR”]) CurrencyLayerApi::Client.new(access_key: “fake”).historical(date: “2019-01-01”, from: “EUR”) #### Response: [

#<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,

] #### Description: Returns the historical exchange rate for a given date, from currency and to currencies



76
77
78
79
80
81
# File 'lib/currency_layer_api/client.rb', line 76

def historical(date:, from: "USD", to: nil)
  resp = get("historical", { date: date, source: from, currencies: to ? to.join(",") : nil }.compact)
  resp.body["quotes"].map do |key, val|
    Conversion.new(from_code: key[0..2], to_code: key[3..], value: val, date: resp.body["date"])
  end
end

#listObject

#### Example: CurrencyLayerApi::Client.new(access_key: “fake”).list #### Response: [

#<CurrencyLayerApi::Currency:0x00007f9b0a0b0a00 @code="AED", @name="United Arab Emirates Dirham">,
#<CurrencyLayerApi::Currency:0x00007f9b0a0b09c0 @code="AFN", @name="Afghan Afghani">,

] #### Description: Resturns currency object array for all currencies available in currency layer



39
40
41
# File 'lib/currency_layer_api/client.rb', line 39

def list
  get("list").body["currencies"].map { |key, val| Currency.new(code: key, name: val) }
end

#live(from: "USD", to: nil) ⇒ Object

#### Params: from: String (Required) (Default: “USD”) to: Array (Optional) (Default: nil) #### Example: CurrencyLayerApi::Client.new(access_key: “fake”).live(from: “USD”, to: [“AED”, “EUR”]) CurrencyLayerApi::Client.new(access_key: “fake”).live(from: “EUR”) #### Response: [

#<CurrencyLayerApi::Conversion:0x00007f9b0a0b0a00 @from_code="USD", @to_code="AED", @value=3.6725, @date="2019-01-01">,
#<CurrencyLayerApi::Conversion:0x00007f9b0a0b09c0 @from_code="USD", @to_code="EUR", @value=0.888, @date="2019-01-01">,

] #### Description: Returns the current exchange rate for a given from currency and to currencies



56
57
58
59
60
# File 'lib/currency_layer_api/client.rb', line 56

def live(from: "USD", to: nil)
  get("live", { source: from, currencies: to ? to.join(",") : nil }.compact).body["quotes"].map do |key, val|
    Conversion.new(from_code: key[0..2], to_code: key[3..], value: val, date: Date.today.to_s)
  end
end