Skip to content

Instantly share code, notes, and snippets.

@regardfs
Forked from ylluminate/convert_currency.rb
Created January 18, 2017 03:42
Show Gist options
  • Save regardfs/9b04619f3e12a869faa304f9bf301b1a to your computer and use it in GitHub Desktop.
Save regardfs/9b04619f3e12a869faa304f9bf301b1a 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})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment