Simple serializer
This commit is contained in:
parent
06359bbe0b
commit
d4b0b879a0
4 changed files with 30 additions and 2 deletions
1
Gemfile
1
Gemfile
|
@ -5,6 +5,7 @@ gem 'activerecord'
|
||||||
gem 'sinatra'
|
gem 'sinatra'
|
||||||
|
|
||||||
gem 'sinatra-activerecord'
|
gem 'sinatra-activerecord'
|
||||||
|
gem 'sinatra-active-model-serializers'
|
||||||
|
|
||||||
group :production do
|
group :production do
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
|
|
19
Gemfile.lock
19
Gemfile.lock
|
@ -1,6 +1,8 @@
|
||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
|
active_model_serializers (0.9.3)
|
||||||
|
activemodel (>= 3.2)
|
||||||
activemodel (4.2.4)
|
activemodel (4.2.4)
|
||||||
activesupport (= 4.2.4)
|
activesupport (= 4.2.4)
|
||||||
builder (~> 3.1)
|
builder (~> 3.1)
|
||||||
|
@ -15,6 +17,7 @@ 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)
|
||||||
|
backports (3.6.6)
|
||||||
builder (3.2.2)
|
builder (3.2.2)
|
||||||
ffi (1.9.10)
|
ffi (1.9.10)
|
||||||
i18n (0.7.0)
|
i18n (0.7.0)
|
||||||
|
@ -23,10 +26,13 @@ GEM
|
||||||
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)
|
||||||
pg (0.18.3)
|
pg (0.18.3)
|
||||||
rack (1.6.4)
|
rack (1.6.4)
|
||||||
rack-protection (1.5.3)
|
rack-protection (1.5.3)
|
||||||
rack
|
rack
|
||||||
|
rack-test (0.6.3)
|
||||||
|
rack (>= 1.0)
|
||||||
rb-fsevent (0.9.6)
|
rb-fsevent (0.9.6)
|
||||||
rb-inotify (0.9.5)
|
rb-inotify (0.9.5)
|
||||||
ffi (>= 0.5.0)
|
ffi (>= 0.5.0)
|
||||||
|
@ -36,9 +42,21 @@ GEM
|
||||||
rack (~> 1.4)
|
rack (~> 1.4)
|
||||||
rack-protection (~> 1.4)
|
rack-protection (~> 1.4)
|
||||||
tilt (>= 1.3, < 3)
|
tilt (>= 1.3, < 3)
|
||||||
|
sinatra-active-model-serializers (0.2.1)
|
||||||
|
active_model_serializers (>= 0.9.0)
|
||||||
|
sinatra (~> 1.4)
|
||||||
|
sinatra-activerecord (>= 2.0.0)
|
||||||
|
sinatra-contrib (>= 1.4.1)
|
||||||
sinatra-activerecord (2.0.9)
|
sinatra-activerecord (2.0.9)
|
||||||
activerecord (>= 3.2)
|
activerecord (>= 3.2)
|
||||||
sinatra (~> 1.0)
|
sinatra (~> 1.0)
|
||||||
|
sinatra-contrib (1.4.6)
|
||||||
|
backports (>= 2.0)
|
||||||
|
multi_json
|
||||||
|
rack-protection
|
||||||
|
rack-test
|
||||||
|
sinatra (~> 1.4.0)
|
||||||
|
tilt (>= 1.3, < 3)
|
||||||
sqlite3 (1.3.11)
|
sqlite3 (1.3.11)
|
||||||
thread_safe (0.3.5)
|
thread_safe (0.3.5)
|
||||||
tilt (2.0.1)
|
tilt (2.0.1)
|
||||||
|
@ -53,6 +71,7 @@ DEPENDENCIES
|
||||||
pg
|
pg
|
||||||
rerun
|
rerun
|
||||||
sinatra
|
sinatra
|
||||||
|
sinatra-active-model-serializers
|
||||||
sinatra-activerecord
|
sinatra-activerecord
|
||||||
sqlite3
|
sqlite3
|
||||||
|
|
||||||
|
|
9
app.rb
9
app.rb
|
@ -1,11 +1,16 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'bundler'
|
require 'bundler'
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
|
require 'json'
|
||||||
|
require 'sinatra/json'
|
||||||
Bundler.require
|
Bundler.require
|
||||||
|
|
||||||
require './config/environments'
|
require './config/environments'
|
||||||
|
|
||||||
require './model/joke'
|
require './model/joke'
|
||||||
|
require './serializers/joke_serializer'
|
||||||
|
|
||||||
|
register Sinatra::ActiveRecordExtension
|
||||||
|
|
||||||
before do
|
before do
|
||||||
content_type :json
|
content_type :json
|
||||||
|
@ -13,7 +18,7 @@ before do
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/jokes' do
|
get '/jokes' do
|
||||||
Joke.all.to_json
|
json Joke.all, root: false
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/jokes' do
|
post '/jokes' do
|
||||||
|
@ -34,7 +39,7 @@ get '/jokes/:id' do
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@joke.to_json
|
json @joke, root: false
|
||||||
end
|
end
|
||||||
|
|
||||||
put '/jokes/:id' do
|
put '/jokes/:id' do
|
||||||
|
|
3
serializers/joke_serializer.rb
Normal file
3
serializers/joke_serializer.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class JokeSerializer < ActiveModel::Serializer
|
||||||
|
attributes :joke, :punchline
|
||||||
|
end
|
Loading…
Reference in a new issue