Sha256: 3a8655351a238108c2a624b465112f316e1568d21a765884d93443d24c1eb8c9

Contents?: true

Size: 836 Bytes

Versions: 1

Compression:

Stored size: 836 Bytes

Contents

require "money_valuer/version"
require 'open-uri'

module MoneyValuer
    class MoneyCheckerFetchError < StandardError

    end

    SERVICE_HOST = "www.google.com"
    SERVICE_PATH = "/finance/converter"

    def self.check_rate(amount,from,to)
      data = build_uri(amount,from, to).read
      rate = extract_rate(data)
    end


    def self.build_uri(amount,from,to)
      uri = URI::HTTP.build(
          :host  => SERVICE_HOST,
          :path  => SERVICE_PATH,
          :query => "a=#{amount}&from=#{from}&to=#{to}"
      )
    end

    def self.extract_rate(data)
      case data
        when /<span class=bld>(\d+\.?\d*) [A-Z]{3}<\/span>/
           return $1.to_f
        when /Could not convert\./
          raise UnknownRate,"Invalid input data"
        else
          raise MoneyCheckerFeatchError
      end
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
money_valuer-0.1.0 lib/money_valuer.rb