Sha256: c8f38be9ed8b459600b5a61eb913e1179bde7b7302230165e95cc7792a874c3f

Contents?: true

Size: 1.56 KB

Versions: 34

Compression:

Stored size: 1.56 KB

Contents

require 'json'

content = File.open('country_codes_table.html').read #_lines[0..20].join

# content = %q{
# <table class="wikitable sortable">
# <tr>
# <th>Code</th>
# <th>Country name</th>
# <th>Year</th>
# <th>ccTLD</th>
# <th><span class="nowrap">ISO 3166-2</span></th>
# <th class="unsortable">Notes</th>
# </tr>
# <tr>
# <td id="AD"><tt>AD</tt></td>
# <td><a href="/wiki/Andorra" title="Andorra">Andorra</a></td>
# <td>1974</td>
# <td><a href="/wiki/.ad" title=".ad">.ad</a></td>
# <td><span class="nowrap"><a href="/wiki/ISO_3166-2:AD" title="ISO 3166-2:AD">ISO 3166-2:AD</a></span></td>
# <td></td>
# </tr>
# <tr>
# <td id="AE"><tt>AE</tt></td>
# <td><a href="/wiki/United_Arab_Emirates" title="United Arab Emirates">United Arab Emirates</a></td>
# <td>1974</td>
# <td><a href="/wiki/.ae" title=".ae">.ae</a></td>
# <td><span class="nowrap"><a href="/wiki/ISO_3166-2:AE" title="ISO 3166-2:AE">ISO 3166-2:AE</a></span></td>
# <td></td>
# </tr>
# <tr>
# <td id="AF"><tt>AF</tt></td>
# <td><a href="/wiki/Afghanistan" title="Afghanistan">Afghanistan</a></td>
# <td>1974</td>
# <td><a href="/wiki/.af" title=".af">.af</a></td>
# <td><span class="nowrap"><a href="/wiki/ISO_3166-2:AF" title="ISO 3166-2:AF">ISO 3166-2:AF</a></span></td>
# <td></td>
# </tr>
# <tr>	
# }

codes = content.scan(/tt>(\w+)<\/tt/m)

names = content.scan(/title="([\w|\s|\'|\-|\,]+)"/m)

zipped = codes.zip(names)

hash = {}
zipped.each do |pair|
	pair = pair.flatten
	hash[pair.first.to_s.downcase] = pair.last
end

json = JSON.pretty_generate(hash)

File.open('ISO-3166-2_codes.en.json', 'w+') do |f|
	f.puts json
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
world-flags-0.6.6 sandbox/extract_codes.rb
world-flags-0.6.5 sandbox/extract_codes.rb
world-flags-0.6.4 sandbox/extract_codes.rb
world-flags-0.6.3.1 sandbox/extract_codes.rb
world-flags-0.6.3 sandbox/extract_codes.rb
world-flags-0.6.2.1 sandbox/extract_codes.rb
world-flags-0.6.2 sandbox/extract_codes.rb
world-flags-0.6.1.1 sandbox/extract_codes.rb
world-flags-0.6.1 sandbox/extract_codes.rb
world-flags-0.6.0 sandbox/extract_codes.rb
world-flags-0.5.1 sandbox/extract_codes.rb
world-flags-0.5.0 sandbox/extract_codes.rb
world-flags-0.4.9 sandbox/extract_codes.rb
world-flags-0.4.8.3 sandbox/extract_codes.rb
world-flags-0.4.8.2 sandbox/extract_codes.rb
world-flags-0.4.7 sandbox/extract_codes.rb
world-flags-0.4.5 sandbox/extract_codes.rb
world-flags-0.4.4 sandbox/extract_codes.rb
world-flags-0.4.3 sandbox/extract_codes.rb
world-flags-0.4.2 sandbox/extract_codes.rb