Created
September 11, 2012 23:05
-
-
Save ylluminate/3702863 to your computer and use it in GitHub Desktop.
Use Google calculator to convert currency in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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