-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature-strategy_refactoring'
- Loading branch information
Showing
19 changed files
with
222 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
.idea | ||
.rvmrc | ||
.ruby-gemset | ||
.ruby-version | ||
spec/log | ||
log/ | ||
*.gem | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ DEPENDENCIES | |
activerecord | ||
coveralls | ||
i18n (= 0.6.1) | ||
mocha | ||
mocha (~> 0.12.0) | ||
multi_json | ||
rack | ||
rake | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module VersionCake | ||
class CustomStrategy < ExtractionStrategy | ||
def initialize(callback) | ||
@callback = callback | ||
end | ||
|
||
def execute(request) | ||
@callback.call(request) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'active_support/core_ext/class/attribute_accessors.rb' | ||
|
||
module VersionCake | ||
class ExtractionStrategy | ||
|
||
cattr_accessor :version_string | ||
@@version_string = 'api_version' | ||
|
||
def extract(request) | ||
version = execute(request) | ||
return version.to_i if version | ||
end | ||
|
||
def execute(request) | ||
raise Exception, "ExtractionStrategy requires execute to be implemented" | ||
end | ||
|
||
def self.lookup(strategy) | ||
if strategy.class == Proc | ||
if strategy.arity == 1 | ||
VersionCake::CustomStrategy.new(strategy) | ||
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 | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/versioncake/strategies/http_accept_parameter_strategy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module VersionCake | ||
class HttpAcceptParameterStrategy < ExtractionStrategy | ||
|
||
def execute(request) | ||
if request.headers.has_key?("HTTP_ACCEPT") && | ||
match = request.headers["HTTP_ACCEPT"].match(/#{@@version_string}=([0-9]+)/) | ||
match[1] | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module VersionCake | ||
class HttpHeaderStrategy < ExtractionStrategy | ||
|
||
def execute(request) | ||
if request.headers.has_key? "HTTP_X_#{@@version_string.upcase}" | ||
request.headers["HTTP_X_#{@@version_string.upcase}"] | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module VersionCake | ||
class QueryParameterStrategy < ExtractionStrategy | ||
|
||
def execute(request) | ||
if request.query_parameters.has_key? @@version_string.to_sym | ||
request.query_parameters[@@version_string.to_sym] | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module VersionCake | ||
class RequestParameterStrategy < ExtractionStrategy | ||
|
||
def execute(request) | ||
if request.request_parameters.has_key? @@version_string.to_sym | ||
request.request_parameters[@@version_string.to_sym].to_i | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require './test/test_helper' | ||
|
||
class ExtractionStrategyTest < ActiveSupport::TestCase | ||
test "execute is required for a strategy" do | ||
assert_raise(Exception) do | ||
VersionCake::ExtractionStrategy.new.execute("request") | ||
end | ||
end | ||
|
||
test "extract will convert the the result to integer" do | ||
class TestStrategy < VersionCake::ExtractionStrategy | ||
def execute(request); "123"; end | ||
end | ||
assert_equal TestStrategy.new.extract("request"), 123 | ||
end | ||
|
||
test "it can lookup a strategy" do | ||
strategy = VersionCake::ExtractionStrategy.lookup(:query_parameter) | ||
assert_equal VersionCake::QueryParameterStrategy, strategy.class | ||
end | ||
|
||
test "it creates a custom strategy for a proc" do | ||
strategy = VersionCake::ExtractionStrategy.lookup(lambda{|req|}) | ||
assert_equal VersionCake::CustomStrategy, strategy.class | ||
end | ||
|
||
test "it fails to create a custom strategy for a proc with no parameters" do | ||
assert_raise(Exception) do | ||
VersionCake::ExtractionStrategy.lookup(lambda{}) | ||
end | ||
end | ||
|
||
test "it raises error when no strategy is found" do | ||
assert_raise(Exception) do | ||
VersionCake::ExtractionStrategy.lookup(:fake_extraction) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require './test/test_helper' | ||
|
||
class HttpAcceptParameterStrategyTest < ActiveSupport::TestCase | ||
setup do | ||
@strategy = VersionCake::HttpAcceptParameterStrategy.new | ||
end | ||
|
||
test "a request with an HTTP_ACCEPT version retrieves the version" do | ||
request = stub(:headers => {'HTTP_ACCEPT' => 'application/xml; api_version=11'}) | ||
assert_equal 11, @strategy.extract(request) | ||
end | ||
|
||
test "a request without an HTTP_ACCEPT version returns nil" do | ||
request = stub(:headers => {'HTTP_ACCEPT' => 'text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c'}) | ||
assert_nil @strategy.extract(request) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require './test/test_helper' | ||
|
||
class HttpHeaderStrategyTest < ActiveSupport::TestCase | ||
setup do | ||
@strategy = VersionCake::HttpHeaderStrategy.new | ||
end | ||
|
||
test "a request with an HTTP_X_API_VERSION retrieves the version" do | ||
request = stub(:headers => {'HTTP_X_API_VERSION' => '11'}) | ||
assert_equal 11, @strategy.extract(request) | ||
end | ||
|
||
test "a request without an HTTP_X_API_VERSION returns nil" do | ||
request = stub(:headers => {'HTTP_ACCEPT' => 'text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c'}) | ||
assert_nil @strategy.extract(request) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require './test/test_helper' | ||
|
||
class QueryParameterStrategyTest < ActiveSupport::TestCase | ||
setup do | ||
@strategy = VersionCake::QueryParameterStrategy.new | ||
end | ||
|
||
test "a request with an api_version parameter retrieves the version" do | ||
request = stub(:query_parameters => {:api_version => '11', :other => 'parameter'}) | ||
assert_equal 11, @strategy.extract(request) | ||
end | ||
|
||
test "a request without an api_version parameter returns nil" do | ||
request = stub(:query_parameters => {:other => 'parameter', :another => 'parameter'}) | ||
assert_nil @strategy.extract(request) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require './test/test_helper' | ||
|
||
class RequestParameterStrategyTest < ActiveSupport::TestCase | ||
setup do | ||
@strategy = VersionCake::RequestParameterStrategy.new | ||
end | ||
|
||
test "a request with an api_version request parameter retrieves the version" do | ||
request = stub(:request_parameters => {:api_version => '11', :other => 'parameter'}) | ||
assert_equal 11, @strategy.extract(request) | ||
end | ||
|
||
test "a request without an api_version request parameter returns nil" do | ||
request = stub(:request_parameters => {:other => 'parameter', :another => 'parameter'}) | ||
assert_nil @strategy.extract(request) | ||
end | ||
end |