Hey Raimon,

I see that you've been experimenting with Camping and Reststop lately,
and just thought I should chime in a bit.

You definitely don't *need* Reststop in order to achieve what you
want, so it might be a good idea to just leave Reststop until it gets
a little more robust. Let's see how we can tackle your problem with
Camping only:

For serving XML, you can use Builder (http://builder.rubyforge.org/).
Here's a little helper for you:

require 'camping'
require 'builder'

Camping.goes :App

module App
  # We include the Views module so we can call them as methods.
  include Views

  module Helpers
    def xml(name)
      # We'll need to send this as text/xml
      @headers["Content-Type"] = "text/xml"
      result = String.new
      # The builder takes a `target` where the XML will end up
      builder = Builder::XmlMarkup.new(:target => result, :indent => 2)
      # Generates a <?xml version="1.0" encoding="UTF-8" ?>
      builder.instruct!
      # Calls the method you sent in, passing in the builder as an argument
      send(name, builder)
      # Return the restult
      result
    end
  end
end

----

If you for instance want to generate this XML:

<posts>
  <post title="Post title">Content of post</post>
</posts>

You would have the following view:

module App::Views
  def posts(xml)
    xml.posts do
      @posts.each do |post|
        xml.post(post.content, :title => post.title)
      end
    end
  end
end

And if you want this XML:

<?xml version="1.0" encoding="UTF-8"?>
<posts>
  <post>
    <title>Hiya</title>
    <content>Hey</content>
  </post>
</posts>

You have this view:

module App::Views
  def posts(xml)
    xml.posts do
      @posts.each do |post|
        xml.post do
          xml.title(post.title)
          xml.content(post.content)
        end
      end
    end
  end
end

-----

You render the XML in the controller like this:

module App::Controllers
  class Index
    def get
      # The view has access to this variable
      @posts = Post.all
      # Calls the helper, which in turn calls the view
      xml :posts
    end
  end
end

----

That's (hopefully) the simplest way to generate XML with Camping.

You still need to create a model to store/retrieve the data. Before we
can help you here, we need to know a few things: Is it going to fetch
data from a specific place, or should it create its own database (from
scratch)? Any specific database you want to use?

Here's a Pastie with all the code: http://pastie.org/1008983 (Should
work on any version of Camping).

// Magnus Holm



On Tue, Jun 8, 2010 at 08:25, Raimon Fernandez <co...@montx.com> wrote:
> hi list,
>
>
> This is my first time here, my first time reading seriously something about 
> Camping.
>
> I need to create a very simple web server for serving only .xml files, 
> extracted from a sqlite database with some queries.
>
> I'm quite comfortable with Ruby on Rails, but it would be too much for this 
> project, so I've decided to take a look at Camping or Sinatra, not sure 
> what's the best option.
>
> There would be only 5 tables, 100 rows per table, and the idea is fetch data 
> from a device like iPad/iPhone/iPod, update it and persist the changes in the 
> server. The data transfer would be in plain .xml files, no html or css.
>
> Any helpful directions would be great
>
> :-)
>
> thanks,
>
> r.
> _______________________________________________
> Camping-list mailing list
> Camping-list@rubyforge.org
> http://rubyforge.org/mailman/listinfo/camping-list
>
_______________________________________________
Camping-list mailing list
Camping-list@rubyforge.org
http://rubyforge.org/mailman/listinfo/camping-list

Reply via email to