Skip to content

Commit

Permalink
resolving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bwillis committed Apr 15, 2014
2 parents 8631be3 + d344eb1 commit b033930
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 44 deletions.
38 changes: 25 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
## Unreleased Changes

[Full Changelog](https://github.com/bwillis/versioncake/compare/v2.1...master)
[Full Changelog](https://github.com/bwillis/versioncake/compare/v2.3.1...master)

Bug Fixes:

* None

Enhancements:

* None

Deprecations:

* Enable support for [Rails API](https://github.com/rails-api/rails-api) project! thanks [David Butler](https://github.com/dwbutler)

## 2.3.1 (Mar 6, 2014)

[Full Changelog](https://github.com/bwillis/versioncake/compare/v2.2...v2.3)

Enhancements:

* Allow an object instance to be used as a custom strategy

## 2.2.0 (Mar 5, 2014)

[Full Changelog](https://github.com/bwillis/versioncake/compare/v2.1...v2.2)

Bug Fixes:

* Be defensive with the return value of a custom strategy (#27)

## 2.1.0 (Mar 3, 2014)

[Full Changelog](https://github.com/bwillis/versioncake/compare/v2.0...v2.1)

Enhancements:

* Allow simpler handling of an unsupported versioned request by raising a custom error (issues #24 and #25) thanks [Richard Nuno](https://github.com/richardnuno) and [Andres Camacho](https://github.com/andresfcamacho)
Expand Down Expand Up @@ -42,20 +66,12 @@ Bug Fixes:

[Full Changelog](https://github.com/bwillis/versioncake/compare/v1.1...v1.2)

Bug Fixes:

* None

Enhancements:

* Support Rails 4
* Adding Rails version testing with [Appraisals](https://github.com/thoughtbot/appraisal)
* Added contribution guide

Deprecations:

* None

## 1.1.0 (May 18, 2013)

[Full Changelog](https://github.com/bwillis/versioncake/compare/v1.0...v1.1)
Expand All @@ -71,10 +87,6 @@ Enhancements:
* Internal refactoring of strategies for better testability.
* Isolated Rails hooks.

Deprecations:

* None

## 1.0.0 (March 14, 2013)

[Full Changelog](https://github.com/bwillis/versioncake/compare/v0.5...v1.0)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
versioncake (2.1.0)
versioncake (2.3.1)
actionpack (>= 3.2)
activesupport (>= 3.2)
railties (>= 3.2)
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Co-authored by Ben Willis ([bwillis](https://github.com/bwillis/)) and Jim Jones ([aantix](https://github.com/aantix)).

Version Cake is an unobtrusive way to version APIs in your Rails app.
Version Cake is an unobtrusive way to version APIs in your Rails app.

- Easily version any view with their API version:

Expand Down Expand Up @@ -39,7 +39,7 @@ gem install versioncake
| [1.1](CHANGELOG.md#110-may-18-2013) | Yes | No | No |
| [1.2](CHANGELOG.md#120-may-26-2013) | Yes | Yes | No |
| [1.3](CHANGELOG.md#130-sept-26-2013) | Yes | Yes | No |
| [2.0](CHANGELOG.md#200-feb-6-2014) | Yes | Yes | Yes |
| [>2.0](CHANGELOG.md#200-feb-6-2014) | Yes | Yes | Yes |

## Upgrade v1.* -> v2.0

Expand Down Expand Up @@ -74,7 +74,7 @@ config.versioncake.extraction_strategy = :query_parameter # for simplicity
```

Often times with APIs, depending upon the version, different logic needs to be applied. With the following controller code, the initial value of @posts includes all Post entries.
But if the requested API version is three or greater, we're going to eagerly load the associated comments as well.
But if the requested API version is three or greater, we're going to eagerly load the associated comments as well.

Being able to control the logic based on the api version allow you to ensure forwards and backwards compatibility for future changes.

Expand Down Expand Up @@ -119,7 +119,7 @@ end

When a version is specified for which a view doesn't exist, the request degrades and renders the next lowest version number to ensure the API's backwards compatibility. In the following case, since views/posts/index.json.v3.jbuilder doesn't exist, views/posts/index.json.v1.jbuilder is rendered instead.

#### http://localhost:3000/posts.json?api_version=3
#### http://localhost:3000/posts.json?api_version=3
```javascript
[
{
Expand All @@ -140,7 +140,7 @@ When a version is specified for which a view doesn't exist, the request degrades

For a given request, if we specify the version number, and that version of the view exists, that version specific view version will be rendered. In the below case, views/posts/index.json.v1.jbuilder is rendered.

#### http://localhost:3000/posts.json?api_version=2 or http://localhost:3000/posts.json?api_version=1
#### http://localhost:3000/posts.json?api_version=2 or http://localhost:3000/posts.json?api_version=1
```javascript
[
{
Expand Down Expand Up @@ -214,11 +214,11 @@ These are the available strategies:
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/`
: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 }
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

If you use the path_parameter strategy with resources routes, you will want to setup your routes.rb config file to capture the api version. You can do that in a few ways. If you have just a few api routes you might specify the path directly like this:
```
Expand Down Expand Up @@ -346,7 +346,7 @@ AppName::Application.config.versioncake.supported_version_numbers.each do |suppo
before do
@controller.stubs(:requested_version).returns(supported_version)
end

test "all versions render the correct template" do
get :index
assert_equal @response.body, "index.html.v1.erb"
Expand All @@ -370,6 +370,7 @@ Thanks to all those who have helped make Version Cake really sweet:
* [mbradshawabs](https://github.com/mbradshawabs)
* [Richard Nuno](https://github.com/richardnuno)
* [Andres Camacho](https://github.com/andresfcamacho)
* [Yukio Mizuta](https://github.com/untidy-hair)
* [David Butler](https://github.com/dwbutler)

# Related Material
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails3.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
versioncake (2.1.0)
versioncake (2.3.1)
actionpack (>= 3.2)
activesupport (>= 3.2)
railties (>= 3.2)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails4.0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
versioncake (2.1.0)
versioncake (2.3.1)
actionpack (>= 3.2)
activesupport (>= 3.2)
railties (>= 3.2)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails4.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../
specs:
versioncake (2.1.0)
versioncake (2.3.1)
actionpack (>= 3.2)
activesupport (>= 3.2)
railties (>= 3.2)
Expand Down
4 changes: 2 additions & 2 deletions lib/versioncake/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Cli

def migrate(path)
path = RAILS_VIEW_PATH unless path
raise ArgumentError.new("No directory exists for '#{path}'") unless File.exists? path
raise ArgumentError.new("No directory exists for '#{path}'") unless File.exist? path

files_to_rename = []
Dir.glob(File.join(path, '**/*.*')).each do |filename|
Expand Down Expand Up @@ -57,4 +57,4 @@ def has_version_in_name?(filename)
end

end
end
end
6 changes: 5 additions & 1 deletion lib/versioncake/strategies/custom_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ def initialize(callback)
end

def execute(request)
@callback.call(request)
if @callback.respond_to? :execute
@callback.execute(request)
else
@callback.call(request)
end
end
end
end
39 changes: 24 additions & 15 deletions lib/versioncake/strategies/extraction_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@ def execute(request)
end

def self.lookup(strategy)
if strategy.class == Proc
if strategy.arity == 1
VersionCake::CustomStrategy.new(strategy)
case strategy
when String, Symbol
strategy_name = "#{strategy}_strategy".camelize
begin
VersionCake.const_get(strategy_name).new
rescue
raise Exception, "Unknown VersionCake extraction strategy #{strategy_name}"
end
when Proc
if strategy.arity == 1
VersionCake::CustomStrategy.new(strategy)
else
raise Exception, "Custom proc extraction strategy requires a single parameter"
end
when Object
if !strategy.methods.include?(:execute)
raise Exception, "Custom extraction strategy requires an execute method"
elsif strategy.method(:execute).arity != 1
raise Exception, "Custom extraction strategy requires an execute method with a single parameter"
else
VersionCake::CustomStrategy.new(strategy)
end
else
raise Exception, "Custom extraction strategy requires a single parameter"
end
else
strategy_name = "#{strategy}_strategy".camelize
begin
VersionCake.const_get(strategy_name).new
rescue
raise Exception, "Unknown VersionCake extraction strategy #{strategy_name}"
end
raise Exception, "Invalid extration strategy"
end

end

end
end
end
2 changes: 1 addition & 1 deletion lib/versioncake/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module VersionCake
VERSION = "2.1.0"
VERSION = "2.3.1"
end
18 changes: 18 additions & 0 deletions test/unit/strategies/extraction_strategy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ def execute(request); Object.new; end
assert_equal VersionCake::CustomStrategy, strategy.class
end

test "it wraps a custom object" do
class FakeStrategy
def execute(request);end
end
strategy = VersionCake::ExtractionStrategy.lookup(FakeStrategy.new)
assert_equal VersionCake::CustomStrategy, strategy.class
end

test "it calls a custom objects execute method" do
class FakeStrategy
def execute(request)
9999
end
end
strategy = VersionCake::ExtractionStrategy.lookup(FakeStrategy.new)
assert_equal 9999, strategy.execute(nil)
end

test "it fails to create a custom strategy for a proc with no parameters" do
assert_raise(Exception) do
VersionCake::ExtractionStrategy.lookup(lambda{})
Expand Down

0 comments on commit b033930

Please sign in to comment.