Sha256: 1858cd81b339097f26a9963e9c6fe1e19bf19fb4cb4f3785aa8d88959e67f76f

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require 'currency_converter/exceptions'
require 'currency_converter/currencies'
require 'net/http'
require 'nokogiri'

module CurrencyConverter
  class Yahoo
    # Returns the Symbol of 'base' currency
    attr_reader :from_currency

    # Returns the Symbol of 'quot' currency
    attr_reader :to_currency

    # Returns the array of currencies rates
    attr_reader :rates

    API_URL = 'https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic'

    def initialize
      response = fetch_data

      parse_rates(response)
    end

    # Receive the amount of you desire currency.
    #
    # @param [String, String, Numeric] other currency to exchange to.
    #
    # @return [amount]
    #
    # @example
    # currency_converter = CurrencyConverter::Yahoo.new
    # currency_converter.exchange('USD', 'EUR', 100)
    # currency_converter.exchange('USD', 'INR', 100)
    def exchange(base, quot, amount)
      @from_currency = base.upcase.to_sym
      @to_currency = quot.upcase.to_sym

      validate_currency

      base_rate = rates[from_currency].to_f
      quot_rate = rates[to_currency].to_f

      rate = base_rate.zero? ? 0 : (quot_rate / base_rate)
      validate_rate(rate)

      amount * rate
    end

    private

    def fetch_data
      Net::HTTP.get(URI(API_URL))
    end

    def parse_rates(html)
      @rates = { USD: 1.0 }

      result = Nokogiri::HTML(html)

      result.css('resource').each do |resource|
        symbol = resource.xpath(".//field[@name='symbol']").text[0,3]
        price  = resource.xpath(".//field[@name='price']").text

        rates[symbol.upcase.to_sym] = price.to_f unless symbol.nil? && price.nil?
      end
    end

    def validate_currency
      raise UnknownCurrency.new(from_currency) unless CURRENCIES.has_key?(from_currency)
      raise UnknownCurrency.new(to_currency) unless CURRENCIES.has_key?(to_currency)
    end

    def validate_rate(rate)
      raise MissingExchangeRate.new(from_currency, to_currency) if rate.zero?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
currency_converter-1.1.4 lib/currency_converter/yahoo.rb
currency_converter-1.1.3 lib/currency_converter/yahoo.rb
currency_converter-1.1.2 lib/currency_converter/yahoo.rb