開発したアプリなど一覧

Ruby で ISO 3166 に則った各国の名称やコード、通貨など様々な情報を取得する countries gem

アフィリエイトリンクを含む場合があります

各国の国名コードとか通貨とかそういったイロイロな情報を欲しくなったのでどうやったら簡単に取得できるのかを調べたところ、countries という gem を使うと良さそうであったので試してみた。

https://github.com/hexorx/countries

Gemfile に gem "countries" と書くか単に gem install countries とか実行すると使えるようになる。

例えば日本の情報を表示する場合は以下のようにする。

country = ISO3166::Country.new('JP') # 取得の仕方が複数ある。 country = ISO3166::Country.find_country_by_name('japan')

そうすると以下のような情報を得る事ができる。

#<ISO3166::Country:0x007fdc4a0150a8 @country_data_or_code="JP", @data= {"continent"=>"Asia", "address_format"=> "〒{{postalcode}}\n{{region_short}}{{city}}{{street}}\n{{recipient}}\n{{country}}", "alpha2"=>"JP", "alpha3"=>"JPN", "country_code"=>"81", "international_prefix"=>"010", "ioc"=>"JPN", "gec"=>"JA", "name"=>"Japan", "national_destination_code_lengths"=>[2], "national_number_lengths"=>[9, 10], "national_prefix"=>"0", "number"=>"392", "region"=>"Asia", "subregion"=>"Eastern Asia", "world_region"=>"APAC", "un_locode"=>"JP", "nationality"=>"Japanese", "postal_code"=>true, "unofficial_names"=>["Japan", "Japon", "Japón", "日本"], "languages_official"=>["ja"], "languages_spoken"=>["ja"], "geo"=> {"latitude"=>36.204824, "latitude_dec"=>"36.281646728515625", "longitude"=>138.252924, "longitude_dec"=>"139.0772705078125", "max_latitude"=>45.6412626, "max_longitude"=>154.0031455, "min_latitude"=>20.3585295, "min_longitude"=>122.8554688, "bounds"=> {"northeast"=>{"lat"=>45.6412626, "lng"=>154.0031455}, "southwest"=>{"lat"=>20.3585295, "lng"=>122.8554688}}}, "currency_code"=>"JPY", "start_of_week"=>"monday", "translations"=>{"en"=>"Japan"}, "translated_names"=>["Japan"]}>

国名、国が属する地域、ISO3166 に則った各種コード、話される言語、緯度経度、通貨、といった様々な情報を得る事ができる。

https://ja.wikipedia.org/wiki/ISO_3166

国名は標準では英語で取得されるが translation メソッドにより他の言語でも取得できる。が、デフォルトでは英語しか表示されないようだ。

README を良く読んだところ、ロケールを設定する事で他の言語の情報を得る事ができる事に気づいた。

ISO3166.configure do |config| config.locales = [:af, :am, :ar, :as, :az, :be, :bg, :bn, :br, :bs, :ca, :cs, :cy, :da, :de, :dz, :el, :en, :eo, :es, :et, :eu, :fa, :fi, :fo, :fr, :ga, :gl, :gu, :he, :hi, :hr, :hu, :hy, :ia, :id, :is, :it, :ja, :ka, :kk, :km, :kn, :ko, :ku, :lt, :lv, :mi, :mk, :ml, :mn, :mr, :ms, :mt, :nb, :ne, :nl, :nn, :oc, :or, :pa, :pl, :ps, :pt, :ro, :ru, :rw, :si, :sk, :sl, :so, :sq, :sr, :sv, :sw, :ta, :te, :th, :ti, :tk, :tl, :tr, :tt, :ug, :uk, :ve, :vi, :wa, :wo, :xh, :zh, :zu] end

このように言語を多数指定した状態で再度 ISO3166::Country.new('JP') してみたところ以下のように複数の言語で日本を表記する事ができた。

country.translation('zh') # => "日本" country.translation('zu') # => "I-Japhani" country.translation('ru') # => "Япония" country.translation('tk') # => "Ýaponiýa" country.translation('ps') # => "جاپان" country.translation('vi') # => "Nhật" country.translation('th') # => "ญี่ปุ่น" country.translation('bn') # => "জাপান"

とりあえずこの gem を利用して一覧を表示してみた。

http://tools.loumo.jp/countries

これは以下のコードにより出力している。コントローラーは all メソッドを呼んでいるだけ。

class CountriesController < ApplicationController def index @countries = ISO3166::Country.all end end

View で each によりテーブルを生成している。

%table.table.table-striped %tr %th="" %th="name" %th="name_ja" %th="region" %th="subregion" %th="country code" %th="alpha-2" %th="alpha-3" %th="currency code" - @countries.each do |country| %tr %td=country.emoji_flag %th=country.name %td=country.translation(:ja) %td=country.region %td=country.subregion %td=country.country_code %td=country.alpha2 %td=country.alpha3 %td=country.currency_code

translation メソッドがこっちだと想定通りに動いているのでちゃんと日本語が表示される。

こんな感じで簡単に各国の情報を得る事ができる。どこからかスクレイピングする必要もない、便利だ。

Sponsored Link

コメント

タイトルとURLをコピーしました