Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Created February 1, 2012 02:07
Show Gist options
  • Save samwgoldman/1714534 to your computer and use it in GitHub Desktop.
Save samwgoldman/1714534 to your computer and use it in GitHub Desktop.
first stab at a webmachine-ruby resource def'n
require 'evaluation'
class EvaluationsResource < Webmachine::Resource
class << self
alias_method :let, :define_method
end
let(:allowed_methods) { ["GET", "POST"] }
let(:content_types_provided) { [["application/json", :to_json]] }
let(:content_types_accepted) { [["application/json", :from_json]] }
let(:resource_exists?) { !request.path_info.has_key?(:id) || Evaluation.exists?(request.path_info[:id]) }
let(:post_is_create?) { true }
let(:allow_missing_post?) { true }
let(:create_path) { "/evaluations/#{create_resource.id}" }
let(:from_json) { request.body.rewind; JSON.parse(request.body.read) }
let(:to_json) { resource_or_collection.to_json }
protected
def create_resource
@resource = Evaluation.create(from_json)
end
def resource
@resource ||= Evaluation.find_by_id(request.path_info[:id])
end
def collection
@collection ||= Evaluation.all
end
def resource_or_collection
resource || collection
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment