Skip to content

Commit

Permalink
Merge branch 'remove-x-prefix-from-header-strategy'
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
bwillis committed Dec 2, 2014
2 parents c076d04 + c74f837 commit 6c18d37
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Bug Fixes:

* When an invalid version is received an unsupported exception is raised (#34)
* Remove deprecated X- from header

Enhancements:

Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ gem install versioncake
| [>2.0](CHANGELOG.md#200-feb-6-2014) | Yes | Yes | Yes | No |
| [>2.4](CHANGELOG.md#200-feb-6-2014) | Yes | Yes | Yes | Yes |

## Upgrade v2.0 -> v3.0

### Accept header name changes

The default accept header was changed from 'X-API-Version' to 'API-Version'. If you require the 'X-' or some other variant, you can specify a custom strategy as outlined in Extraction Strategy section below.

## Upgrade v1.* -> v2.0

### Filename changes
Expand Down Expand Up @@ -217,7 +223,7 @@ Strategy | Description | Example
:query_parameter | version in the url query parameter, for testing or to override for special case | `http://localhost:3000/posts.json?api_version=1` (This is the default.)
:path_parameter | version in the url path parameter | `api/v:api_version/`
request_parameter | version that is sent in the body of the request | Good for testing.
:http_header | Api version HTTP header | `X-API-Version: 1`
:http_header | Api version HTTP header | `API-Version: 1`
:http_accept_parameter | HTTP Accept header | `Accept: application/xml; version=1` [why do this?](http://blog.steveklabnik.com/posts/2011-07-03-nobody-understands-rest-or-http#i_want_my_api_to_be_versioned)
custom | takes the request object and must return an integer | lambda {|request| request.headers["HTTP_X_MY_VERSION"].to_i } or class ExtractorStrategy; def execute(request);end;end

Expand Down Expand Up @@ -296,7 +302,7 @@ class ApplicationController < ActionController::Base
private

def render_unsupported_version
headers['X-API-Version-Supported'] = 'false'
headers['API-Version-Supported'] = 'false'
respond_to do |format|
format.json { render json: {message: "You requested an unsupported version (#{requested_version})"}, status: :unprocessable_entity }
end
Expand Down
4 changes: 2 additions & 2 deletions lib/versioncake/strategies/http_header_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module VersionCake
class HttpHeaderStrategy < ExtractionStrategy

def execute(request)
if request.headers.key? "HTTP_X_#{version_key.upcase}"
request.headers["HTTP_X_#{version_key.upcase}"]
if request.headers.key? "HTTP_#{version_key.upcase}"
request.headers["HTTP_#{version_key.upcase}"]
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/test_cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
# http_header_strategy
- request:
headers:
HTTP_X_API_VERSION: "1"
HTTP_API_VERSION: "1"
response: "template v1"

- request:
headers:
HTTP_X_API_VERSION: "2"
HTTP_API_VERSION: "2"
response: "template v2"

# general
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/strategies/http_header_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
subject { strategy.extract(request) }

context "a request with an HTTP_X_API_VERSION retrieves the version" do
let(:request) { instance_double('Request', headers: {'HTTP_X_API_VERSION' => '11'}) }
let(:request) { instance_double('Request', headers: {'HTTP_API_VERSION' => '11'}) }

it { is_expected.to eq 11 }
end
Expand Down

0 comments on commit 6c18d37

Please sign in to comment.