From: Brian Candler Date: 2009-01-21T00:05:14+09:00 Subject: Re: Desktop <-> Web That simplifies things a bit. Here it is for completeness: --- myapp.rb --- require 'rubygems' require 'sinatra' require 'json' require 'widget' get '/widgets.json' do Widget.all.to_json end get '/widgets/:id.json' do Widget.find(Integer(params[:id])).to_json end post '/widgets.json' do item = JSON.parse(request.body.read) Widget.create(item).to_s end --- widget.rb --- # Rubbish model, not thread-safe! class Widget attr_accessor :id, :name, :price def initialize(params) @id = params["id"] @name = params["name"] @price = params["price"] end def to_json(*a) instance_variables.inject({}) { |h,v| h[v.to_s[1..-1]] = instance_variable_get(v) h }.to_json(*a) end @all = [] @seq = 0 def self.all @all end def self.add(item) item.id = (@seq += 1) all << item return @seq end def self.create(params) add(new(params)) end def self.find(id) all.find { |item| item.id == id } end end Widget.create("name" => "flurble", "price" => 12.3) Widget.create("name" => "boing", "price" => 4.56) If you require 'json/add/rails' then you don't need to define your own to_json method, but it also allows people to create arbitrary Ruby objects on your machine (which makes me uncomfortable) -- Posted via http://www.ruby-forum.com/.