lib/oxr.rb in oxr-0.1.0.1 vs lib/oxr.rb in oxr-0.2.0

- old
+ new

@@ -5,40 +5,77 @@ 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 latest(only: nil) - endpoint = URI.join BASE_PATH, 'latest.json' - endpoint.query = "app_id=#{app_id}" - # Only allowed for paid plans - endpoint.query += "&symbols=#{Array(only).join ','}" if only - JSON.load open endpoint + def [](code) + latest['rates'][code] end - def historical(on:, only: nil) - date = on.strftime '%Y-%m-%d' - endpoint = URI.join BASE_PATH, 'historical/', "#{date}.json" - endpoint.query = "app_id=#{app_id}" - # Only allowed for paid plans - endpoint.query += "&symbols=#{Array(only).join ','}" if only - JSON.load open endpoint + 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 = URI.join BASE_PATH, 'currencies.json' - endpoint.query = "app_id=#{app_id}" - JSON.load open endpoint + endpoint = sources[:currencies] || build_uri_endpoint('currencies.json') + call endpoint end def usage - endpoint = URI.join BASE_PATH, 'usage.json' - endpoint.query = "app_id=#{app_id}" + 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