Class: Uktt::MonetaryExchangeRate

Inherits:
Object
  • Object
show all
Defined in:
lib/uktt/monetary_exchange_rate.rb

Overview

A MonetaryExchangeRate object for dealing with an API resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MonetaryExchangeRate

Returns a new instance of MonetaryExchangeRate.



6
7
8
9
10
# File 'lib/uktt/monetary_exchange_rate.rb', line 6

def initialize(opts = {})
  @monetary_exchange_rate_id = opts[:monetary_exchange_rate_id] || nil
  Uktt.configure(opts)
  @config = Uktt.config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/uktt/monetary_exchange_rate.rb', line 4

def config
  @config
end

#monetary_exchange_rate_idObject

Returns the value of attribute monetary_exchange_rate_id.



4
5
6
# File 'lib/uktt/monetary_exchange_rate.rb', line 4

def monetary_exchange_rate_id
  @monetary_exchange_rate_id
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/uktt/monetary_exchange_rate.rb', line 4

def response
  @response
end

Instance Method Details

#latest(currency) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/uktt/monetary_exchange_rate.rb', line 16

def latest(currency)
  retrieve_all unless @response

  case @config[:version]
  when 'v1'
    @response.select{ |obj| obj.child_monetary_unit_code == currency.upcase }
             .sort_by(&:validity_start_date)
             .last.exchange_rate.to_f
  when 'v2'
    @response.data.select{ |obj| obj.attributes.child_monetary_unit_code == currency.upcase }
             .sort_by{ |obj| obj.attributes.validity_start_date }
             .last.attributes.exchange_rate.to_f
  else
    raise StandardError.new "`#{@opts[:version]}` is not a supported API version. Supported API versions are: v1 and v2"
  end
end

#retrieve_allObject



12
13
14
# File 'lib/uktt/monetary_exchange_rate.rb', line 12

def retrieve_all
  fetch "#{M_X_RATE}.json"
end