Use RABL to build JSON responses given by API
This commit is contained in:
parent
8681999d4c
commit
196ea16a94
2
Gemfile
2
Gemfile
@ -8,6 +8,8 @@ gem 'haml', '~> 3.1'
|
|||||||
|
|
||||||
gem 'bcrypt-ruby', '~> 3.0'
|
gem 'bcrypt-ruby', '~> 3.0'
|
||||||
|
|
||||||
|
gem 'rabl', '~> 0.6'
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem 'rspec-rails', '~> 2.6'
|
gem 'rspec-rails', '~> 2.6'
|
||||||
gem 'capybara', '~> 1.1'
|
gem 'capybara', '~> 1.1'
|
||||||
|
@ -70,6 +70,9 @@ GEM
|
|||||||
nokogiri (1.5.0)
|
nokogiri (1.5.0)
|
||||||
pg (0.11.0)
|
pg (0.11.0)
|
||||||
polyglot (0.3.3)
|
polyglot (0.3.3)
|
||||||
|
rabl (0.6.0)
|
||||||
|
activesupport (>= 2.3.14)
|
||||||
|
multi_json (~> 1.0.3)
|
||||||
rack (1.3.5)
|
rack (1.3.5)
|
||||||
rack-cache (1.1)
|
rack-cache (1.1)
|
||||||
rack (>= 0.4)
|
rack (>= 0.4)
|
||||||
@ -146,6 +149,7 @@ DEPENDENCIES
|
|||||||
haml (~> 3.1)
|
haml (~> 3.1)
|
||||||
libnotify (~> 0.6)
|
libnotify (~> 0.6)
|
||||||
pg (~> 0.11)
|
pg (~> 0.11)
|
||||||
|
rabl (~> 0.6)
|
||||||
rails (~> 3.1)
|
rails (~> 3.1)
|
||||||
rb-inotify (~> 0.8)
|
rb-inotify (~> 0.8)
|
||||||
rspec-rails (~> 2.6)
|
rspec-rails (~> 2.6)
|
||||||
|
@ -22,4 +22,8 @@ guard 'rspec', :cli => '--drb', :notification => false do
|
|||||||
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
|
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
|
||||||
|
|
||||||
watch(%r{^app/views/layouts/}) { 'spec/integration' }
|
watch(%r{^app/views/layouts/}) { 'spec/integration' }
|
||||||
|
|
||||||
|
watch(%r{^app/views/(.+)/(?:[^/]+).rabl}) do |m|
|
||||||
|
"spec/controllers/#{m[1]}_controller_spec.rb"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,6 +3,5 @@ class Api::V0::PlaylistsController < Api::ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@playlists = Playlist.all
|
@playlists = Playlist.all
|
||||||
respond_with @playlists
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
4
app/views/api/v0/playlists/index.rabl
Normal file
4
app/views/api/v0/playlists/index.rabl
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
collection @playlists
|
||||||
|
|
||||||
|
attribute :id
|
||||||
|
attribute :name
|
16
config/initializers/rabl_init.rb
Normal file
16
config/initializers/rabl_init.rb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Rabl.configure do |config|
|
||||||
|
# Commented as these are defaults
|
||||||
|
# config.cache_sources = false
|
||||||
|
# config.json_engine = nil # Any multi\_json engines
|
||||||
|
# config.msgpack_engine = nil # Defaults to ::MessagePack
|
||||||
|
# config.bson_engine = nil # Defaults to ::BSON
|
||||||
|
# config.plist_engine = nil # Defaults to ::Plist::Emit
|
||||||
|
# config.include_json_root = true
|
||||||
|
config.include_json_root = false
|
||||||
|
# config.include_msgpack_root = true
|
||||||
|
# config.include_bson_root = true
|
||||||
|
# config.include_plist_root = true
|
||||||
|
# config.include_xml_root = false
|
||||||
|
# config.enable_json_callbacks = false
|
||||||
|
# config.xml_options = { :dasherize => true, :skip_types => false }
|
||||||
|
end
|
@ -20,6 +20,10 @@ describe Api::V0::PlaylistsController do
|
|||||||
do_get.should have(2).items
|
do_get.should have(2).items
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'lists playlists with their id' do
|
||||||
|
do_get.each { |t| t.keys.should include 'id' }
|
||||||
|
end
|
||||||
|
|
||||||
it 'lists playlists with their name' do
|
it 'lists playlists with their name' do
|
||||||
do_get.each { |t| t.keys.should include 'name' }
|
do_get.each { |t| t.keys.should include 'name' }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user