Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Using REST and XML Builder for
legacy XML
Keith Pitty
http://keithpitty.com
REST is great for responding to different formats
class NewsItemsController < ApplicationController

  def index
    @news_items = NewsItem.all

    respond_to do |format|
...
@news_items.to_xml
# == Schema Information
#
# Table name: news_items
#
# id          :integer(4)     not null, primary key
# heading     :st...
So it’s easy to provide XML via to_xml, right?
But what if a legacy XML format is imposed?
<?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
<newsletter>
  <summary>
    <title>The Trinity Tribune</title>
   ...
Use presenter object and Builder::XmlBuilder
class NewsletterController < ApplicationController

  def show
    @newsletter = Newsletter.new
    respond_to do |format|...
remember the legacy xml format?
<?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
<newsletter>
  <summary>
    <title>The Trinity Tribune</title>
   ...
app/views/newsletters/show.xml.builder

     contains Builder::XmlMarkup
xml.instruct!
xml.newsletter {
  xml.summary {
    xml.title quot;The Trinity Tribunequot;
    xml.date quot;#{Date.today....
Presenter class
class Newsletter
  attr_reader :articles, :feature_article

  def initialize
    @articles = NewsItem.all_except_for_featu...
this example is simple and contrived
hopefully you can imagine how this technique
 could be useful in more complex examples
Summary


REST needs a bit of help sometimes
XML Builder useful for producing XML in legacy format
Presenter object can he...
Upcoming SlideShare
Loading in …5
×

Using REST and XML Builder for legacy XML

3,090 views

Published on

A lightning talk given at the June 2009 Sydney Ruby Meetup about how to use REST, XML Builder and a Presenter to provide XML in a legacy format.

Published in: Technology, News & Politics
  • http://www.dbmanagement.info/Tutorials/XML.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Using REST and XML Builder for legacy XML

  1. 1. Using REST and XML Builder for legacy XML Keith Pitty http://keithpitty.com
  2. 2. REST is great for responding to different formats
  3. 3. class NewsItemsController < ApplicationController def index @news_items = NewsItem.all respond_to do |format| format.html # index.rhtml format.xml { render :xml => @news_items.to_xml } end end def show @news_item = NewsItem.find(params[:id]) respond_to do |format| format.html # show.rhtml format.xml { render :xml => @news_item.to_xml } end end end
  4. 4. @news_items.to_xml
  5. 5. # == Schema Information # # Table name: news_items # # id :integer(4) not null, primary key # heading :string(255) # author :string(255) # body :text # created_at :datetime # updated_at :datetime # class NewsItem < ActiveRecord::Base end
  6. 6. So it’s easy to provide XML via to_xml, right?
  7. 7. But what if a legacy XML format is imposed?
  8. 8. <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <newsletter> <summary> <title>The Trinity Tribune</title> <date>June 9th, 2009</date> </summary> <feature> <article heading=quot;Railscamp 5 a Roaring Successquot; by=quot;Lachie Coxquot;> Railscamp 5 started out so well. Saturday was awesome... </article> </feature> <article heading=quot;Bivou.ac blows Heroku away!quot; by=quot;Martin Stannardquot;> Move over Heroku, bivou.ac is here! </article> <article heading=quot;Bananas de jourquot; by=quot;Tim Lucasquot;> Bon jour, campers! Wanna share some cool code? </article> </newsletter>
  9. 9. Use presenter object and Builder::XmlBuilder
  10. 10. class NewsletterController < ApplicationController def show @newsletter = Newsletter.new respond_to do |format| format.xml { render :action => quot;show.xml.builderquot;, :layout => false } end end end
  11. 11. remember the legacy xml format?
  12. 12. <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <newsletter> <summary> <title>The Trinity Tribune</title> <date>June 9th, 2009</date> </summary> <feature> <article heading=quot;Railscamp 5 a Roaring Successquot; by=quot;Lachie Coxquot;> Railscamp 5 started out so well. Saturday was awesome... </article> </feature> <article heading=quot;Bivou.ac blows Heroku away!quot; by=quot;Martin Stannardquot;> Move over Heroku, bivou.ac is here! </article> <article heading=quot;Bananas de jourquot; by=quot;Tim Lucasquot;> Bon jour, campers! Wanna share some cool code? </article> </newsletter>
  13. 13. app/views/newsletters/show.xml.builder contains Builder::XmlMarkup
  14. 14. xml.instruct! xml.newsletter { xml.summary { xml.title quot;The Trinity Tribunequot; xml.date quot;#{Date.today.to_formatted_s(:rfc822)}quot; } xml.feature { @newsletter.feature_article(quot;headingquot; => feature_article.heading, quot;byquot; => feature_article.author) } @newsletter.articles.each { |article| xml.article(quot;headingquot; => article.heading, quot;byquot; => article.author) } }
  15. 15. Presenter class
  16. 16. class Newsletter attr_reader :articles, :feature_article def initialize @articles = NewsItem.all_except_for_feature end def feature_article @feature_article = NewsItem.feature end # Potential for more useful methods here end
  17. 17. this example is simple and contrived
  18. 18. hopefully you can imagine how this technique could be useful in more complex examples
  19. 19. Summary REST needs a bit of help sometimes XML Builder useful for producing XML in legacy format Presenter object can help encapsulate what is required in the XML markup

×