Skip to content

Instantly share code, notes, and snippets.

@bernerdschaefer
Created January 12, 2012 14:20
Show Gist options
  • Save bernerdschaefer/1600780 to your computer and use it in GitHub Desktop.
Save bernerdschaefer/1600780 to your computer and use it in GitHub Desktop.
module WebmachineExample
# GET /notes/1
# returns a 404
# GET /notes/2
# returns a 200
# POST /notes
# with an invalid content type
# returns a 415
# does not set the location
# with a valid type
# returns a 201
# sets the location
# PUT /notes/2
# returns a 204
# PUT /notes/2 (invalid content type)
# returns a 415
# POST /notes/1
# returns a 405
# PUT /notes/1
# returns a 404
# DELETE /notes/1
# returns a 404
# DELETE /notes/2
# returns a 204
Application.routes do
add ["notes"],
->(request) { request.method == "POST" },
Resources::Note
add ["notes"], Resources::NoteList
add ["notes", :id], Resources::Note
end
module Resources
class NoteList < Webmachine::Resource
def allowed_methods
%w[ HEAD GET ]
end
def to_html
"list"
end
end
class Note < Webmachine::Resource
def content_types_accepted
[
["application/json", :accept_json]
]
end
def accept_json
end
def allow_missing_post?
!request.path_info.has_key?(:id)
end
def resource_exists?
if request.path_info.has_key?(:id)
request.path_info[:id] == "2" || 404
else
false
end
end
def allowed_methods
if request.path_info.has_key?(:id)
%w[ HEAD GET PUT DELETE ]
else
%w[ POST ]
end
end
def post_is_create?
!request.path_info.has_key?(:id)
end
def create_path
"/notes/1"
end
def delete_resource
true
end
def to_html
"note"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment