HTML - RSS・Atom - Ruby で作成する

 クラウディア
1. 概要・参考サイト

1. 概要・参考サイト

 ruby のライブラリに RSS::Maker というのがあるそうで・・・。  本項は、「Ruby で RSS を作成する方法」を参考にさせていただいて書こうと思ったのですが。  なんせ ruby は初心者の域を出ないもので・・・。ようわからん。  どうも本家の「library rss (Ruby 2.5.0)」を見ながらやってみるほうができそうだ。  両方を合わせて作成してみたいと思います。

2. サンプルコードを動かしてみる

 参考サイトのサンプルコード通りに作成して出力してみました。  コードは、参考サイトをご参照ください。  同じモジュールで RSS1.0、RSS2.0、Atom の形式で出力してみました。  同一のモジュールで  RSS1.0 形式で出力するとこういう結果
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://example.com/index.xsl" type="text/xsl"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/"
  xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
  xmlns:image="http://purl.org/rss/1.0/modules/image/">
  <channel rdf:about="http://example.com/atom.xml">
    <title>Example</title>
    <link>http://example.com/</link>
    <description>Example Site</description>
    <image rdf:resource="http://example.com/logo.png"/>
    <items>
      <rdf:Seq>
        <rdf:li resource="http://example.com/article2.html"/>
        <rdf:li resource="http://example.com/article.html"/>
      </rdf:Seq>
    </items>
    <textinput rdf:resource="http://example.com/search.cgi"/>
    <dc:date>2018-10-12T09:18:53.693244+09:00</dc:date>
  </channel>
  <image rdf:about="http://example.com/logo.png">
    <title>Example Site</title>
    <url>http://example.com/logo.png</url>
    <link>http://example.com/</link>
  </image>
  <item rdf:about="http://example.com/article2.html">
    <title>Sample Article2</title>
    <link>http://example.com/article2.html</link>
    <dc:date>2004-11-02T10:10:00+09:00</dc:date>
  </item>
  <item rdf:about="http://example.com/article.html">
    <title>Sample Article</title>
    <link>http://example.com/article.html</link>
    <dc:date>2004-11-01T10:10:00+09:00</dc:date>
  </item>
  <textinput rdf:about="http://example.com/search.cgi">
    <title>Search Example Site</title>
    <description>Search Example Site's all text</description>
    <name>keyword</name>
    <link>http://example.com/search.cgi</link>
  </textinput>
</rdf:RDF>

 RSS2.0 形式で出力するとこういう結果
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://example.com/index.xsl" type="text/xsl"?>
<rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
  <channel>
    <title>Example</title>
    <link>http://example.com/</link>
    <description>Example Site</description>
    <pubDate>Fri, 12 Oct 2018 09:18:53 +0900</pubDate>
    <image>
      <url>http://example.com/logo.png</url>
      <title>Example Site</title>
      <link>http://example.com/</link>
    </image>
    <item>
      <title>Sample Article2</title>
      <link>http://example.com/article2.html</link>
      <pubDate>Tue, 02 Nov 2004 10:10:00 +0900</pubDate>
      <dc:date>2004-11-02T10:10:00+09:00</dc:date>
    </item>
    <item>
      <title>Sample Article</title>
      <link>http://example.com/article.html</link>
      <pubDate>Mon, 01 Nov 2004 10:10:00 +0900</pubDate>
      <dc:date>2004-11-01T10:10:00+09:00</dc:date>
    </item>
    <textInput>
      <title>Search Example Site</title>
      <description>Search Example Site's all text</description>
      <name>keyword</name>
      <link>http://example.com/search.cgi</link>
    </textInput>
    <dc:date>2018-10-12T09:18:53.67728+09:00</dc:date>
  </channel>
</rss>

 Atom 形式で出力するとこういう結果
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://example.com/index.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/">
  <author>
    <name>Bob</name>
  </author>
  <id>http://example.com/atom.xml</id>
  <link href="http://example.com/"/>
  <logo>http://example.com/logo.png</logo>
  <subtitle>Example Site</subtitle>
  <title>Example</title>
  <updated>2018-10-12T09:18:53.701305+09:00</updated>
  <entry>
    <id>http://example.com/article2.html</id>
    <link href="http://example.com/article2.html"/>
    <title>Sample Article2</title>
    <updated>2004-11-02T10:10:00+09:00</updated>
    <dc:date>2004-11-02T10:10:00+09:00</dc:date>
  </entry>
  <entry>
    <id>http://example.com/article.html</id>
    <link href="http://example.com/article.html"/>
    <title>Sample Article</title>
    <updated>2004-11-01T10:10:00+09:00</updated>
    <dc:date>2004-11-01T10:10:00+09:00</dc:date>
  </entry>
  <dc:date>2018-10-12T09:18:53.701305+09:00</dc:date>
</feed>

 もともとウェブスクレイピングを ruby でやってみたので、それが応用できそうです。  ruby で作ってみようと思います。  よって、作成自体は「php」の方で記録を残していきます。
スポンサーリンク