RubyでRSSのリクエスト&レスポンスを実装します。

RSSをリクエストします。

require 'rss'
require 'open-uri'

url = 'http://www.ruby-lang.org/en/feeds/news.rss'
open(url) do |rss|
  feed = RSS::Parser.parse(rss)
  puts "Title: #{feed.channel.title}"
  feed.items.each do |item|
    puts "Item: #{item.title}"
  end
end

リクエスト実行結果

Title: Ruby News
Item: We retire Ruby 1.8.7
Item: Ruby 2.0.0-p247 is released
Item: Ruby 1.9.3-p448 is released
Item: Ruby 1.8.7-p374 is released
Item: Hostname check bypassing vulnerability in SSL client (CVE-2013-4073)
Item: A brand-new ruby-lang.org has been released
Item: Ruby 1.9.3-p429 is released
Item: Ruby 2.0.0-p195 is released
Item: Object taint bypassing in DL and Fiddle in Ruby (CVE-2013-2065)
Item: Ruby 2.0.0-p0 is released

RSSをレスポンスします。

require "rss"

rss = RSS::Maker.make("atom") do |maker|
  maker.channel.author = "matz"
  maker.channel.updated = Time.now.to_s
  maker.channel.about = "http://www.ruby-lang.org/en/feeds/news.rss"
  maker.channel.title = "Example Feed"

  maker.items.new_item do |item|
    item.link = "http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"
    item.title = "Ruby 1.9.2-p136 is released"
    item.updated = Time.now.to_s
  end
end

puts rss

レスポンス実行結果

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/">
  <author>
    <name>matz</name>
  </author>
  <id>http://www.ruby-lang.org/en/feeds/news.rss</id>
  <title>Example Feed</title>
  <updated>2013-07-23T19:37:09+09:00</updated>
  <entry>
    <id>http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/</id>
    <link href="http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"/>
    <title>Ruby 1.9.2-p136 is released</title>
    <updated>2013-07-23T19:37:09+09:00</updated>
    <dc:date>2013-07-23T19:37:09+09:00</dc:date>
  </entry>
  <dc:date>2013-07-23T19:37:09+09:00</dc:date>
</feed>

かなり簡単に実装出来そうですね。

さて、ちょっと応用してみますか。
ソースはイケてない箇所ありますが、とりあえず動かすこと優先で

指定のRSSからRSSを取得して、RSSとして出力する機能

CallRss.rb


require 'rss'
require 'open-uri'
require './ResponseRss.rb'

class CallRss
    
    url = 'http://www.ruby-lang.org/en/feeds/news.rss'

    feeds = []
    open(url) do |rss|
      feed = RSS::Parser.parse(rss)      

      feeds.push(feed.channel.title)

      feed.items.each do |item|
        feeds.push(item.link)
        feeds.push(item.title)
      end
    end
    
    responseRss = ResponseRss.new()

    channelAuthor = "asklife"
    channelAbout = url
    channelTitle = feeds[0]
    itemLink = []
    itemTitle = []
    countNo = 1
    feeds.each do |value|
      itemLink.push(feeds[countNo])
      itemTitle.push(feeds[countNo+1])
      countNo += countNo
    end

    xml = responseRss.response(channelAuthor , channelAbout , channelTitle , itemLink , itemTitle)
    
    puts xml

end

RSSに整形する機能
課題メモ:固定長なことが課題、可変にしたい。引数を柔軟にしたい。メソッドが長い。

ResponseRss.rb

class ResponseRss

  def response(channelAuthor , channelAbout , channelTitle , itemLink , itemTitle)
    require "rss"
    rss = RSS::Maker.make("atom") do |maker|
      maker.channel.author = channelAuthor
      maker.channel.updated = Time.now.to_s
      maker.channel.about = channelAbout
      maker.channel.title = channelTitle
    

      maker.items.new_item do |item|
        item.link = itemLink[0]
        item.title = itemTitle[0]
        item.updated = Time.now.to_s
      end
      
      maker.items.new_item do |item|
        item.link = itemLink[1]
        item.title = itemTitle[1]
        item.updated = Time.now.to_s
      end      

      maker.items.new_item do |item|
        item.link = itemLink[2]
        item.title = itemTitle[2]
        item.updated = Time.now.to_s
      end      

      
    end
    return rss
  end

end

参考
http://www.ruby-doc.org/stdlib……c/RSS.html