lib/wurst/resource.rb in wurst-0.0.1 vs lib/wurst/resource.rb in wurst-0.0.3
- old
+ new
@@ -44,15 +44,14 @@
content_type :js, :charset => 'utf-8'
# @@models_js_template defined at bottom of module def
Tilt::ERBTemplate.new{@@models_js_template}.render
end
- Dir.glob(File.join('..', 'assets')) do |file|
- # TODO cross-platform? probably not.
- base.send :get, file.split('/').last do
+ %w(handlebars-v1.3.0.js ember.js ember-data.js).each do |filename|
+ base.send :get, "/#{filename}" do
content_type :js, :charset => 'utf-8'
- send_file file
+ File.read File.join(File.dirname(File.expand_path(__FILE__)), "../assets/#{filename}")
end
end
end
def resource model_sym, &block
@@ -68,10 +67,18 @@
router.switch
puts "***"
puts router.routes.inspect
+ # Parse JSON payload
+ # http://stackoverflow.com/a/17049683
+ before do
+ request.body.rewind
+ request_body = request.body.read
+ @request_payload = JSON.parse(request_body, :symbolize_names => true) unless request_body.empty?
+ end
+
# Find
get "/#{model_name_string}/:id" do |id|
content_type :json, :charset => 'utf-8'
if router.has_route? :read
{post: router.read(id)}.to_json
@@ -92,26 +99,26 @@
# Update
put "/#{model_name_string}/:id" do |id|
content_type :json, :charset => 'utf-8'
if router.has_route? :update
- {post: router.update(id, params)}.to_json
+ {post: router.update(id, @request_payload[:post])}.to_json
else
param_names = model.properties.select{ |prop| prop.class != DataMapper::Property::Serial }.map{ |prop| prop.name }
m = model.get(id)
- m.update(params.select{ |k,v| param_names.include? k.to_sym })
+ m.update(@request_payload[:post].select{ |k,v| param_names.include? k.to_sym })
{post: m}.to_json
end
end
# Create
post "/#{model_name_string}" do
content_type :json, :charset => 'utf-8'
if router.has_route? :create
- {post: router.create(params)}.to_json
+ {post: router.create(@request_payload[:post])}.to_json
else
param_names = model.properties.select{ |prop| prop.class != DataMapper::Property::Serial }.map{ |prop| prop.name }
- m = model.create(params.select{ |k,v| param_names.include? k.to_sym })
+ m = model.create(@request_payload[:post].select{ |k,v| param_names.include? k.to_sym })
{post: m}.to_json
end
end
# Delete
\ No newline at end of file