Module: GoogCurrency
- Defined in:
- lib/goog_currency.rb,
lib/goog_currency/version.rb
Defined Under Namespace
Classes: Exception, NoMethodException
Constant Summary
collapse
- MILLION =
1_000_000
- VERSION =
"1.0.2"
Class Method Summary
collapse
Class Method Details
.method_missing(meth, *args) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/goog_currency.rb', line 7
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 = RestClient.get("http://www.google.com/ig/calculator?hl=en&q=#{args.first}#{from.upcase}=?#{to.upcase}").body
encoding_options = {
:invalid => :replace, :undef => :replace, :replace => '' }
response = response.encode(Encoding.find('ASCII'), encoding_options)
response.gsub!(/(lhs|rhs|error|icc)/, '"\1"')
response_hash = JSON.parse(response)
if response_hash['error'].nil? or response_hash['error'] == ''
case response_hash['rhs']
when /million/
response_hash['rhs'].to_f * MILLION
when /billion/
response_hash['rhs'].to_f * MILLION * 1_000
when /trillion/
response_hash['rhs'].to_f * MILLION * MILLION
else
response_hash['rhs'].to_f
end
else
raise Exception, "An error occurred: #{response_hash['error']}"
end
end
|
.respond_to?(meth) ⇒ Boolean
44
45
46
47
48
49
50
51
52
|
# File 'lib/goog_currency.rb', line 44
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
|