Skip to content

Instantly share code, notes, and snippets.

@ridem
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridem/80697bed0580589ea424 to your computer and use it in GitHub Desktop.
Save ridem/80697bed0580589ea424 to your computer and use it in GitHub Desktop.
Get a YAML dictionary of ISO 4217 and ISO 3166-1 mapping / Country-currency binding - Made for the `exchange` gem
require "json"
require "net/http"
require "uri"
require 'yaml'
uri = URI.parse("http://restcountries.eu/rest/v1/all")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code == "200"
result = JSON.parse(response.body)
dict = Hash.new
result.each do |country|
## If a country has more than one currency, we have to check
## on the ISO norm which one is the actually used currency. The 'puts' line displays those countries that need double-checking.
if country["currencies"].length > 1
puts "#{country["name"]} (#{country["alpha2Code"]}-#{country["alpha3Code"]}): #{country["currencies"].length}"
end
currency = country["currencies"][0].downcase.to_sym
dict[country["alpha2Code"]] = currency
dict[country["alpha3Code"]] = currency
end
File.open('iso4217_country_map.yml','w') do |s|
s.puts dict.to_yaml
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment