Class: CurrencyConversion::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/currency_conversion/gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Gateway

Returns a new instance of Gateway.



5
6
7
# File 'lib/currency_conversion/gateway.rb', line 5

def initialize(params={})
  @options = params
end

Instance Method Details

#convert_paymentObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/currency_conversion/gateway.rb', line 8

def convert_payment
  response = RestClient.get("http://www.google.com/ig/calculator?hl=en&q=#{@options[:value]}#{@options[:from].upcase}=?#{@options[:to].upcase}").body
  response.gsub!(/(lhs|rhs|error|icc)/, '"\1"')
  response_hash = JSON.parse(response)

  if response_hash['error'].nil? or response_hash['error'] == ''
    encoding_options = {
      :invalid           => :replace,  
      :undef             => :replace, 
      :replace           => '',      
      :universal_newline => true    
    }
    response_hash['rhs'].encode(Encoding.find('ASCII'), encoding_options).to_f
  else
    raise Exception, "An error occurred: #{response_hash['error']}"
  end
end