Jokes API in Grape
This commit is contained in:
parent
06359bbe0b
commit
c9cd546df4
4 changed files with 80 additions and 49 deletions
2
Gemfile
2
Gemfile
|
@ -6,6 +6,8 @@ gem 'sinatra'
|
|||
|
||||
gem 'sinatra-activerecord'
|
||||
|
||||
gem 'grape'
|
||||
|
||||
group :production do
|
||||
gem 'pg'
|
||||
end
|
||||
|
|
33
Gemfile.lock
33
Gemfile.lock
|
@ -15,16 +15,43 @@ GEM
|
|||
thread_safe (~> 0.3, >= 0.3.4)
|
||||
tzinfo (~> 1.1)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
ice_nine (0.11.1)
|
||||
json (1.8.3)
|
||||
listen (3.0.3)
|
||||
rb-fsevent (>= 0.9.3)
|
||||
rb-inotify (>= 0.9)
|
||||
minitest (5.8.1)
|
||||
multi_json (1.11.2)
|
||||
multi_xml (0.5.5)
|
||||
pg (0.18.3)
|
||||
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
|
||||
rb-fsevent (0.9.6)
|
||||
|
@ -44,12 +71,18 @@ GEM
|
|||
tilt (2.0.1)
|
||||
tzinfo (1.2.2)
|
||||
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
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
activerecord
|
||||
grape
|
||||
pg
|
||||
rerun
|
||||
sinatra
|
||||
|
|
87
app.rb
87
app.rb
|
@ -7,58 +7,53 @@ require './config/environments'
|
|||
|
||||
require './model/joke'
|
||||
|
||||
before do
|
||||
content_type :json
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
end
|
||||
class Jokes < Grape::API
|
||||
format :json
|
||||
|
||||
get '/jokes' do
|
||||
Joke.all.to_json
|
||||
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
|
||||
rescue_from ActiveRecord::RecordNotFound do |e|
|
||||
rack_response('{ "status": 404, "message": "Not Found." }', 404)
|
||||
end
|
||||
|
||||
@joke.to_json
|
||||
end
|
||||
|
||||
put '/jokes/:id' do
|
||||
begin
|
||||
@joke = Joke.find(params[:id])
|
||||
rescue Exception
|
||||
status 404
|
||||
return
|
||||
resource :jokes do
|
||||
desc 'Return all jokes'
|
||||
get do
|
||||
Joke.all
|
||||
end
|
||||
|
||||
if @joke.update(params[:joke])
|
||||
status 204
|
||||
else
|
||||
status 422
|
||||
desc 'Create new joke'
|
||||
params do
|
||||
requires :joke, type: String, desc: 'Joke'
|
||||
requires :punchline, type: String, desc: 'Punchline'
|
||||
end
|
||||
end
|
||||
|
||||
delete '/jokes/:id' do
|
||||
begin
|
||||
@joke = Joke.find(params[:id])
|
||||
|
||||
@joke.delete
|
||||
rescue Exception
|
||||
post do
|
||||
Joke.create!(
|
||||
joke: params[:joke],
|
||||
punchline: params[:punchline]
|
||||
)
|
||||
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
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
require './app'
|
||||
run Sinatra::Application
|
||||
|
||||
map('/') { run Jokes }
|
||||
|
|
Loading…
Reference in a new issue