Implement track/index action for API
This commit is contained in:
parent
fc1ec120ae
commit
9b9e17e591
5
app/controllers/api/tracks_controller.rb
Normal file
5
app/controllers/api/tracks_controller.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Api::TracksController < Api::ApplicationController
|
||||||
|
def index
|
||||||
|
@tracks = Track.all
|
||||||
|
end
|
||||||
|
end
|
6
app/views/api/tracks/index.rabl
Normal file
6
app/views/api/tracks/index.rabl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
collection @tracks
|
||||||
|
|
||||||
|
attribute :id
|
||||||
|
attribute :name
|
||||||
|
|
||||||
|
node(:sound_url, :if => ->(t) { t.sound? }) { |t| api_sound_url t.sound }
|
@ -1,6 +1,7 @@
|
|||||||
Scube::Application.routes.draw do
|
Scube::Application.routes.draw do
|
||||||
namespace :api do
|
namespace :api do
|
||||||
resources :sounds, :only => [:show]
|
resources :sounds, :only => [:show]
|
||||||
|
resources :tracks, :only => [:index]
|
||||||
resources :playlists, :only => [:index, :create]
|
resources :playlists, :only => [:index, :create]
|
||||||
resources :sessions, :only => [:create]
|
resources :sessions, :only => [:create]
|
||||||
|
|
||||||
|
37
spec/controllers/api/tracks_controller_spec.rb
Normal file
37
spec/controllers/api/tracks_controller_spec.rb
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Api::TracksController do
|
||||||
|
include UserControllerHelpers
|
||||||
|
|
||||||
|
before do
|
||||||
|
sign_in
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET index' do
|
||||||
|
before do
|
||||||
|
Factory.create(:track_with_sound)
|
||||||
|
Factory.create(:track_with_sound)
|
||||||
|
end
|
||||||
|
|
||||||
|
def do_get
|
||||||
|
get :index, :format => :json
|
||||||
|
JSON response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'lists all tracks' do
|
||||||
|
do_get.should have(2).items
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'lists tracks with their id' do
|
||||||
|
do_get.each { |p| p.should include 'id' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'lists tracks with their name' do
|
||||||
|
do_get.each { |p| p.should include 'name' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'lists tracks with sound URL' do
|
||||||
|
do_get.each { |p| p.should include 'sound_url' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
28
spec/integration/api/tracks_spec.rb
Normal file
28
spec/integration/api/tracks_spec.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
feature 'API tracks' do
|
||||||
|
include UserIntegrationHelpers
|
||||||
|
|
||||||
|
background do
|
||||||
|
api_sign_in
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'lists tracks' do
|
||||||
|
track_1 = Factory.create(:track_with_sound, :name => 'Track 1')
|
||||||
|
track_2 = Factory.create(:track, :name => 'Track 2')
|
||||||
|
|
||||||
|
get api_tracks_path, :format => :json
|
||||||
|
|
||||||
|
response.body.should == [
|
||||||
|
{
|
||||||
|
:id => track_1.id,
|
||||||
|
:name => 'Track 1',
|
||||||
|
:sound_url => api_sound_url(track_1.sound)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
:id => track_2.id,
|
||||||
|
:name => 'Track 2'
|
||||||
|
}
|
||||||
|
].to_json
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user