From 933c5d98011b51d46c11da3ac792d905b967f4e8 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 25 Feb 2014 18:28:57 +0100 Subject: [PATCH] Namespace the existing API as v0 --- app/controllers/api/asciicasts_controller.rb | 23 ------- .../api/v0/asciicasts_controller.rb | 28 +++++++++ config/routes.rb | 4 +- .../v0/asciicast_create_spec.rb} | 3 +- .../api/asciicasts_controller_spec.rb | 58 ----------------- .../api/v0/asciicasts_controller_spec.rb | 62 +++++++++++++++++++ spec/routing/api_v0_spec.rb | 10 +++ 7 files changed, 102 insertions(+), 86 deletions(-) delete mode 100644 app/controllers/api/asciicasts_controller.rb create mode 100644 app/controllers/api/v0/asciicasts_controller.rb rename spec/{requests/asciicast_upload_spec.rb => api/v0/asciicast_create_spec.rb} (94%) delete mode 100644 spec/controllers/api/asciicasts_controller_spec.rb create mode 100644 spec/controllers/api/v0/asciicasts_controller_spec.rb create mode 100644 spec/routing/api_v0_spec.rb diff --git a/app/controllers/api/asciicasts_controller.rb b/app/controllers/api/asciicasts_controller.rb deleted file mode 100644 index f72f872..0000000 --- a/app/controllers/api/asciicasts_controller.rb +++ /dev/null @@ -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 diff --git a/app/controllers/api/v0/asciicasts_controller.rb b/app/controllers/api/v0/asciicasts_controller.rb new file mode 100644 index 0000000..724b310 --- /dev/null +++ b/app/controllers/api/v0/asciicasts_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index d64e208..b1e7d2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/spec/requests/asciicast_upload_spec.rb b/spec/api/v0/asciicast_create_spec.rb similarity index 94% rename from spec/requests/asciicast_upload_spec.rb rename to spec/api/v0/asciicast_create_spec.rb index dba7e00..6e3516e 100644 --- a/spec/requests/asciicast_upload_spec.rb +++ b/spec/api/v0/asciicast_create_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' -describe "Asciicast upload" do - subject { response.body } +describe "Asciicast creation" do before do post '/api/asciicasts', asciicast: { diff --git a/spec/controllers/api/asciicasts_controller_spec.rb b/spec/controllers/api/asciicasts_controller_spec.rb deleted file mode 100644 index d1378dc..0000000 --- a/spec/controllers/api/asciicasts_controller_spec.rb +++ /dev/null @@ -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 diff --git a/spec/controllers/api/v0/asciicasts_controller_spec.rb b/spec/controllers/api/v0/asciicasts_controller_spec.rb new file mode 100644 index 0000000..d0640ba --- /dev/null +++ b/spec/controllers/api/v0/asciicasts_controller_spec.rb @@ -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 diff --git a/spec/routing/api_v0_spec.rb b/spec/routing/api_v0_spec.rb new file mode 100644 index 0000000..a4fbf5f --- /dev/null +++ b/spec/routing/api_v0_spec.rb @@ -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