Skip to content

Commit

Permalink
Add specs and example for multiple strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
twinge committed Jul 24, 2012
1 parent 2ae857a commit 87832c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.markdown
Expand Up @@ -117,6 +117,32 @@ MyApi::Application.routes.draw do
end
```

### Using Multiple Strategies

It is possible to combine two or more of the above strategies. One
example of when you might want to support multiple strategies
simultaneously is if you have an API that supports JSONP. Your preferred
strategy might be for users of the API to set a header, but since JSONP
doesn't support setting custom headers, you also need to support either
:path or :parameter.

Example:

```ruby
MyApi::Application.routes.draw do
api_version(:module => 'V2', :header => 'API-VERSION', :parameter => "version", :value => 'v2', :path => 'v2') do
match '/foos.(:format)' => 'foos#index', :via => :get
match '/foos_no_format' => 'foos#index', :via => :get
resources :bars
end
end
```

Caveat: If you are using :header and :parameter, they need to share the
same value. For this reason, if you use mupltiple strategies, you will
probably want to set a custom header instead of using an Accept mime
type.

### Default Version

If a request is made to your API without specifying a specific version, by default a RoutingError (i.e. 404) will occur. You can optionally configure Versionist to
Expand Down
32 changes: 32 additions & 0 deletions spec/api_routing_spec.rb
Expand Up @@ -632,6 +632,38 @@
end
end
end

context "multi strategy" do
before :each do
TestApi::Application.routes.draw do
api_version({:module => mod, :header => "API-VERSION", :parameter => "version", :path => ver, :value => ver}) do
match '/foos.(:format)' => 'foos#index', :via => :get
end
end
end

it "should route to the correct controller when header matches" do
@headers["HTTP_API_VERSION"] = ver
get "/foos.json", nil, @headers
assert_response 200
assert_equal 'application/json', response.content_type
assert_equal ver, response.body
end

it "should route to the correct controller when path matches" do
get "/#{ver}/foos.json", nil, @headers
assert_response 200
assert_equal 'application/json', response.content_type
assert_equal ver, response.body
end

it "should route to the correct controller when parameter matches" do
get "/foos.json?version=#{ver}", nil, @headers
assert_response 200
assert_equal 'application/json', response.content_type
assert_equal ver, response.body
end
end
end
end
end
Expand Down

0 comments on commit 87832c2

Please sign in to comment.