Use RABL to build JSON responses given by API

This commit is contained in:
Thibault Jouan 2012-03-11 23:25:57 +00:00
parent 8681999d4c
commit 196ea16a94
7 changed files with 34 additions and 1 deletions

View File

@ -8,6 +8,8 @@ gem 'haml', '~> 3.1'
gem 'bcrypt-ruby', '~> 3.0'
gem 'rabl', '~> 0.6'
group :development, :test do
gem 'rspec-rails', '~> 2.6'
gem 'capybara', '~> 1.1'

View File

@ -70,6 +70,9 @@ GEM
nokogiri (1.5.0)
pg (0.11.0)
polyglot (0.3.3)
rabl (0.6.0)
activesupport (>= 2.3.14)
multi_json (~> 1.0.3)
rack (1.3.5)
rack-cache (1.1)
rack (>= 0.4)
@ -146,6 +149,7 @@ DEPENDENCIES
haml (~> 3.1)
libnotify (~> 0.6)
pg (~> 0.11)
rabl (~> 0.6)
rails (~> 3.1)
rb-inotify (~> 0.8)
rspec-rails (~> 2.6)

View File

@ -22,4 +22,8 @@ guard 'rspec', :cli => '--drb', :notification => false do
watch('app/controllers/application_controller.rb') { 'spec/controllers' }
watch(%r{^app/views/layouts/}) { 'spec/integration' }
watch(%r{^app/views/(.+)/(?:[^/]+).rabl}) do |m|
"spec/controllers/#{m[1]}_controller_spec.rb"
end
end

View File

@ -3,6 +3,5 @@ class Api::V0::PlaylistsController < Api::ApplicationController
def index
@playlists = Playlist.all
respond_with @playlists
end
end

View File

@ -0,0 +1,4 @@
collection @playlists
attribute :id
attribute :name

View 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

View File

@ -20,6 +20,10 @@ describe Api::V0::PlaylistsController do
do_get.should have(2).items
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
do_get.each { |t| t.keys.should include 'name' }
end