Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Use Google calculator to convert currency in Ruby

View convert_currency.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 
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
Something went wrong with that request. Please try again.