Sha256: 95dae6fe794c56ca9cb13968136ae609e3e89f296c0bfc1b98ff54b29efda607
Contents?: true
Size: 1.43 KB
Versions: 1
Compression:
Stored size: 1.43 KB
Contents
require "oxr/version" require 'date' require 'json' require 'open-uri' class OXR BASE_PATH = 'https://openexchangerates.org/api/'.freeze class OXRError < StandardError def initialize(message, response) super message @response = response end attr_reader :response end def initialize(app_id) @app_id = app_id end attr_reader :app_id def [](code) latest['rates'][code] end def latest endpoint = sources[:latest] || build_uri_endpoint('latest.json') call endpoint end def historical(on:) endpoint = sources[:historical] || \ build_uri_endpoint('historical/', "#{on.strftime '%Y-%m-%d'}.json") call endpoint end def currencies endpoint = sources[:currencies] || build_uri_endpoint('currencies.json') call endpoint end def usage endpoint= sources[:usage] || build_uri_endpoint('usage.json') call endpoint end private def build_uri_endpoint(*path, **params) URI.join(BASE_PATH, *path).tap do |uri| uri.query = "app_id=#{app_id}" end end def call(endpoint) JSON.load open endpoint rescue OpenURI::HTTPError => e case e.message when /\A4[[:digit:]]{2}/ response = JSON.load e.io raise OXRError.new response['description'], response else raise end end def sources self.class.sources end def self.sources @sources ||= {} end def self.reset_sources sources.clear end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
oxr-0.2.0 | lib/oxr.rb |