Module: Mula::Helpers

Defined in:
lib/mula/helpers.rb

Instance Method Summary collapse

Instance Method Details

#convert(amount = 1, current_currency = 'USD', target_currency = 'GBP') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mula/helpers.rb', line 9

def convert(amount=1, current_currency='USD', target_currency='GBP')
  uri = URI.parse("#{API_BASE}?hl=en&q=#{amount}#{current_currency}=?#{target_currency}")
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.request(Net::HTTP::Get.new(uri.request_uri))
  # fix the mess than Google have made of the JSON
  fixed_json_str = response.body.gsub!(/(['"])?([a-zA-Z0-9_]+)(['"])?:/, '"\2":')
  ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
  valid_string = ic.iconv(fixed_json_str + ' ')[0..-2]
  parsed_response = ActiveSupport::JSON.decode(valid_string)
  return parsed_response["rhs"].split(" ")[0]
end