class CryptoVal::FiatExchange::Fixer

Constants

DEFAULT_SOURCE
DEFAULT_TARGET

Attributes

source_currency[R]
target_currency[R]

Public Class Methods

fetch(opts={}) click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 17
def self.fetch opts={}
  new(opts).fetch
end
new(opts={}) click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 8
def initialize opts={}
  @source_currency = opts[:source_currency] || DEFAULT_SOURCE
  @target_currency = opts[:target_currency] || DEFAULT_TARGET
end

Public Instance Methods

fetch() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 13
def fetch
  format_response
end

Private Instance Methods

base_url() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 23
def base_url
  "https://api.fixer.io"
end
format_response() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 51
def format_response
  {
    source: parsed["base"],
    target: @target_currency,
    date: parsed_date,
    rate: parsed["rates"][@target_currency].to_f
  }
end
get_response() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 35
def get_response
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Get.new(uri.request_uri)
  http.request(request).body
end
parsed() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 43
def parsed
  @parsed ||= JSON.parse(response)
end
parsed_date() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 47
def parsed_date
  Date.parse(parsed["date"]) rescue nil
end
response() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 31
def response
  @response ||= get_response
end
url() click to toggle source
# File lib/crypto_val/fiat_exchange/fixer.rb, line 27
def url
  "#{base_url}/latest?base=#{@source_currency}&symbols=#{@target_currency}"
end