jokes-api/app.rb

65 lines
842 B
Ruby
Raw Normal View History

2015-10-12 16:30:43 -04:00
require 'rubygems'
require 'bundler'
require 'bundler/setup'
Bundler.require
require './config/environments'
require './model/joke'
before do
content_type :json
response.headers['Access-Control-Allow-Origin'] = '*'
end
get '/jokes' do
Joke.all.to_json
end
post '/jokes' do
@joke = Joke.new(params[:joke])
if @joke.save
2015-10-12 16:44:28 -04:00
status 201
2015-10-12 16:30:43 -04:00
else
status 422
end
end
get '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end
@joke.to_json
end
put '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end
if @joke.update(params[:joke])
status 204
else
status 422
end
end
delete '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
@joke.delete
rescue Exception
end
status 204
end