-
-
Save regardfs/9b04619f3e12a869faa304f9bf301b1a 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