Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api fixes #17

Merged
merged 3 commits into from
Mar 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions app/controllers/api/v1/credentials_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/api_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def can_sort?(resource)
can_do?(:sort, resource)
end

end
end
4 changes: 0 additions & 4 deletions app/views/api/v1/credentials/me.json.jbuilder

This file was deleted.

4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
end

namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraints.new(version: 1) do
# ApiConstraints.default should be set to true for the latest version,
# all other versions should not have default set to true
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
get '/me' => 'credentials#me'

resources :users, only: [:show, :update] do
Expand Down
4 changes: 2 additions & 2 deletions lib/api_constraints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ def initialize(options)
end

def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.exercises.openstax.v#{@version}")
@default || req.headers['Accept'].try(:include?, "application/vnd.exercises.openstax.v#{@version}")
end
end
end
2 changes: 1 addition & 1 deletion spec/features/add_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
login_as 'admin'
expect(page).to have_content('Welcome, admin')
visit '/oauth/applications'
expect(page).to have_content('OAuth2 Provider')
expect(page).to have_content('OAuth Applications')
create_new_application
expect(page).to have_content('Application created.')
expect(page).to have_content('Application: example')
Expand Down
69 changes: 69 additions & 0 deletions spec/lib/api_constraints_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'spec_helper'

describe ApiConstraints do
context 'default is not defined' do
let!(:constraints) { ApiConstraints.new(version: 1) }
let(:req) { double('Request') }
it 'matches if version is correct in the accept headers' do
req.stub(:headers).and_return({
'Accept' => 'application/vnd.exercises.openstax.v1'
})
expect(constraints.matches? req).to be_true
end

it 'does not match if version is incorrect in the accept headers' do
req.stub(:headers).and_return({
'Accept' => 'application/vnd.exercises.openstax.v2'
})
expect(constraints.matches? req).to be_false
end

it 'does not match if version is not defined in the accept headers' do
req.stub(:headers).and_return({
'Accept' => '*/*',
})
expect(constraints.matches? req).to be_false
end

it 'does not match if accept is not in headers' do
req.stub(:headers).and_return({
'Host' => 'localhost'
})
expect(constraints.matches? req).to be_nil
end
end

context 'default is defined' do
let!(:default) { double('default') }
let!(:constraints) { ApiConstraints.new(version: 1, default: default) }
let(:req) { double('Request') }

it 'matches if version is correct in the accept headers' do
req.stub(:headers).and_return({
'Accept' => 'application/vnd.exercises.openstax.v1'
})
expect(constraints.matches? req).to be_true
end

it 'returns default if version is incorrect in the accept headers' do
req.stub(:headers).and_return({
'Accept' => 'application/vnd.exercises.openstax.v2'
})
expect(constraints.matches? req).to eq(default)
end

it 'returns default if version is not defined in the accept headers' do
req.stub(:headers).and_return({
'Accept' => '*/*',
})
expect(constraints.matches? req).to eq(default)
end

it 'returns default if accept is not in headers' do
req.stub(:headers).and_return({
'Host' => 'localhost'
})
expect(constraints.matches? req).to eq(default)
end
end
end