class Arbolito::Exchange::AlphaVantage

Constants

BaseURL

Attributes

api_key[R]

Public Class Methods

new(api_key) click to toggle source
# File lib/arbolito/exchange/alpha_vantage.rb, line 11
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

build_uri(from, to) click to toggle source
# File lib/arbolito/exchange/alpha_vantage.rb, line 39
def build_uri(from, to)
  url = BaseURL % [from.upcase, to.upcase, api_key]
  URI(url)
end
find_current_rate(quote) click to toggle source
# File lib/arbolito/exchange/alpha_vantage.rb, line 15
def find_current_rate(quote)
  data = response(build_uri(quote.from,quote.to))
  rated_at_str = "#{data['6. Last Refreshed']} #{data['7. Time Zone']}"
  rated_at = Time.strptime(rated_at_str,'%Y-%m-%d %H:%M:%S %Z')

  values = { 
    quote: quote.to_hash,
    price: data['5. Exchange Rate'],
    rated_at: rated_at.localtime
  }

  Currency::Rate.new(
      values[:price],
      values[:quote].to_hash,
      values[:rated_at]
  )
end
response(uri) click to toggle source
# File lib/arbolito/exchange/alpha_vantage.rb, line 33
def response(uri)
  response = Net::HTTP.get(uri)
  json = JSON.parse(response)
  json['Realtime Currency Exchange Rate']
end