Module: GoogCurrency

Defined in:
lib/goog_currency.rb,
lib/goog_currency/version.rb

Defined Under Namespace

Classes: Exception, NoMethodException

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.handle_response(response) ⇒ Object

Raises:


26
27
28
29
30
# File 'lib/goog_currency.rb', line 26

def self.handle_response(response)
  value = response.scan(/<span class=bld>([^.]+(?:\.(?:\d+))?)/)
  raise Exception, "An error occurred: Currency not found" if value.empty?
  value[0][0].to_f
end

.method_missing(meth, *args) ⇒ Object


5
6
7
8
9
10
11
12
13
14
# File 'lib/goog_currency.rb', line 5

def self.method_missing(meth, *args)
  from, to = meth.to_s.split("_to_")

  if from.nil? or to.nil? or from == "" or to == ""
    raise NoMethodException, "GoogCurrency accepts methods in 'usd_to_inr' or 'gbp_to_usd' format"
  end

  response = open("http://www.google.com/finance/converter?a=#{args.first}&from=#{from.upcase}&to=#{to.upcase}").read
  handle_response(response)
end

.respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)

16
17
18
19
20
21
22
23
24
# File 'lib/goog_currency.rb', line 16

def self.respond_to?(meth)
  from, to = meth.to_s.split("_to_")

  if from.nil? or from == "" or to.nil? or to == ""
    super
  else
    true
  end
end