ActiveResourceでライブドア全国の地点定義表RSSを読み込む
ActiveResourceでRSSファイルの読み込みはどうするのか知りたかったので、全国の地点定義表を読み込んでみました。
環境
Rails 2.3.5
Ruby 1.8.7
モデル
:fromに取得したいxmlファイルのパスを記述します。
class Livedoorweather < ActiveResource::Base self.site = "http://weather.livedoor.com/" def self.list Livedoorweather.find(:one, :from => "/forecast/rss/forecastmap.xml") end end
コントローラ
class LivedoorweathersController < ApplicationController def index @weathers = Livedoorweather.list respond_to do |format| format.html # index.html.erb format.xml { render :xml => @weathers } end end end
ビュー
このレベルはこんな感じ。
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <title>1次細分区定義表 - livedoor 天気情報</title>
<p><%= @weathers.channel.title %></p>
もう一つ下がるとこんな感じ。
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <image> <title>livedoor 天気情報</title>
<p><%= @weathers.channel.image.title %></p>
ネームスペースは不要です。
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <ldWeather:source title="全国" link="http://weather.livedoor.com/forecast/rss/index.xml">
<p><%= @weathers.channel.source.title %></p>
繰り返しは、こんな風に。
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <ldWeather:source title="全国" link="http://weather.livedoor.com/forecast/rss/index.xml"> <area title="北海道地方" source="http://weather.livedoor.com/forecast/rss/1.xml"> <pref title="道北"> <warn title="警報・注意報" source="http://weather.livedoor.com/forecast/rss/warn/1a.xml" /> <city title="稚内" id="1" source="http://weather.livedoor.com/forecast/rss/1a/1.xml" /> <city title="旭川" id="2" source="http://weather.livedoor.com/forecast/rss/1a/2.xml" /> <city title="留萌" id="3" source="http://weather.livedoor.com/forecast/rss/1a/3.xml" /> </pref> <pref title="道央"> <warn title="警報・注意報" source="http://weather.livedoor.com/forecast/rss/warn/1b.xml" /> <city title="札幌" id="4" source="http://weather.livedoor.com/forecast/rss/1b/4.xml" /> <city title="岩見沢" id="5" source="http://weather.livedoor.com/forecast/rss/1b/5.xml" /> <city title="倶知安" id="6" source="http://weather.livedoor.com/forecast/rss/1b/6.xml" /> </pref>
<table class="table"> <tr> <th>Area.Title</th> </tr> <% @weathers.channel.source.area.each do |area| %> <tr> <td><%=h area.title %></td> </tr> <% end %> </table>
以降のアクセスには、ノードの指定が必要です。
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ldWeather="http://weather.livedoor.com/ns/rss/2.0"> <channel> <ldWeather:source title="全国" link="http://weather.livedoor.com/forecast/rss/index.xml"> <area title="北海道地方" source="http://weather.livedoor.com/forecast/rss/1.xml"> <pref title="道北"> <warn title="警報・注意報" source="http://weather.livedoor.com/forecast/rss/warn/1a.xml" /> <city title="稚内" id="1" source="http://weather.livedoor.com/forecast/rss/1a/1.xml" /> <city title="旭川" id="2" source="http://weather.livedoor.com/forecast/rss/1a/2.xml" /> <city title="留萌" id="3" source="http://weather.livedoor.com/forecast/rss/1a/3.xml" /> </pref> <pref title="道央"> <warn title="警報・注意報" source="http://weather.livedoor.com/forecast/rss/warn/1b.xml" /> <city title="札幌" id="4" source="http://weather.livedoor.com/forecast/rss/1b/4.xml" /> <city title="岩見沢" id="5" source="http://weather.livedoor.com/forecast/rss/1b/5.xml" /> <city title="倶知安" id="6" source="http://weather.livedoor.com/forecast/rss/1b/6.xml" /> </pref>
<table class="table"> <tr> <th>Pref.Title</th> </tr> <% @weathers.channel.source.area[0].pref.each do |pref| %> <tr> <td><%=h pref.title %></td> </tr> <% end %> </table>
さらにその下のノード。
<table class="table"> <tr> <th>City.Title</th> </tr> <% @weathers.channel.source.area[0].pref[0].city.each do |city| %> <tr> <td><%=h city.id %></td> <td><%=h city.title %></td> </tr> <% end %> </table>