Sha256: e2af513b44302225ae87738b6db435cbb63ad1aad4769bebbd84f969343ab820

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Fixer
  class Request
    attr_reader :response
    def initialize(base = Fixer.base)
      base_host = "https://api.fixer.io/latest?base=#{base}"
      @response = HTTParty.get(base_host)
    end

    def transfer(data)
      if data.is_a? Hash
        if [:target, :ammount].all? { |k| data.key? k }
          target = data[:target]
          if (valid_currency?(target)) && data[:ammount].is_a?(Numeric)
            result = JSON.parse @response.body, symbolize_names: true
            data[:ammount] * result[:rates][target.to_sym]
          else
            raise "current currency is not supported or ammount set to an non-integer value"
          end
        end
      else
        raise "only hash allowed"
      end
    end

    private
    def valid_currency?(currency)
      valid_currencies = ["EUR", "AUD", "BGN", "BRL", "CAD", "CHF", "CNY",
                          "CZK", "DKK", "GBP", "HKD", "HRK", "HUF", "IDR",
                          "ILS", "INR", "JPY", "KRW", "MXN", "MYR", "NOK",
                          "NZD", "PHP", "PLN", "RON", "RUB", "SEK", "SGD",
                          "THB", "TRY", "USD", "ZAR"]
      valid_currencies.include? currency
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fixer-rb-0.1.1 lib/fixer/request.rb
fixer-rb-0.1.0 lib/fixer/request.rb