Namespace the existing API as v0
This commit is contained in:
parent
fe5907e96f
commit
933c5d9801
@ -1,23 +0,0 @@
|
||||
class Api::AsciicastsController < ApplicationController
|
||||
|
||||
skip_before_filter :verify_authenticity_token
|
||||
|
||||
def create
|
||||
asciicast = asciicast_creator.create(attributes)
|
||||
render text: asciicast_url(asciicast), status: :created, location: asciicast
|
||||
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render nothing: true, status: 422
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def attributes
|
||||
AsciicastParams.build(params[:asciicast], request.user_agent)
|
||||
end
|
||||
|
||||
def asciicast_creator
|
||||
AsciicastCreator.new
|
||||
end
|
||||
|
||||
end
|
28
app/controllers/api/v0/asciicasts_controller.rb
Normal file
28
app/controllers/api/v0/asciicasts_controller.rb
Normal file
@ -0,0 +1,28 @@
|
||||
module Api
|
||||
module V0
|
||||
class AsciicastsController < ApplicationController
|
||||
|
||||
skip_before_filter :verify_authenticity_token
|
||||
|
||||
def create
|
||||
asciicast = asciicast_creator.create(attributes)
|
||||
render text: asciicast_url(asciicast), status: :created,
|
||||
location: asciicast
|
||||
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render nothing: true, status: 422
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def attributes
|
||||
AsciicastParams.build(params[:asciicast], request.user_agent)
|
||||
end
|
||||
|
||||
def asciicast_creator
|
||||
AsciicastCreator.new
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -27,9 +27,7 @@ Asciinema::Application.routes.draw do
|
||||
|
||||
resource :user
|
||||
|
||||
namespace :api do
|
||||
resources :asciicasts
|
||||
end
|
||||
post '/api/asciicasts' => 'api/v0/asciicasts#create'
|
||||
|
||||
root 'home#show'
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe "Asciicast upload" do
|
||||
subject { response.body }
|
||||
describe "Asciicast creation" do
|
||||
|
||||
before do
|
||||
post '/api/asciicasts', asciicast: {
|
@ -1,58 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Api::AsciicastsController do
|
||||
|
||||
describe '#create' do
|
||||
subject { post :create, asciicast: attributes }
|
||||
|
||||
let(:attributes) { { 'title' => 'bar' } }
|
||||
let(:creator) { double('creator') }
|
||||
|
||||
before do
|
||||
request.headers['User-Agent'] = 'Smith'
|
||||
allow(controller).to receive(:asciicast_creator) { creator }
|
||||
allow(AsciicastParams).to receive(:build).
|
||||
with(attributes, 'Smith') { { title: 'bar', user_agent: 'Smith' } }
|
||||
end
|
||||
|
||||
context 'when the creator returns an asciicast' do
|
||||
let(:asciicast) { stub_model(Asciicast, id: 666) }
|
||||
|
||||
before do
|
||||
allow(creator).to receive(:create).
|
||||
with(title: 'bar', user_agent: 'Smith') { asciicast }
|
||||
subject
|
||||
end
|
||||
|
||||
it 'returns the status 201' do
|
||||
expect(response.status).to eq(201)
|
||||
end
|
||||
|
||||
it 'returns the URL of created asciicast as the content body' do
|
||||
expect(response.body).to eq(asciicast_url(asciicast))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the creator raises ActiveRecord::RecordInvalid' do
|
||||
let(:asciicast) { stub_model(Asciicast, errors: errors) }
|
||||
let(:errors) { double('errors', full_messages: full_messages) }
|
||||
let(:full_messages) { ['This is invalid'] }
|
||||
|
||||
before do
|
||||
allow(creator).to receive(:create).
|
||||
with(title: 'bar', user_agent: 'Smith').
|
||||
and_raise(ActiveRecord::RecordInvalid.new(asciicast))
|
||||
post :create, asciicast: attributes
|
||||
end
|
||||
|
||||
it 'returns the status 422' do
|
||||
expect(response.status).to eq(422)
|
||||
end
|
||||
|
||||
it 'returns nothing as the content body' do
|
||||
expect(response.body).to be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
62
spec/controllers/api/v0/asciicasts_controller_spec.rb
Normal file
62
spec/controllers/api/v0/asciicasts_controller_spec.rb
Normal file
@ -0,0 +1,62 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Api
|
||||
module V0
|
||||
describe AsciicastsController do
|
||||
|
||||
describe '#create' do
|
||||
subject { post :create, asciicast: attributes }
|
||||
|
||||
let(:attributes) { { 'title' => 'bar' } }
|
||||
let(:creator) { double('creator') }
|
||||
|
||||
before do
|
||||
request.headers['User-Agent'] = 'Smith'
|
||||
allow(controller).to receive(:asciicast_creator) { creator }
|
||||
allow(AsciicastParams).to receive(:build).
|
||||
with(attributes, 'Smith') { { title: 'bar', user_agent: 'Smith' } }
|
||||
end
|
||||
|
||||
context 'when the creator returns an asciicast' do
|
||||
let(:asciicast) { stub_model(Asciicast, id: 666) }
|
||||
|
||||
before do
|
||||
allow(creator).to receive(:create).
|
||||
with(title: 'bar', user_agent: 'Smith') { asciicast }
|
||||
subject
|
||||
end
|
||||
|
||||
it 'returns the status 201' do
|
||||
expect(response.status).to eq(201)
|
||||
end
|
||||
|
||||
it 'returns the URL of created asciicast as the content body' do
|
||||
expect(response.body).to eq(asciicast_url(asciicast))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the creator raises ActiveRecord::RecordInvalid' do
|
||||
let(:asciicast) { stub_model(Asciicast, errors: errors) }
|
||||
let(:errors) { double('errors', full_messages: full_messages) }
|
||||
let(:full_messages) { ['This is invalid'] }
|
||||
|
||||
before do
|
||||
allow(creator).to receive(:create).
|
||||
with(title: 'bar', user_agent: 'Smith').
|
||||
and_raise(ActiveRecord::RecordInvalid.new(asciicast))
|
||||
post :create, asciicast: attributes
|
||||
end
|
||||
|
||||
it 'returns the status 422' do
|
||||
expect(response.status).to eq(422)
|
||||
end
|
||||
|
||||
it 'returns nothing as the content body' do
|
||||
expect(response.body).to be_blank
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
10
spec/routing/api_v0_spec.rb
Normal file
10
spec/routing/api_v0_spec.rb
Normal file
@ -0,0 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'routing to API v0' do
|
||||
it 'routes POST /api/asciicasts to api/v0/asciicasts#create for api_token' do
|
||||
expect(post: '/api/asciicasts').to route_to(
|
||||
controller: 'api/v0/asciicasts',
|
||||
action: 'create',
|
||||
)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user