lib/money_valuer.rb in money_valuer-0.1.0 vs lib/money_valuer.rb in money_valuer-0.1.1

- old
+ new

@@ -1,37 +1,33 @@ -require "money_valuer/version" +require 'money_valuer/version' require 'open-uri' - +require 'money_valuer/errors' module MoneyValuer - class MoneyCheckerFetchError < StandardError + attr_accessor :resource - end + SERVICE_HOST = 'www.google.com'.freeze + SERVICE_PATH = '/finance/converter'.freeze - 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.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.build_uri(amount,from,to) - uri = URI::HTTP.build( - :host => SERVICE_HOST, - :path => SERVICE_PATH, - :query => "a=#{amount}&from=#{from}&to=#{to}" - ) + def self.extract_rate(data) + case data + when /<span class=bld>(\d+\.?\d*) [A-Z]{3}<\/span>/ + return Regexp.last_match(1).to_f + when /Could not convert\./ + raise MoneyValuer::UnknownRate, 'Invalid input data' + else + raise MoneyValuer::ConnectionError, 'Unexpected parameters' 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 end