Skip to content

Instantly share code, notes, and snippets.

irb(main)> resource = Restfulie.at('http://localhost:3000/items').get.resource
Response:
ready to execute request using stack typeRestfulie::Client::Feature::BaseRequestargstypeRestfulie::Client::Feature::RescueExceptionargstypeRestfulie::Client::Feature::SetupHeaderargstypeRestfulie::Client::Feature::SerializeBodyargstypeRestfulie::Client::Feature::EnhanceResponseargstypeRestfulie::Client::Feature::FollowRequestargs
invoking filter Restfulie::Client::Feature::FollowRequest with #<Restfulie::Client::Dsl:0xb708ff7c> at
invoking filter Restfulie::Client::Feature::EnhanceResponse with #<Restfulie::Client::Dsl:0xb708ff7c> at
invoking filter Restfulie::Client::Feature::SerializeBody with #<Restfulie::Client::Dsl:0xb708ff7c> at
invoking filter Restfulie::Client::Feature::SetupHeader with #<Restfulie::Client::Dsl:0xb708ff7c> at
invoking filter Restfulie::Client::Feature::RescueException with #<Restfulie::Client::Dsl:0xb708ff7c> at
@hemalich
hemalich / show.tokamak
Created April 27, 2011 08:24
/views/items
member(@item) do |m|
m.link "self", item_url(@item)
m.values { |v|
v.name @item.name
v.price @item.price}
end
@hemalich
hemalich / index.tokamak
Created April 27, 2011 08:22
/views/items
collection(@items, :root => "items") do |items|
items.link "self", items_url
# setting :root is unnecessary here but is useful for customizing element names.
items.members(:root => "item") do |m, item|
m.link :self, item_url(item)
m.values { |values|
values.name item.name
values.price item.price }
end
end
// To create a new item using Restfulie
irb(main)> resource = Restfulie.at('http://localhost:3000/items').as("application/xml").post!(:item => { :name => "Mobile", :price => 5000})
// To access newly created Item
irb(main)> p resource.resource.item.name
"Mobile"
=> nil
def show
respond_with @item = Item.find(params[:id])
end
def create
@item = Item.create(params[:item])
render :text => "", :status => 201, :location => item_url(@item)
end
irb(main)> response = Restfulie.at('http://localhost:3000/items').accepts('application/json').get
Output:
ready to execute request using stack
typeRestfulie::Client::Feature::BaseRequestargstypeRestfulie::Client::Feature::RescueExceptionargstypeRestfulie::Client::Feature::SetupHeaderargstypeRestfulie::Client::Feature::SerializeBodyargstypeRestfulie::Client::Feature::EnhanceResponseargstypeRestfulie::Client::Feature::FollowRequestargs
invoking filter Restfulie::Client::Feature::FollowRequest with #<Restfulie::Client::Dsl:0xb6a12d5c> at
invoking filter Restfulie::Client::Feature::EnhanceResponse with #<Restfulie::Client::Dsl:0xb6a12d5c> at
invoking filter Restfulie::Client::Feature::SerializeBody with #<Restfulie::Client::Dsl:0xb6a12d5c> at
invoking filter Restfulie::Client::Feature::SetupHeader with #<Restfulie::Client::Dsl:0xb6a12d5c> at
curl -i "http://localhost:3000/items" -H "Accept:application/json"
HTTP/1.1 200 OK
X-Ua-Compatible: IE=Edge
Etag: "579a54772674bf702106c51fc48d9d2b"
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Date: Fri, 08 Apr 2011 08:50:52 GMT
Server: WEBrick/1.3.1 (Ruby/1.8.7/2008-08-11)
X-Runtime: 0.655732
Item.create :name => "Pendrive", :price => "500"
Item.create :name => "Laptop", :price => "25000"
> curl -i "http://localhost:3000/items"
RESPONSE :
HTTP/1.1 200 OK
X-Ua-Compatible: IE=Edge
Etag: "4f31ca96db448bb738a3923db737871d"
Connection: Keep-Alive
Content-Type: application/xml; charset=utf-8
Date: Fri, 08 Apr 2011 08:47:08 GMT
class ItemsController < ApplicationController
// To notify Rails that we are capable of handling xml and json request
respond_to : xml, :json
// To notify that this controller behave as a Restfulie controller
acts_as_restfulie
def index
@items = Item.all
respond_with @items