8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Reality を使って Ruby であらゆる情報を簡単に取ってくる

Last updated at Posted at 2016-03-20

molybdenum-99/reality: Comprehensive data proxy to knowledge about real world

Wikipedia API を使って、あらゆる情報を、数珠つなぎに取って来れる Gem。あるオブジェクトから、次のオブジェクトに、情報がどんどんつながっていくところがオブジェクト指向にマッチしていて、おもしろい。

使ってみる

例えば、Japan を調べてみる。

require 'reality'
include Reality::Methods

ja = Entity('Japan')

ja.population
#=> #<Reality::Measure(128,057,352 person)>
ja.capital
#=> #<Reality::Entity?(Tokyo Prefecture)>
ja.currency
#=> #<Reality::Entity?(Japanese yen)>

人口、首都、通貨など、様々な情報が取り出せることが分かります。
ちなみに、describe とすれば、取れる属性がまるっと分かります。

ja.describe

#=>
---------------------------------
#<Reality::Entity(Japan):country>
---------------------------------
 adm_divisions: #<Reality::List[Tokyo Prefecture?, Kagoshima Prefecture?, Hokkaidō?, Tochigi Prefecture?, Miyagi Prefecture?, Iwate Prefecture?, Aomori Prefecture?, Fukushima Prefecture?, Chiba Prefecture?, Aichi Prefecture?, Akita Prefecture?, Ibaraki Prefecture?, Kyōto Prefecture?, Ōsaka Prefecture?, Fukuoka Prefecture?, Ehime Prefecture?, Yamagata Prefecture?, Yamaguchi Prefecture?, Kanagawa Prefecture?, Nagano Prefecture?, Saitama Prefecture?, Mie Prefecture?, Gunma Prefecture?, Hyōgo Prefecture?, Miyazaki Prefecture?, Kumamoto Prefecture?, Gifu Prefecture?, Ishikawa Prefecture?, Nara Prefecture?, Wakayama Prefecture?, Shizuoka Prefecture?, Shiga Prefecture?, Niigata Prefecture?, Yamanashi Prefecture?, Shimane Prefecture?, Toyama Prefecture?, Okayama Prefecture?, Fukui Prefecture?, Ōita Prefecture?, Tottori Prefecture?, Kōchi Prefecture?, Saga Prefecture?, Tokushima Prefecture?, Kagawa Prefecture?, Nagasaki Prefecture?, Hiroshima Prefecture?, Okinawa Prefecture?]>
          area: #<Reality::Measure(377,944 km²)>
  calling_code: "+81"
       capital: #<Reality::Entity?(Tokyo Prefecture)>
     continent: #<Reality::Entity?(Asia)>
         coord: #<Reality::Geo::Coord(35°0′0″N,136°0′0″E)>
       country: #<Reality::Entity?(Japan)>
    created_at: #<Date: 1947-05-03 ((2432309j,0s,0n),+0s,2299161j)>
      currency: #<Reality::Entity?(Japanese yen)>
       follows: #<Reality::Entity?(Empire of Japan)>
   gdp_nominal: #<Reality::Measure(4,601,461,206,885 $)>
       gdp_ppp: #<Reality::Measure(4,843,000,000,000 $)>
 head_of_state: #<Reality::Entity?(Akihito)>
 highest_point: #<Reality::Entity?(Mount Fuji)>
     iso2_code: "JP"
     iso3_code: "JPN"
     long_name: "Japan"
    neighbours: #<Reality::List[People's Republic of China?, Russia?, North Korea?, Taiwan?, South Korea?]>
 organizations: #<Reality::List[United Nations?, World Trade Organization?, G-20 major economies?, G8?, Organisation for Economic Co-operation and Development?, Asia-Pacific Economic Cooperation?, Asian Development Bank?, International Bank for Reconstruction and Development?, African Development Bank?, International Finance Corporation?, Australia Group?, International Development Association?, International Centre for Settlement of Investment Disputes?, Multilateral Investment Guarantee Agency?, Association of Southeast Asian Nations Regional Forum?]>
         parts: #<Reality::List[list of regions of Japan?, island of Japan?]>
    population: #<Reality::Measure(128,057,352 person)>
           tld: ".jp"
     tz_offset: #<Reality::TZOffset(UTC+09:00)>

すごい!

ただ、ここで、終わらないのが Reality の素敵なところ。
よくみると値一つ一つが Reality オブジェクトになっているので、さらにデータを取り出す事ができます。

例えば

ja.head_of_state
#=> #<Reality::Entity?(Akihito)>

ja.head_of_state.birthday
#=> #<Date: 1933-12-23 ((2427430j,0s,0n),+0s,2299161j)>

ja.head_of_state.birth_place
#=> #<Reality::Entity?(Tokyo Prefecture)>

ja.head_of_state.birth_place.population
#=> #<Reality::Measure(13,512,186 person)>

へー天皇は 1933/12/23 の東京生まれなんだ。
その東京には、13,512,186 人いるのか。。。

と Ruby でネットサーフィンが出来てしまう!おもしろい!

いろいろな人や国の情報が取って来れるので、遊び以外でもいろいろと使えそうですね。まあ、Wikipedia の情報っていうのが若干あれですけど!

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?