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 'sinatra-activerecord'
|
||||||
|
|
||||||
|
gem 'grape'
|
||||||
|
|
||||||
group :production do
|
group :production do
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
end
|
end
|
||||||
|
|
33
Gemfile.lock
33
Gemfile.lock
|
@ -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
|
||||||
|
|
75
app.rb
75
app.rb
|
@ -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'] = '*'
|
|
||||||
|
rescue_from ActiveRecord::RecordNotFound do |e|
|
||||||
|
rack_response('{ "status": 404, "message": "Not Found." }', 404)
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/jokes' do
|
resource :jokes do
|
||||||
Joke.all.to_json
|
desc 'Return all jokes'
|
||||||
|
get do
|
||||||
|
Joke.all
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/jokes' do
|
desc 'Create new joke'
|
||||||
@joke = Joke.new(params[:joke])
|
params do
|
||||||
|
requires :joke, type: String, desc: 'Joke'
|
||||||
if @joke.save
|
requires :punchline, type: String, desc: 'Punchline'
|
||||||
status 201
|
|
||||||
else
|
|
||||||
status 422
|
|
||||||
end
|
end
|
||||||
|
post do
|
||||||
|
Joke.create!(
|
||||||
|
joke: params[:joke],
|
||||||
|
punchline: params[:punchline]
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/jokes/:id' do
|
route_param :id do
|
||||||
begin
|
desc 'View a single joke'
|
||||||
@joke = Joke.find(params[:id])
|
get do
|
||||||
rescue Exception
|
Joke.find(params[:id])
|
||||||
status 404
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@joke.to_json
|
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
|
end
|
||||||
|
|
||||||
put '/jokes/:id' do
|
desc 'Delete a joke'
|
||||||
begin
|
delete do
|
||||||
@joke = Joke.find(params[:id])
|
Joke.find(params[:id]).delete
|
||||||
rescue Exception
|
|
||||||
status 404
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if @joke.update(params[:joke])
|
|
||||||
status 204
|
|
||||||
else
|
|
||||||
status 422
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
delete '/jokes/:id' do
|
|
||||||
begin
|
|
||||||
@joke = Joke.find(params[:id])
|
|
||||||
|
|
||||||
@joke.delete
|
|
||||||
rescue Exception
|
|
||||||
end
|
end
|
||||||
|
|
||||||
status 204
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
require './app'
|
require './app'
|
||||||
run Sinatra::Application
|
|
||||||
|
map('/') { run Jokes }
|
||||||
|
|
Loading…
Reference in a new issue