Skip to content

Instantly share code, notes, and snippets.

@ylluminate
Created September 11, 2012 23:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ylluminate/3702863 to your computer and use it in GitHub Desktop.
Save ylluminate/3702863 to your computer and use it in GitHub Desktop.
Use Google calculator to convert currency in Ruby
require 'faraday'
require 'faraday_middleware'
require 'json'
# Debug:
# require "pry"
country_code_src = "USD"
country_code_dst = "INR"
connection = Faraday.get("http://www.google.com/ig/calculator?hl=en&q=1#{country_code_src}=?#{country_code_dst}")
currency_comparison_hash = eval connection.body #Google's output is not JSON, it's a hash
dst_currency_value, *dst_currency_text = *currency_comparison_hash[:rhs].split(' ')
dst_currency_value = dst_currency_value.to_f
dst_currency_text = dst_currency_text.join(' ')
puts "#{country_code_dst} -> #{dst_currency_value} (#{dst_currency_text} to 1 #{country_code_src})"
@martonpe
Copy link

martonpe commented Apr 8, 2015

Found this Gist somewhere on SO, so I thought I'd drop a line that iGoogle has been since decommissioned. You can find a working example with Google's finance calculator here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment