Class: ECBExchangeRatesApi::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
SharedMethods, HTTParty
Defined in:
lib/ecb_exchange_rates_api/client.rb

Overview

Client. Provides ways for different configurations.

Instance Method Summary collapse

Methods included from SharedMethods

included

Constructor Details

#initialize {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
# File 'lib/ecb_exchange_rates_api/client.rb', line 21

def initialize
  @options = ECBExchangeRatesApi::Options.new
  yield self if block_given?
end

Instance Method Details

#at(date) ⇒ Object



41
42
43
44
# File 'lib/ecb_exchange_rates_api/client.rb', line 41

def at(date)
  @options.specific_date = date
  self
end

#convert(amount, base_code = nil, codes = nil, date = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/ecb_exchange_rates_api/client.rb', line 61

def convert(amount, base_code = nil, codes = nil, date = nil)
  response = self.class.new do |client|
    client.with_base presented_base(base_code)
    client.for_rates presented_symbols(codes)
    client.at presented_date(date)
  end.fetch

  response.rates.transform_values! { |v| v * amount }
  response
end

#currency_is_supported?(code) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/ecb_exchange_rates_api/client.rb', line 72

def currency_is_supported?(code)
  supported_currency?(validated_currency_code(code))
end

#fetchObject



26
27
28
29
# File 'lib/ecb_exchange_rates_api/client.rb', line 26

def fetch
  response = self.class.get(path, query: options.to_params)
  create_result(response.parsed_response, response.code)
end

#for_rate(code) ⇒ Object



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

def for_rate(code)
  @options.append_symbol(code)
  self
end

#for_rates(codes) ⇒ Object



51
52
53
54
# File 'lib/ecb_exchange_rates_api/client.rb', line 51

def for_rates(codes)
  codes.each(&method(:for_rate))
  self
end

#from(date) ⇒ Object



31
32
33
34
# File 'lib/ecb_exchange_rates_api/client.rb', line 31

def from(date)
  @options.start_at = date
  self
end

#supported_currenciesObject



76
77
78
# File 'lib/ecb_exchange_rates_api/client.rb', line 76

def supported_currencies
  CURRENCIES
end

#to(date) ⇒ Object



36
37
38
39
# File 'lib/ecb_exchange_rates_api/client.rb', line 36

def to(date)
  @options.end_at = date
  self
end

#with_base(code) ⇒ Object



46
47
48
49
# File 'lib/ecb_exchange_rates_api/client.rb', line 46

def with_base(code)
  @options.base = code
  self
end