<?xml version="1.0" encoding="UTF-8"?>
<post>
  <alt>Black Sabbath, Technical Ecstasy</alt>
  <body>&lt;p&gt;On dit que le cordonnier est souvent mal chauss&#233;. C'est un concept qui se refl&#232;te aussi sur le m&#233;tier de d&#233;veloppement et de programmation ; J'ai finalement d&#233;cid&#233; d'impl&#233;menter un fil RSS chez Maxula, et comme vous allez le voir, il suffit de 5 minutes de concentration pour balancer du RSS partout dans un site, un site sous RoR bien s&#251;r.&lt;/p&gt;
&lt;p&gt;Rails nous permet d'avoir differents types de sortie avec le m&#234;me contenu, que ce soit du html, du xml, du json, .... l'avantage du MVC.&lt;br/&gt;
Avant l'arriv&#233;e de Rails 2.0, il fallait orienter le controlleur vers la vue ad&#233;quate avec la fonction &lt;span&gt;respond_to&lt;/span&gt;, voici &#224; quoi &#231;a ressemble au niveau du controlleur
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;iv&quot;&gt;@posts&lt;/span&gt; = &lt;span class=&quot;co&quot;&gt;Post&lt;/span&gt;.find(&lt;span class=&quot;sy&quot;&gt;:all&lt;/span&gt;)
  
  respond_to &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt; |format|
      format.html
      format.xml { render &lt;span class=&quot;sy&quot;&gt;:xml&lt;/span&gt; =&amp;gt; &lt;span class=&quot;iv&quot;&gt;@posts&lt;/span&gt;}
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Avec Rails 2.0 on pose uniquement l'instance &#224; placer dans la vue, &#231;a se simplifie et &#231;a devient&lt;/p&gt;

&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;r&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;iv&quot;&gt;@posts&lt;/span&gt; = &lt;span class=&quot;co&quot;&gt;Post&lt;/span&gt;.find(&lt;span class=&quot;sy&quot;&gt;:all&lt;/span&gt;)
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Tout se joue au niveau des vues. Vous avez compris!, il y aura une vue pour la sortie html classique et une autre vue pour le RSS. Voici un aspect de l'arborescence sous la vue&lt;/p&gt;
&lt;img src=&quot;/images/rss/arborescence.png&quot; alt=&quot;RSS arborescence&quot; /&gt;
&lt;p&gt;Quand on lance la requ&#234;te &lt;span&gt;www.monsiteweb.com/posts/index.rss&lt;/span&gt; avec l'extension &lt;span&gt;.rss&lt;/span&gt; on appelle la vue correspondante.&lt;/p&gt;
&lt;p&gt;Maintenant on va aborder la partie la plus difficile, voyons &#224; quoi ressemble le contenu de la vue RSS : le fichier &lt;span&gt;app/views/posts/index.rss.builder&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
xml.instruct! &lt;span class=&quot;sy&quot;&gt;:xml&lt;/span&gt;, &lt;span class=&quot;sy&quot;&gt;:version&lt;/span&gt; =&amp;gt;&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
xml.rss &lt;span class=&quot;sy&quot;&gt;:version&lt;/span&gt; =&amp;gt; &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt;
    xml.channel &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt;
    xml.title &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;Articles&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    xml.description &lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;Ensemble des Articles&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    xml.link formatted_posts_url(&lt;span class=&quot;sy&quot;&gt;:rss&lt;/span&gt;)
    
    &lt;span class=&quot;r&quot;&gt;for&lt;/span&gt; post &lt;span class=&quot;r&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;iv&quot;&gt;@posts&lt;/span&gt;
      xml.item &lt;span class=&quot;r&quot;&gt;do&lt;/span&gt;
        xml.title post.title
        xml.description post.short
        xml.pubDate post.created_at.to_s(&lt;span class=&quot;sy&quot;&gt;:rfc822&lt;/span&gt;)
        xml.link post_url(post)
     &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;r&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;La premi&#232;re partie du code concerne l'en-t&#234;te du fichier XML, le RSS quoi !. La deuxi&#232;me c'est une boucle pour lire les donn&#233;es de l'instance envoy&#233;e du controleur. Voici ce que &#231;a donne &#224; la sortie, de suite il n'y a plus grand chose &#224; expliquer.&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;
&lt;span class=&quot;pp&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;ta&quot;&gt;&amp;lt;rss&lt;/span&gt; &lt;span class=&quot;an&quot;&gt;version&lt;/span&gt;=&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;ta&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;ta&quot;&gt;&amp;lt;channel&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Articles&lt;span class=&quot;ta&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;description&amp;gt;&lt;/span&gt;Ensemble des Articles&lt;span class=&quot;ta&quot;&gt;&amp;lt;/description&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;link&amp;gt;&lt;/span&gt;http://maxula.net/posts.rss&lt;span class=&quot;ta&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;item&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;BrowserCMS&lt;span class=&quot;ta&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;description&amp;gt;&lt;/span&gt;Un autre CMS sous Ruby on Rails.&lt;span class=&quot;ta&quot;&gt;&amp;lt;/description&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;pubDate&amp;gt;&lt;/span&gt;Thu, 30 Jul 2009 10:20:22 +0000&lt;span class=&quot;ta&quot;&gt;&amp;lt;/pubDate&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;link&amp;gt;&lt;/span&gt;http://maxula.net/posts/browsercms&lt;span class=&quot;ta&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;item&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Comatose&lt;span class=&quot;ta&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;description&amp;gt;&lt;/span&gt;un CMS minimaliste, tr&lt;span class=&quot;en&quot;&gt;&amp;amp;#232;&lt;/span&gt;s souple&lt;span class=&quot;ta&quot;&gt;&amp;lt;/description&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;pubDate&amp;gt;&lt;/span&gt;Wed, 12 Aug 2009 00:55:29 +0000&lt;span class=&quot;ta&quot;&gt;&amp;lt;/pubDate&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;ta&quot;&gt;&amp;lt;link&amp;gt;&lt;/span&gt;http://maxula.net/posts/Comatose-CMS&lt;span class=&quot;ta&quot;&gt;&amp;lt;/link&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;ta&quot;&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;
    ...
    ...
    ...
  &lt;span class=&quot;ta&quot;&gt;&amp;lt;/channel&amp;gt;&lt;/span&gt;

&lt;span class=&quot;ta&quot;&gt;&amp;lt;/rss&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Je recommande d'utiliser l'attribut &lt;span&gt;updated_at&lt;/span&gt; et non le &lt;span&gt;created_at&lt;/span&gt; dans la g&#233;n&#233;ration du XML, le lecteur de flux RSS d&#233;tectera automatiquement une mise &#224; jour.&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;

xml.pubDate post.updated_at.to_s(&lt;span class=&quot;sy&quot;&gt;:rfc822&lt;/span&gt;)

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;De nouveau on observe la puissance de l'architecture REST, le lien qui pointe vers la vue RSS est &lt;span&gt;formatted_posts_url(:rss)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Le code simplifi&#233; pour attacher le lien &#224; l'icone du fil RSS aura l'aspect suivant:&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;

&amp;lt;&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; link_to (image_tag(&amp;quot;rss.gif&amp;quot;), formatted_posts_url(:rss) ) %&amp;gt;

&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;On va rajouter une derni&#232;re chose pour &#234;tre dans les normes d'accessibilit&#233; surtout pour les navigateurs qui ont leur propre lecteur de flux RSS; une ligne de code &#224; l'en-t&#234;te du fichier HTML, entre les balises &lt;span&gt;HEAD&lt;/span&gt; du &lt;strong&gt;Layout&lt;/strong&gt;. Il s'agit du fichier &lt;span&gt;app/views/layouts/applications.html.erb&lt;/span&gt; par d&#233;faut et cette ligne sera la suivante:&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;

&lt;span class=&quot;ta&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span class=&quot;an&quot;&gt;href&lt;/span&gt;=&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;/posts.rss&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;an&quot;&gt;rel&lt;/span&gt;=&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;alternate&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;an&quot;&gt;title&lt;/span&gt;=&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;RSS&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;an&quot;&gt;type&lt;/span&gt;=&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;application/rss+xml&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;ta&quot;&gt;/&amp;gt;&lt;/span&gt;

&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Fa&#231;on Rails &#231;a ressemble plut&#244;t &#224; &#231;a:&lt;/p&gt;
&lt;div class=&quot;CodeRay&quot;&gt;
  &lt;div class=&quot;code&quot;&gt;&lt;pre&gt;

&amp;lt;&lt;span class=&quot;s&quot;&gt;&lt;span class=&quot;dl&quot;&gt;%=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt; auto_discovery_link_tag :rss, formatted_posts_path(:rss) %&amp;gt;

&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Cette ligne permet d'afficher un raccourci pour le RSS dans la barre d'adresse du navigateur.&lt;/p&gt;
&lt;p&gt;La Classe Builder:XMLMarkup de l'ActiveSupport, nous &#224; facilit&#233; la vie pour g&#233;n&#233;rer la structure du code XML et la puissance du concept REST, nous &#224; facilit&#233; la cr&#233;ation des liens pour chaque vue. Il y a de quoi m&#233;diter sur cet exemple, pour g&#233;n&#233;rer des vues plus complexes dans d'autres formats sans limites, bonne m&#233;ditation !&lt;/p&gt;
</body>
  <cat-id type="integer">1</cat-id>
  <created-at type="datetime">2009-08-20T18:17:44Z</created-at>
  <id type="integer">9</id>
  <pagetitle>int&#233;gration de RSS avec Ruby on Rails</pagetitle>
  <photo-content-type>image/png</photo-content-type>
  <photo-file-name>technical-ecstasy.png</photo-file-name>
  <photo-file-size type="integer">22603</photo-file-size>
  <short>Il est souvent interessant de rajouter un lien de syndication sur son site, que ce soit un blog, des articles, des commentaires...
Voici quelques lignes de code sous RoR d&#233;crivant une solution d'int&#233;gration de la syndication dans les applications Ruby on Rails</short>
  <title>RSS et Rails</title>
  <updated-at type="datetime">2009-08-24T22:04:06Z</updated-at>
  <url>rss-ruby-on-rails</url>
</post>
