From c9cd546df4196caa98272100b9977df64255a015 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 13 Oct 2015 11:03:02 -0400 Subject: [PATCH] Jokes API in Grape --- Gemfile | 2 ++ Gemfile.lock | 33 +++++++++++++++++++ app.rb | 91 +++++++++++++++++++++++++--------------------------- config.ru | 3 +- 4 files changed, 80 insertions(+), 49 deletions(-) diff --git a/Gemfile b/Gemfile index d01d958..32a0aa5 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,8 @@ gem 'sinatra' gem 'sinatra-activerecord' +gem 'grape' + group :production do gem 'pg' end diff --git a/Gemfile.lock b/Gemfile.lock index b234b50..ef1b5b1 100644 --- a/Gemfile.lock +++ b/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 diff --git a/app.rb b/app.rb index afe2241..3d2994b 100644 --- a/app.rb +++ b/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 + resource :jokes do + desc 'Return all jokes' + get do + Joke.all + end -put '/jokes/:id' do - begin - @joke = Joke.find(params[:id]) - rescue Exception - status 404 - return - end + desc 'Create new joke' + params do + requires :joke, type: String, desc: 'Joke' + requires :punchline, type: String, desc: 'Punchline' + end + post do + Joke.create!( + joke: params[:joke], + punchline: params[:punchline] + ) + end - if @joke.update(params[:joke]) - status 204 - else - status 422 + 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 - -delete '/jokes/:id' do - begin - @joke = Joke.find(params[:id]) - - @joke.delete - rescue Exception - end - - status 204 -end diff --git a/config.ru b/config.ru index 76a6edf..6382519 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,3 @@ require './app' -run Sinatra::Application + +map('/') { run Jokes }