lib/money-fixer-io.rb in money-fixer-io-0.0.1 vs lib/money-fixer-io.rb in money-fixer-io-0.0.2
- old
+ new
@@ -1,11 +1,9 @@
require "money"
require "open-uri"
class Money::Bank::FixerIo < Money::Bank::VariableExchange
- SERVICE_HOST = "api.fixer.io".freeze
-
# @return [Hash] Stores the currently known rates.
attr_reader :rates
class << self
# @return [Integer] Returns the Time To Live (TTL) in seconds.
@@ -129,10 +127,11 @@
from = Money::Currency.wrap(from)
to = Money::Currency.wrap(to)
uri = build_uri(from, to, args)
data = JSON.parse(uri.read)
+
rate = data.fetch("rates").fetch(to.iso_code)
rate = 1 / extract_rate(build_uri(to, from).read) if rate < 0.1
rate
end
@@ -143,20 +142,20 @@
# @param [Currency] to The currency to convert to.
#
# @return [URI::HTTP]
def build_uri(from, _to, args = {})
if args[:exchanged_at]
- path = "/#{args.fetch(:exchanged_at)}"
+ path = "/api/#{args.fetch(:exchanged_at)}"
else
- path = "/latest"
+ path = "/api/latest"
end
- query = "base=#{from.iso_code}"
+ query = {base: from.iso_code}
+ query[:access_key] = ENV["FIXER_IO_API_KEY"] if ENV["FIXER_IO_API_KEY"]
- uri = URI::HTTP.build(
- host: SERVICE_HOST,
+ URI::HTTP.build(
+ host: "data.fixer.io",
path: path,
- query: query
+ query: URI.encode_www_form(query)
)
- uri
end
end