Skip to content

Commit

Permalink
Merge pull request #18 from elfassy/path_parameter_strategy
Browse files Browse the repository at this point in the history
path parameters strategy
  • Loading branch information
bwillis committed Oct 30, 2013
2 parents 8ca3091 + 59130de commit e1db224
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,15 @@ You can also define the way to extract the version. The `view_version_extraction
config.view_version_extraction_strategy = :query_parameter # [:http_header, :http_accept_parameter]
```
These are the available strategies:
- **query_parameter**: version in the url query parameter, for testing or to override for special case i.e. ```http://localhost:3000/posts.json?api_version=1``` (This is the default.)
- **request_parameter**: version that is sent in the body of the request. Good for testing.
- **http_header**: Api version HTTP header ie. ```X-API-Version: 1```
- **http_accept_parameter**: HTTP Accept header ie. ```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**: `lambda {|request| request.headers["HTTP_X_MY_VERSION"].to_i }` takes the request object and must return an integer

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_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 }


#### Default Version
Expand Down
1 change: 1 addition & 0 deletions lib/versioncake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'versioncake/strategies/http_accept_parameter_strategy'
require 'versioncake/strategies/http_header_strategy'
require 'versioncake/strategies/query_parameter_strategy'
require 'versioncake/strategies/path_parameter_strategy'
require 'versioncake/strategies/request_parameter_strategy'
require 'versioncake/strategies/custom_strategy'

Expand Down
11 changes: 11 additions & 0 deletions lib/versioncake/strategies/path_parameter_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module VersionCake
class PathParameterStrategy < ExtractionStrategy

def execute(request)
if request.path_parameters.key? @@version_string.to_sym
request.path_parameters[@@version_string.to_sym]
end
end

end
end
17 changes: 17 additions & 0 deletions test/unit/strategies/path_parameter_strategy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require './test/test_helper'

class PathParameterStrategyTest < ActiveSupport::TestCase
setup do
@strategy = VersionCake::PathParameterStrategy.new
end

test "a request with an api_version path parameter retrieves the version" do
request = stub(:path_parameters => {:api_version => '11', :other => 'parameter'})
assert_equal 11, @strategy.extract(request)
end

test "a request without an api_version path parameter returns nil" do
request = stub(:path_parameters => {:other => 'parameter', :another => 'parameter'})
assert_nil @strategy.extract(request)
end
end

0 comments on commit e1db224

Please sign in to comment.