Jokes API in Grape

This commit is contained in:
Andrew Tomaka 2015-10-13 11:03:02 -04:00
parent 06359bbe0b
commit c9cd546df4
4 changed files with 80 additions and 49 deletions

View file

@ -6,6 +6,8 @@ gem 'sinatra'
gem 'sinatra-activerecord' gem 'sinatra-activerecord'
gem 'grape'
group :production do group :production do
gem 'pg' gem 'pg'
end end

View file

@ -15,16 +15,43 @@ GEM
thread_safe (~> 0.3, >= 0.3.4) thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1) tzinfo (~> 1.1)
arel (6.0.3) arel (6.0.3)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
builder (3.2.2) builder (3.2.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
equalizer (0.0.11)
ffi (1.9.10) ffi (1.9.10)
grape (0.13.0)
activesupport
builder
hashie (>= 2.1.0)
multi_json (>= 1.3.2)
multi_xml (>= 0.5.2)
rack (>= 1.3.0)
rack-accept
rack-mount
virtus (>= 1.0.0)
hashie (3.4.2)
i18n (0.7.0) i18n (0.7.0)
ice_nine (0.11.1)
json (1.8.3) json (1.8.3)
listen (3.0.3) listen (3.0.3)
rb-fsevent (>= 0.9.3) rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9) rb-inotify (>= 0.9)
minitest (5.8.1) minitest (5.8.1)
multi_json (1.11.2)
multi_xml (0.5.5)
pg (0.18.3) pg (0.18.3)
rack (1.6.4) rack (1.6.4)
rack-accept (0.4.5)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-protection (1.5.3) rack-protection (1.5.3)
rack rack
rb-fsevent (0.9.6) rb-fsevent (0.9.6)
@ -44,12 +71,18 @@ GEM
tilt (2.0.1) tilt (2.0.1)
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
activerecord activerecord
grape
pg pg
rerun rerun
sinatra sinatra

87
app.rb
View file

@ -7,58 +7,53 @@ require './config/environments'
require './model/joke' require './model/joke'
before do class Jokes < Grape::API
content_type :json format :json
response.headers['Access-Control-Allow-Origin'] = '*'
end
get '/jokes' do rescue_from ActiveRecord::RecordNotFound do |e|
Joke.all.to_json rack_response('{ "status": 404, "message": "Not Found." }', 404)
end
post '/jokes' do
@joke = Joke.new(params[:joke])
if @joke.save
status 201
else
status 422
end
end
get '/jokes/:id' do
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end end
@joke.to_json resource :jokes do
end desc 'Return all jokes'
get do
put '/jokes/:id' do Joke.all
begin
@joke = Joke.find(params[:id])
rescue Exception
status 404
return
end end
if @joke.update(params[:joke]) desc 'Create new joke'
status 204 params do
else requires :joke, type: String, desc: 'Joke'
status 422 requires :punchline, type: String, desc: 'Punchline'
end end
end post do
Joke.create!(
delete '/jokes/:id' do joke: params[:joke],
begin punchline: params[:punchline]
@joke = Joke.find(params[:id]) )
@joke.delete
rescue Exception
end end
status 204 route_param :id do
desc 'View a single joke'
get do
Joke.find(params[:id])
end
desc 'Update a joke'
params do
requires :joke, type: String, desc: 'Joke'
requires :punchline, type: String, desc: 'Punchline'
end
put do
Joke.find(params[:id]).update({
joke: params[:joke],
punchline: params[:punchline]
})
end
desc 'Delete a joke'
delete do
Joke.find(params[:id]).delete
end
end
end
end end

View file

@ -1,2 +1,3 @@
require './app' require './app'
run Sinatra::Application
map('/') { run Jokes }