Sha256: 292ab1848e0c13207005d24b28f6864b25826d310f220de9b05c27ccc335c08d

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

require 'money'
require 'open-uri'

class Money
  module Bank
    class GoogleCurrency < Money::Bank::VariableExchange
      attr_reader :rates

      def get_rate(from, to)
        @mutex.synchronize{
          @rates[rate_key_for(from, to)] ||= get_google_rate(from, to)
        }
      end

      def get_google_rate(from, to)
        from = Currency.wrap(from)
        to   = Currency.wrap(to)

        data = URI.parse("http://www.google.com/ig/calculator?hl=en&q=1#{from.iso_code}%3D%3F#{to.iso_code}").read
        data.gsub!(/lhs:/, ':lhs =>')
        data.gsub!(/rhs:/, ':rhs =>')
        data.gsub!(/error:/, ':error =>')
        data.gsub!(/icc:/, ':icc =>')
        data = eval(data)

        raise UnknownRate unless data[:error] == '' or data[:error] == '0'
        data[:rhs].split(' ')[0].to_f
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google_currency-0.1.0 lib/money/bank/google_currency.rb