versioncake 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +37 -0
- data/Gemfile +3 -5
- data/Gemfile.lock +18 -14
- data/README.md +31 -2
- data/lib/versioncake/configuration.rb +46 -0
- data/lib/versioncake/controller_additions.rb +24 -0
- data/lib/versioncake/railtie.rb +23 -0
- data/lib/versioncake/strategies/custom_strategy.rb +11 -0
- data/lib/versioncake/strategies/extraction_strategy.rb +37 -0
- data/lib/versioncake/strategies/http_accept_parameter_strategy.rb +12 -0
- data/lib/versioncake/strategies/http_header_strategy.rb +11 -0
- data/lib/versioncake/strategies/query_parameter_strategy.rb +11 -0
- data/lib/versioncake/strategies/request_parameter_strategy.rb +11 -0
- data/lib/versioncake/version.rb +1 -1
- data/lib/versioncake/versioned_request.rb +38 -0
- data/lib/versioncake/{action_view/resolver.rb → view_additions.rb} +15 -0
- data/lib/versioncake.rb +12 -6
- data/test/app/controllers/renders_controller.rb +4 -1
- data/test/config/application.rb +5 -7
- data/test/functional/renders_controller_test.rb +32 -11
- data/test/test_helper.rb +6 -2
- data/test/unit/configuration_test.rb +49 -0
- data/test/unit/strategies/extraction_strategy_test.rb +38 -0
- data/test/unit/strategies/http_accept_parameter_strategy_test.rb +17 -0
- data/test/unit/strategies/http_header_strategy_test.rb +17 -0
- data/test/unit/strategies/query_parameter_strategy_test.rb +17 -0
- data/test/unit/strategies/request_parameter_strategy_test.rb +17 -0
- data/test/unit/versioned_request_test.rb +48 -0
- metadata +36 -37
- data/lib/versioncake/action_controller/versioning.rb +0 -31
- data/lib/versioncake/action_view/lookup_context.rb +0 -10
- data/lib/versioncake/action_view/railtie.rb +0 -19
- data/lib/versioncake/action_view/template.rb +0 -11
- data/lib/versioncake/action_view/versions.rb +0 -90
- data/test/config/database.yml +0 -4
- data/test/db/test.sqlite3 +0 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: ad6b180d713a358c8dca71c1ee7d13763b33d61c
|
|
4
|
+
data.tar.gz: 6b3a64e7b6af7ebf8839d27c8f427c0dcdf1030d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c13e158b7af821d0099ac548bc20f723554b335383088b857e886b7483fea4cae52880087b9713745e7450fd160787323471e59169c953eb9d0ed6fc796a2248
|
|
7
|
+
data.tar.gz: 9d371dcb0558ec42eeb179141c908e7c9a4b6c2831c6836e0063412772b5cfb9dc397db6f79af65e41db6f2d981c8be6eafb94f90d87439b2fe173b20c881fca
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
## Unreleased Changes
|
|
2
|
+
|
|
3
|
+
[Full Changelog](https://github.com/bwillis/versioncake/compare/v1.1...master)
|
|
4
|
+
|
|
5
|
+
* No significant changes.
|
|
6
|
+
|
|
7
|
+
## 1.1.0 (May 18, 2013)
|
|
8
|
+
|
|
9
|
+
[Full Changelog](https://github.com/bwillis/versioncake/compare/v1.0...v1.1)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Bug Fixes:
|
|
13
|
+
|
|
14
|
+
* HttpAcceptParameterStrategy will now accept multiple digit versions
|
|
15
|
+
|
|
16
|
+
Enhancements:
|
|
17
|
+
|
|
18
|
+
* Added this CHANGELOG!
|
|
19
|
+
* New configuration parameter default_version to set the desired version when a client does not send a version instead of the default latest version. Thanks to [Billy Catherall](https://github.com/bcatherall) for implementing this.
|
|
20
|
+
* Internal refactoring of strategies for better testability.
|
|
21
|
+
* Isolated Rails hooks.
|
|
22
|
+
|
|
23
|
+
Deprecations:
|
|
24
|
+
|
|
25
|
+
* None
|
|
26
|
+
|
|
27
|
+
## 1.0.0 (March 14, 2013)
|
|
28
|
+
|
|
29
|
+
[Full Changelog](https://github.com/bwillis/versioncake/compare/v0.5...v1.0)
|
|
30
|
+
|
|
31
|
+
## 0.5.0 (December 6, 2012)
|
|
32
|
+
|
|
33
|
+
[Full Changelog](https://github.com/bwillis/versioncake/compare/v0.4...v0.5)
|
|
34
|
+
|
|
35
|
+
## 0.4.0 (October 29, 2012)
|
|
36
|
+
|
|
37
|
+
[Full Changelog](https://github.com/bwillis/versioncake/compare/0fe364999ec9e5fe27faf39f82d8f7f2d26f38be...v0.4)
|
data/Gemfile
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
source "
|
|
1
|
+
source "https://rubygems.org"
|
|
2
2
|
|
|
3
3
|
gemspec
|
|
4
4
|
|
|
5
5
|
gem 'rack'
|
|
6
|
-
gem 'actionpack'
|
|
7
|
-
gem 'activerecord'
|
|
8
6
|
gem 'multi_json'
|
|
9
|
-
gem 'i18n', '0.6.1'
|
|
10
7
|
gem 'tzinfo'
|
|
11
|
-
gem '
|
|
8
|
+
gem 'coveralls', require: false
|
|
12
9
|
|
|
13
10
|
group :development do
|
|
14
11
|
# for travis-ci.org
|
|
15
12
|
gem "rake"
|
|
16
13
|
gem 'test-unit'
|
|
14
|
+
gem 'mocha', '~> 0.12.0', :require => false
|
|
17
15
|
end
|
data/Gemfile.lock
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
versioncake (1.
|
|
4
|
+
versioncake (1.1.0)
|
|
5
5
|
actionpack (>= 3.0)
|
|
6
6
|
activesupport (>= 3.0)
|
|
7
7
|
railties (>= 3.0)
|
|
8
8
|
|
|
9
9
|
GEM
|
|
10
|
-
remote:
|
|
10
|
+
remote: https://rubygems.org/
|
|
11
11
|
specs:
|
|
12
12
|
actionpack (3.2.8)
|
|
13
13
|
activemodel (= 3.2.8)
|
|
@@ -22,22 +22,24 @@ GEM
|
|
|
22
22
|
activemodel (3.2.8)
|
|
23
23
|
activesupport (= 3.2.8)
|
|
24
24
|
builder (~> 3.0.0)
|
|
25
|
-
activerecord (3.2.8)
|
|
26
|
-
activemodel (= 3.2.8)
|
|
27
|
-
activesupport (= 3.2.8)
|
|
28
|
-
arel (~> 3.0.2)
|
|
29
|
-
tzinfo (~> 0.3.29)
|
|
30
25
|
activesupport (3.2.8)
|
|
31
26
|
i18n (~> 0.6)
|
|
32
27
|
multi_json (~> 1.0)
|
|
33
|
-
arel (3.0.2)
|
|
34
28
|
builder (3.0.0)
|
|
29
|
+
colorize (0.5.8)
|
|
30
|
+
coveralls (0.6.3)
|
|
31
|
+
colorize
|
|
32
|
+
multi_json (~> 1.3)
|
|
33
|
+
rest-client
|
|
34
|
+
simplecov (>= 0.7)
|
|
35
|
+
thor
|
|
35
36
|
erubis (2.7.0)
|
|
36
37
|
hike (1.2.1)
|
|
37
38
|
i18n (0.6.1)
|
|
38
39
|
journey (1.0.4)
|
|
39
40
|
json (1.7.5)
|
|
40
41
|
metaclass (0.0.1)
|
|
42
|
+
mime-types (1.21)
|
|
41
43
|
mocha (0.12.3)
|
|
42
44
|
metaclass (~> 0.0.1)
|
|
43
45
|
multi_json (1.3.6)
|
|
@@ -58,11 +60,16 @@ GEM
|
|
|
58
60
|
rake (0.9.2.2)
|
|
59
61
|
rdoc (3.12)
|
|
60
62
|
json (~> 1.4)
|
|
63
|
+
rest-client (1.6.7)
|
|
64
|
+
mime-types (>= 1.16)
|
|
65
|
+
simplecov (0.7.1)
|
|
66
|
+
multi_json (~> 1.0)
|
|
67
|
+
simplecov-html (~> 0.7.1)
|
|
68
|
+
simplecov-html (0.7.1)
|
|
61
69
|
sprockets (2.1.3)
|
|
62
70
|
hike (~> 1.2)
|
|
63
71
|
rack (~> 1.0)
|
|
64
72
|
tilt (~> 1.1, != 1.3.0)
|
|
65
|
-
sqlite3 (1.3.6)
|
|
66
73
|
test-unit (2.5.2)
|
|
67
74
|
thor (0.16.0)
|
|
68
75
|
tilt (1.3.3)
|
|
@@ -72,14 +79,11 @@ PLATFORMS
|
|
|
72
79
|
ruby
|
|
73
80
|
|
|
74
81
|
DEPENDENCIES
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
i18n (= 0.6.1)
|
|
78
|
-
mocha
|
|
82
|
+
coveralls
|
|
83
|
+
mocha (~> 0.12.0)
|
|
79
84
|
multi_json
|
|
80
85
|
rack
|
|
81
86
|
rake
|
|
82
|
-
sqlite3
|
|
83
87
|
test-unit
|
|
84
88
|
tzinfo
|
|
85
89
|
versioncake!
|
data/README.md
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[](http://travis-ci.org/bwillis/versioncake)
|
|
4
4
|
[](https://codeclimate.com/github/bwillis/versioncake)
|
|
5
|
+
[](https://coveralls.io/r/bwillis/versioncake)
|
|
6
|
+
[](https://gemnasium.com/bwillis/versioncake)
|
|
7
|
+
[](http://badge.fury.io/rb/versioncake)
|
|
5
8
|
|
|
6
9
|
Co-authored by Ben Willis ([@bwillis](https://github.com/bwillis/)) and Jim Jones ([@aantix](https://github.com/aantix)).
|
|
7
10
|
|
|
@@ -159,12 +162,17 @@ When no version is specified, the latest version of the view is rendered. In th
|
|
|
159
162
|
## How to use
|
|
160
163
|
|
|
161
164
|
### Configuration
|
|
165
|
+
The configuration should be placed in your Rails projects `config/application.rb`. It is also suggested to enable different settings per environment, for example development and test can have extraction strategies that make it easier to develop or write test code.
|
|
162
166
|
|
|
167
|
+
#### Supported Versions
|
|
163
168
|
You need to define the supported versions in your Rails application.rb file as `view_versions`. Use this config to set the range of supported API versions that can be served:
|
|
164
169
|
|
|
165
170
|
```ruby
|
|
166
|
-
config.view_versions = [1,3,4,5] # or (1
|
|
171
|
+
config.view_versions = [1,2,3,4,5] # or (1..5)
|
|
167
172
|
```
|
|
173
|
+
|
|
174
|
+
#### Extraction Strategy
|
|
175
|
+
|
|
168
176
|
You can also define the way to extract the version. The `view_version_extraction_strategy` allows you to set one of the default strategies or provide a proc to set your own. You can also pass it a prioritized array of the strategies.
|
|
169
177
|
```ruby
|
|
170
178
|
config.view_version_extraction_strategy = :query_parameter # [:http_header, :http_accept_parameter]
|
|
@@ -176,7 +184,17 @@ These are the available strategies:
|
|
|
176
184
|
- **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)
|
|
177
185
|
- **custom**: `lambda {|request| request.headers["HTTP_X_MY_VERSION"].to_i }` takes the request object and must return an integer
|
|
178
186
|
|
|
179
|
-
|
|
187
|
+
|
|
188
|
+
#### Default Version
|
|
189
|
+
|
|
190
|
+
When no version is supplied by a client, the version rendered will be the latest version by default. If you want to override this to another version, set the following property:
|
|
191
|
+
```ruby
|
|
192
|
+
config.default_version = 4
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Version String
|
|
196
|
+
|
|
197
|
+
The extraction strategies use a default string key of `api_version`, but that can be changed:
|
|
180
198
|
```ruby
|
|
181
199
|
config.view_version_string = "special_version_parameter_name"
|
|
182
200
|
```
|
|
@@ -264,6 +282,16 @@ AppName::Application.config.view_versions.each do |supported_version|
|
|
|
264
282
|
end
|
|
265
283
|
```
|
|
266
284
|
|
|
285
|
+
# Thanks!
|
|
286
|
+
|
|
287
|
+
Thanks to all those who have helped make Version Cake really sweet:
|
|
288
|
+
|
|
289
|
+
* [Manilla](https://github.com/manilla)
|
|
290
|
+
* [Alicia](https://github.com/alicial)
|
|
291
|
+
* [Rohit](https://github.com/rg)
|
|
292
|
+
* Sevag
|
|
293
|
+
* [Billy](https://github.com/bcatherall)
|
|
294
|
+
|
|
267
295
|
# Related Material
|
|
268
296
|
|
|
269
297
|
## Usages
|
|
@@ -285,6 +313,7 @@ end
|
|
|
285
313
|
- [Ruby5 on versioncake](http://ruby5.envylabs.com/episodes/310-episode-306-september-18th-2012/stories/2704-version-cake)
|
|
286
314
|
- [Rails core discussion](https://groups.google.com/forum/#!msg/rubyonrails-core/odwmEYYIum0/9POep66BvoMJ)
|
|
287
315
|
- [RubyWeekly](http://rubyweekly.com/archive/119.html)
|
|
316
|
+
- [API building tools on Ruby Toolbox](https://www.ruby-toolbox.com/categories/API_Builders)
|
|
288
317
|
|
|
289
318
|
# Questions?
|
|
290
319
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors.rb'
|
|
2
|
+
|
|
3
|
+
module VersionCake
|
|
4
|
+
module Configuration
|
|
5
|
+
|
|
6
|
+
SUPPORTED_VERSIONS_DEFAULT = (1..10)
|
|
7
|
+
|
|
8
|
+
mattr_accessor :supported_version_numbers
|
|
9
|
+
self.supported_version_numbers = SUPPORTED_VERSIONS_DEFAULT
|
|
10
|
+
|
|
11
|
+
mattr_accessor :extraction_strategies
|
|
12
|
+
self.extraction_strategies = [VersionCake::QueryParameterStrategy.new]
|
|
13
|
+
|
|
14
|
+
mattr_accessor :default_version
|
|
15
|
+
self.default_version = nil
|
|
16
|
+
|
|
17
|
+
def self.extraction_strategy=(val)
|
|
18
|
+
@@extraction_strategies.clear
|
|
19
|
+
Array.wrap(val).each do |configured_strategy|
|
|
20
|
+
@@extraction_strategies << VersionCake::ExtractionStrategy.lookup(configured_strategy)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.supported_version_numbers=(val)
|
|
25
|
+
@@supported_version_numbers = val.respond_to?(:to_a) ? val.to_a : Array.wrap(val)
|
|
26
|
+
@@supported_version_numbers.sort!.reverse!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.supported_versions(requested_version_number=nil)
|
|
30
|
+
supported_version_numbers.collect do |supported_version_number|
|
|
31
|
+
if requested_version_number.nil? || supported_version_number <= requested_version_number
|
|
32
|
+
:"v#{supported_version_number}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.supports_version?(version)
|
|
38
|
+
supported_version_numbers.include? version
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.latest_version
|
|
42
|
+
supported_version_numbers.first
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'action_controller'
|
|
2
|
+
|
|
3
|
+
module VersionCake
|
|
4
|
+
module ControllerAdditions
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
attr_accessor :requested_version, :is_latest_version, :derived_version
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
prepend_before_filter :set_version
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
protected
|
|
14
|
+
def set_version(override_version=nil)
|
|
15
|
+
versioned_request = VersionCake::VersionedRequest.new(request, override_version)
|
|
16
|
+
@requested_version = versioned_request.extracted_version
|
|
17
|
+
@derived_version = versioned_request.version
|
|
18
|
+
@_lookup_context.versions = versioned_request.supported_versions
|
|
19
|
+
@is_latest_version = versioned_request.is_latest_version
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
ActionController::Base.send(:include, VersionCake::ControllerAdditions)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'rails'
|
|
2
|
+
|
|
3
|
+
class ActionViewVersions < Rails::Railtie
|
|
4
|
+
initializer "view_versions" do |app|
|
|
5
|
+
ActiveSupport.on_load(:action_view) do
|
|
6
|
+
if app.config.respond_to?(:view_versions)
|
|
7
|
+
VersionCake::Configuration.supported_version_numbers = app.config.view_versions
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if app.config.respond_to?(:view_version_extraction_strategy)
|
|
11
|
+
VersionCake::Configuration.extraction_strategy = app.config.view_version_extraction_strategy
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
if app.config.respond_to?(:view_version_string)
|
|
15
|
+
VersionCake::ExtractionStrategy.version_string = app.config.view_version_string
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
if app.config.respond_to?(:default_version)
|
|
19
|
+
VersionCake::Configuration.default_version = app.config.default_version
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'active_support/core_ext/class/attribute_accessors.rb'
|
|
2
|
+
|
|
3
|
+
module VersionCake
|
|
4
|
+
class ExtractionStrategy
|
|
5
|
+
|
|
6
|
+
cattr_accessor :version_string
|
|
7
|
+
@@version_string = 'api_version'
|
|
8
|
+
|
|
9
|
+
def extract(request)
|
|
10
|
+
version = execute(request)
|
|
11
|
+
return version.to_i if version
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def execute(request)
|
|
15
|
+
raise Exception, "ExtractionStrategy requires execute to be implemented"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.lookup(strategy)
|
|
19
|
+
if strategy.class == Proc
|
|
20
|
+
if strategy.arity == 1
|
|
21
|
+
VersionCake::CustomStrategy.new(strategy)
|
|
22
|
+
else
|
|
23
|
+
raise Exception, "Custom extraction strategy requires a single parameter"
|
|
24
|
+
end
|
|
25
|
+
else
|
|
26
|
+
strategy_name = "#{strategy}_strategy".camelize
|
|
27
|
+
begin
|
|
28
|
+
VersionCake.const_get(strategy_name).new
|
|
29
|
+
rescue
|
|
30
|
+
raise Exception, "Unknown VersionCake extraction strategy #{strategy_name}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module VersionCake
|
|
2
|
+
class HttpAcceptParameterStrategy < ExtractionStrategy
|
|
3
|
+
|
|
4
|
+
def execute(request)
|
|
5
|
+
if request.headers.has_key?("HTTP_ACCEPT") &&
|
|
6
|
+
match = request.headers["HTTP_ACCEPT"].match(/#{@@version_string}=([0-9]+)/)
|
|
7
|
+
match[1]
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
end
|
data/lib/versioncake/version.rb
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module VersionCake
|
|
2
|
+
class VersionedRequest
|
|
3
|
+
attr_reader :version, :extracted_version, :is_latest_version
|
|
4
|
+
|
|
5
|
+
def initialize(request, version_override=nil)
|
|
6
|
+
@version = version_override || extract_version(request)
|
|
7
|
+
@is_latest_version = @version == VersionCake::Configuration.latest_version
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def supported_versions
|
|
11
|
+
VersionCake::Configuration.supported_versions(@version)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def apply_strategies(request)
|
|
17
|
+
version = nil
|
|
18
|
+
VersionCake::Configuration.extraction_strategies.each do |strategy|
|
|
19
|
+
version = strategy.extract(request)
|
|
20
|
+
break unless version.nil?
|
|
21
|
+
end
|
|
22
|
+
version
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def extract_version(request)
|
|
26
|
+
@extracted_version = apply_strategies(request)
|
|
27
|
+
if @extracted_version.nil?
|
|
28
|
+
@version = VersionCake::Configuration.default_version || VersionCake::Configuration.latest_version
|
|
29
|
+
elsif VersionCake::Configuration.supports_version? @extracted_version
|
|
30
|
+
@version = @extracted_version
|
|
31
|
+
elsif @extracted_version > VersionCake::Configuration.latest_version
|
|
32
|
+
raise ActionController::RoutingError.new("No route match for version")
|
|
33
|
+
else
|
|
34
|
+
raise ActionController::RoutingError.new("Version is deprecated")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
require 'action_view'
|
|
2
2
|
|
|
3
|
+
# register an addition detail for the lookup context to understand,
|
|
4
|
+
# this will allow us to have the versions available upon lookup in
|
|
5
|
+
# the resolver.
|
|
6
|
+
ActionView::LookupContext.register_detail(:versions){ VersionCake::Configuration.supported_versions }
|
|
7
|
+
|
|
3
8
|
ActionView::PathResolver.class_eval do
|
|
4
9
|
|
|
5
10
|
# not sure why we are doing this yet, but looks like a good idea
|
|
@@ -10,4 +15,14 @@ ActionView::PathResolver.class_eval do
|
|
|
10
15
|
# it to the default pattern
|
|
11
16
|
ActionView::PathResolver::DEFAULT_PATTERN.replace ":prefix/:action{.:locale,}{.:versions,}{.:formats,}{.:handlers,}"
|
|
12
17
|
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
ActionView::Template.class_eval do
|
|
21
|
+
|
|
22
|
+
# the identifier method name filters out numbers,
|
|
23
|
+
# but we want to preserve them for v1 etc.
|
|
24
|
+
def identifier_method_name #:nodoc:
|
|
25
|
+
inspect.gsub(/[^a-z0-9_]/, '_')
|
|
26
|
+
end
|
|
27
|
+
|
|
13
28
|
end
|
data/lib/versioncake.rb
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
require 'versioncake/
|
|
2
|
-
require 'versioncake/
|
|
3
|
-
require 'versioncake/
|
|
4
|
-
require 'versioncake/
|
|
5
|
-
require 'versioncake/
|
|
6
|
-
require 'versioncake/
|
|
1
|
+
require 'versioncake/strategies/extraction_strategy'
|
|
2
|
+
require 'versioncake/strategies/http_accept_parameter_strategy'
|
|
3
|
+
require 'versioncake/strategies/http_header_strategy'
|
|
4
|
+
require 'versioncake/strategies/query_parameter_strategy'
|
|
5
|
+
require 'versioncake/strategies/request_parameter_strategy'
|
|
6
|
+
require 'versioncake/strategies/custom_strategy'
|
|
7
|
+
|
|
8
|
+
require 'versioncake/configuration'
|
|
9
|
+
require 'versioncake/controller_additions'
|
|
10
|
+
require 'versioncake/view_additions'
|
|
11
|
+
require 'versioncake/versioned_request'
|
|
12
|
+
require 'versioncake/railtie'
|
data/test/config/application.rb
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "action_controller/railtie"
|
|
2
|
+
require "active_support/railtie"
|
|
3
|
+
|
|
4
|
+
# Auto-require default libraries and those for the current Rails environment.
|
|
5
|
+
Bundler.require :default, Rails.env
|
|
2
6
|
|
|
3
7
|
module RendersTest
|
|
4
8
|
class Application < Rails::Application
|
|
5
|
-
config.encoding = "utf-8"
|
|
6
|
-
config.filter_parameters += [:password]
|
|
7
|
-
config.active_support.escape_html_entities_in_json = true
|
|
8
|
-
config.active_record.whitelist_attributes = true
|
|
9
|
-
config.assets.enabled = true
|
|
10
|
-
config.assets.version = '1.0'
|
|
11
9
|
config.active_support.deprecation = :stderr
|
|
12
10
|
config.view_versions = (1..3)
|
|
13
11
|
config.view_version_extraction_strategy = :http_header
|
|
@@ -6,11 +6,11 @@ class RendersControllerTest < ActionController::TestCase
|
|
|
6
6
|
|
|
7
7
|
setup do
|
|
8
8
|
# change the version string for configuration testing
|
|
9
|
-
|
|
9
|
+
VersionCake::ExtractionStrategy.version_string = "version"
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
teardown do
|
|
13
|
-
|
|
13
|
+
VersionCake::ExtractionStrategy.version_string = "api_version"
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
test "render latest version of partial" do
|
|
@@ -33,22 +33,34 @@ class RendersControllerTest < ActionController::TestCase
|
|
|
33
33
|
assert !@controller.is_latest_version
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
test "exposes the derived version when the version is not set" do
|
|
36
|
+
test "exposes the derived version when the version is not set and no default" do
|
|
37
37
|
get :index
|
|
38
38
|
assert_equal 3, @controller.derived_version
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
test "exposes the default version when the version is not set default is set" do
|
|
42
|
+
VersionCake::Configuration.default_version = 1
|
|
43
|
+
get :index
|
|
44
|
+
assert_equal 1, @controller.derived_version
|
|
45
|
+
VersionCake::Configuration.default_version = nil
|
|
46
|
+
end
|
|
47
|
+
|
|
41
48
|
test "requested version is blank when the version is not set" do
|
|
42
49
|
get :index
|
|
43
50
|
assert_blank @controller.requested_version
|
|
44
51
|
end
|
|
52
|
+
|
|
53
|
+
test "set_version can be called to override the requested version" do
|
|
54
|
+
get :index, "version" => "1", "override_version" => 2
|
|
55
|
+
assert_equal 2, @controller.derived_version
|
|
56
|
+
end
|
|
45
57
|
end
|
|
46
58
|
|
|
47
59
|
class ParameterStragegyTest < ActionController::TestCase
|
|
48
60
|
tests RendersController
|
|
49
61
|
|
|
50
62
|
setup do
|
|
51
|
-
|
|
63
|
+
VersionCake::Configuration.extraction_strategy = :query_parameter
|
|
52
64
|
end
|
|
53
65
|
|
|
54
66
|
test "render version 1 of the partial based on the parameter _api_version" do
|
|
@@ -71,7 +83,7 @@ class CustomHeaderStrategyTest < ActionController::TestCase
|
|
|
71
83
|
tests RendersController
|
|
72
84
|
|
|
73
85
|
setup do
|
|
74
|
-
|
|
86
|
+
VersionCake::Configuration.extraction_strategy = :http_header
|
|
75
87
|
end
|
|
76
88
|
|
|
77
89
|
test "renders version 1 of the partial based on the header API-Version" do
|
|
@@ -97,7 +109,7 @@ class RequestBodyStrategyTest < ActionController::TestCase
|
|
|
97
109
|
tests RendersController
|
|
98
110
|
|
|
99
111
|
setup do
|
|
100
|
-
|
|
112
|
+
VersionCake::Configuration.extraction_strategy = :request_parameter
|
|
101
113
|
end
|
|
102
114
|
|
|
103
115
|
test "requested version is in the body" do
|
|
@@ -110,7 +122,7 @@ class AcceptHeaderStrategyTest < ActionController::TestCase
|
|
|
110
122
|
tests RendersController
|
|
111
123
|
|
|
112
124
|
setup do
|
|
113
|
-
|
|
125
|
+
VersionCake::Configuration.extraction_strategy = :http_accept_parameter
|
|
114
126
|
end
|
|
115
127
|
|
|
116
128
|
test "render version 1 of the partial based on the header Accept" do
|
|
@@ -136,13 +148,22 @@ class AcceptHeaderStrategyTest < ActionController::TestCase
|
|
|
136
148
|
get :index
|
|
137
149
|
assert_equal @response.body, "index.v2.html.erb"
|
|
138
150
|
end
|
|
151
|
+
|
|
152
|
+
test "render the default version version of the partial" do
|
|
153
|
+
VersionCake::Configuration.default_version = 1
|
|
154
|
+
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=abc"})
|
|
155
|
+
get :index
|
|
156
|
+
assert_equal @response.body, "index.v1.html.erb"
|
|
157
|
+
VersionCake::Configuration.default_version = nil
|
|
158
|
+
end
|
|
159
|
+
|
|
139
160
|
end
|
|
140
161
|
|
|
141
162
|
class CustomStrategyTest < ActionController::TestCase
|
|
142
163
|
tests RendersController
|
|
143
164
|
|
|
144
165
|
setup do
|
|
145
|
-
|
|
166
|
+
VersionCake::Configuration.extraction_strategy = lambda { |request| 2 }
|
|
146
167
|
end
|
|
147
168
|
|
|
148
169
|
test "renders version 2 of the partial based on the header Accept" do
|
|
@@ -155,7 +176,7 @@ class MultipleStrategyTest < ActionController::TestCase
|
|
|
155
176
|
tests RendersController
|
|
156
177
|
|
|
157
178
|
setup do
|
|
158
|
-
|
|
179
|
+
VersionCake::Configuration.extraction_strategy = [:http_accept_parameter, :query_parameter]
|
|
159
180
|
end
|
|
160
181
|
|
|
161
182
|
test "renders version 1 of the partial based on the header Accept" do
|
|
@@ -180,7 +201,7 @@ class UnsupportedVersionTest < ActionController::TestCase
|
|
|
180
201
|
tests RendersController
|
|
181
202
|
|
|
182
203
|
setup do
|
|
183
|
-
|
|
204
|
+
VersionCake::Configuration.extraction_strategy = :query_parameter
|
|
184
205
|
end
|
|
185
206
|
|
|
186
207
|
test "responds with 404 when the version is larger than the supported version" do
|
|
@@ -190,7 +211,7 @@ class UnsupportedVersionTest < ActionController::TestCase
|
|
|
190
211
|
end
|
|
191
212
|
|
|
192
213
|
test "responds with 404 when the version is lower than the latest version, but not an available version" do
|
|
193
|
-
|
|
214
|
+
VersionCake::Configuration.supported_version_numbers = [2,3]
|
|
194
215
|
assert_raise ActionController::RoutingError do
|
|
195
216
|
get :index, "api_version" => "1"
|
|
196
217
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
require 'coveralls'
|
|
2
|
+
Coveralls.wear!
|
|
3
|
+
|
|
1
4
|
require 'bundler'
|
|
2
5
|
Bundler.require
|
|
3
6
|
|
|
@@ -5,13 +8,14 @@ require 'versioncake'
|
|
|
5
8
|
|
|
6
9
|
ENV["RAILS_ENV"] = 'test'
|
|
7
10
|
|
|
8
|
-
require 'rails/all'
|
|
9
11
|
require 'rails/test_help'
|
|
10
12
|
require 'test/unit'
|
|
11
13
|
|
|
14
|
+
require "mocha"
|
|
15
|
+
|
|
12
16
|
require File.expand_path('../config/application', __FILE__)
|
|
13
17
|
|
|
14
18
|
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
|
|
15
19
|
FIXTURES = Pathname.new(FIXTURE_LOAD_PATH)
|
|
16
20
|
|
|
17
|
-
RendersTest::Application.initialize!
|
|
21
|
+
RendersTest::Application.initialize!
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class ConfigurationTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@previous_versions = VersionCake::Configuration.supported_version_numbers
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
teardown do
|
|
9
|
+
VersionCake::Configuration.supported_version_numbers = @previous_versions
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
test "supported_version_numbers can be set with range" do
|
|
13
|
+
VersionCake::Configuration.supported_version_numbers = (1..7)
|
|
14
|
+
assert_equal [7,6,5,4,3,2,1], VersionCake::Configuration.supported_version_numbers
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test "supported_version_numbers can be set with an unordered array" do
|
|
18
|
+
VersionCake::Configuration.supported_version_numbers = [2,4,1,5,3,6,7]
|
|
19
|
+
assert_equal [7,6,5,4,3,2,1], VersionCake::Configuration.supported_version_numbers
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test "supported_version_numbers can be set with a single value" do
|
|
23
|
+
VersionCake::Configuration.supported_version_numbers = 19
|
|
24
|
+
assert_equal [19], VersionCake::Configuration.supported_version_numbers
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test "supports_version? is only true for given supported versions" do
|
|
28
|
+
VersionCake::Configuration.supported_version_numbers = (1..7)
|
|
29
|
+
VersionCake::Configuration.supported_version_numbers.each do |supported_version|
|
|
30
|
+
assert_true VersionCake::Configuration.supports_version? supported_version
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test "supports_version? is not true for other versions" do
|
|
35
|
+
VersionCake::Configuration.supported_version_numbers = (1..7)
|
|
36
|
+
[-2,-1,0,8,9,10].each do |unsupported_version|
|
|
37
|
+
assert_false VersionCake::Configuration.supports_version? unsupported_version
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
test "latest_version retrieves the highest supported version" do
|
|
42
|
+
VersionCake::Configuration.supported_version_numbers = [4,1,3,9,2,54]
|
|
43
|
+
assert_equal 54, VersionCake::Configuration.latest_version
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
test "default supported_version_numbers should be a logic set of version numbers" do
|
|
47
|
+
assert_equal VersionCake::Configuration::SUPPORTED_VERSIONS_DEFAULT, (1..10)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class ExtractionStrategyTest < ActiveSupport::TestCase
|
|
4
|
+
test "execute is required for a strategy" do
|
|
5
|
+
assert_raise(Exception) do
|
|
6
|
+
VersionCake::ExtractionStrategy.new.execute("request")
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
test "extract will convert the the result to integer" do
|
|
11
|
+
class TestStrategy < VersionCake::ExtractionStrategy
|
|
12
|
+
def execute(request); "123"; end
|
|
13
|
+
end
|
|
14
|
+
assert_equal TestStrategy.new.extract("request"), 123
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test "it can lookup a strategy" do
|
|
18
|
+
strategy = VersionCake::ExtractionStrategy.lookup(:query_parameter)
|
|
19
|
+
assert_equal VersionCake::QueryParameterStrategy, strategy.class
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test "it creates a custom strategy for a proc" do
|
|
23
|
+
strategy = VersionCake::ExtractionStrategy.lookup(lambda{|req|})
|
|
24
|
+
assert_equal VersionCake::CustomStrategy, strategy.class
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test "it fails to create a custom strategy for a proc with no parameters" do
|
|
28
|
+
assert_raise(Exception) do
|
|
29
|
+
VersionCake::ExtractionStrategy.lookup(lambda{})
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test "it raises error when no strategy is found" do
|
|
34
|
+
assert_raise(Exception) do
|
|
35
|
+
VersionCake::ExtractionStrategy.lookup(:fake_extraction)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class HttpAcceptParameterStrategyTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@strategy = VersionCake::HttpAcceptParameterStrategy.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test "a request with an HTTP_ACCEPT version retrieves the version" do
|
|
9
|
+
request = stub(:headers => {'HTTP_ACCEPT' => 'application/xml; api_version=11'})
|
|
10
|
+
assert_equal 11, @strategy.extract(request)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test "a request without an HTTP_ACCEPT version returns nil" do
|
|
14
|
+
request = stub(:headers => {'HTTP_ACCEPT' => 'text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c'})
|
|
15
|
+
assert_nil @strategy.extract(request)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class HttpHeaderStrategyTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@strategy = VersionCake::HttpHeaderStrategy.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test "a request with an HTTP_X_API_VERSION retrieves the version" do
|
|
9
|
+
request = stub(:headers => {'HTTP_X_API_VERSION' => '11'})
|
|
10
|
+
assert_equal 11, @strategy.extract(request)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test "a request without an HTTP_X_API_VERSION returns nil" do
|
|
14
|
+
request = stub(:headers => {'HTTP_ACCEPT' => 'text/x-dvi; q=.8; mxb=100000; mxt=5.0, text/x-c'})
|
|
15
|
+
assert_nil @strategy.extract(request)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class QueryParameterStrategyTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@strategy = VersionCake::QueryParameterStrategy.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test "a request with an api_version parameter retrieves the version" do
|
|
9
|
+
request = stub(:query_parameters => {:api_version => '11', :other => 'parameter'})
|
|
10
|
+
assert_equal 11, @strategy.extract(request)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test "a request without an api_version parameter returns nil" do
|
|
14
|
+
request = stub(:query_parameters => {:other => 'parameter', :another => 'parameter'})
|
|
15
|
+
assert_nil @strategy.extract(request)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class RequestParameterStrategyTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@strategy = VersionCake::RequestParameterStrategy.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test "a request with an api_version request parameter retrieves the version" do
|
|
9
|
+
request = stub(:request_parameters => {:api_version => '11', :other => 'parameter'})
|
|
10
|
+
assert_equal 11, @strategy.extract(request)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test "a request without an api_version request parameter returns nil" do
|
|
14
|
+
request = stub(:request_parameters => {:other => 'parameter', :another => 'parameter'})
|
|
15
|
+
assert_nil @strategy.extract(request)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require './test/test_helper'
|
|
2
|
+
|
|
3
|
+
class VersionedRequestTest < ActiveSupport::TestCase
|
|
4
|
+
test "a request with a supported version returns the version" do
|
|
5
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => 2)
|
|
6
|
+
versioned_request = VersionCake::VersionedRequest.new(stub())
|
|
7
|
+
assert_equal 2, versioned_request.version
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
test "a request without a version returns the latest version" do
|
|
11
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => nil)
|
|
12
|
+
versioned_request = VersionCake::VersionedRequest.new(stub())
|
|
13
|
+
assert_equal 3, versioned_request.version
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
test "a request for a version that is higher than the latest version raises an error" do
|
|
17
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => 99)
|
|
18
|
+
assert_raise ActionController::RoutingError do
|
|
19
|
+
VersionCake::VersionedRequest.new(stub())
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "a request for a deprecated version raises an exception" do
|
|
24
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => 2)
|
|
25
|
+
VersionCake::Configuration.stubs(:supports_version? => false)
|
|
26
|
+
assert_raise ActionController::RoutingError do
|
|
27
|
+
VersionCake::VersionedRequest.new(stub())
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test "has a method to determine if requesting the latest version" do
|
|
32
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => nil)
|
|
33
|
+
versioned_request = VersionCake::VersionedRequest.new(stub())
|
|
34
|
+
assert_true versioned_request.is_latest_version
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test "has a method to retrieve the extracted version" do
|
|
38
|
+
VersionCake::VersionedRequest.any_instance.stubs(:apply_strategies => nil)
|
|
39
|
+
versioned_request = VersionCake::VersionedRequest.new(stub())
|
|
40
|
+
assert_nil versioned_request.extracted_version
|
|
41
|
+
assert_equal 3, versioned_request.version
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test "the version can be overriden by a parameter" do
|
|
45
|
+
versioned_request = VersionCake::VersionedRequest.new(stub(), 8)
|
|
46
|
+
assert_equal 8, versioned_request.version
|
|
47
|
+
end
|
|
48
|
+
end
|
metadata
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: versioncake
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 1.1.0
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- Jim Jones
|
|
@@ -10,86 +9,76 @@ authors:
|
|
|
10
9
|
autorequire:
|
|
11
10
|
bindir: bin
|
|
12
11
|
cert_chain: []
|
|
13
|
-
date: 2013-
|
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
|
14
13
|
dependencies:
|
|
15
14
|
- !ruby/object:Gem::Dependency
|
|
16
15
|
name: actionpack
|
|
17
16
|
requirement: !ruby/object:Gem::Requirement
|
|
18
|
-
none: false
|
|
19
17
|
requirements:
|
|
20
|
-
- -
|
|
18
|
+
- - '>='
|
|
21
19
|
- !ruby/object:Gem::Version
|
|
22
20
|
version: '3.0'
|
|
23
21
|
type: :runtime
|
|
24
22
|
prerelease: false
|
|
25
23
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
-
none: false
|
|
27
24
|
requirements:
|
|
28
|
-
- -
|
|
25
|
+
- - '>='
|
|
29
26
|
- !ruby/object:Gem::Version
|
|
30
27
|
version: '3.0'
|
|
31
28
|
- !ruby/object:Gem::Dependency
|
|
32
29
|
name: activesupport
|
|
33
30
|
requirement: !ruby/object:Gem::Requirement
|
|
34
|
-
none: false
|
|
35
31
|
requirements:
|
|
36
|
-
- -
|
|
32
|
+
- - '>='
|
|
37
33
|
- !ruby/object:Gem::Version
|
|
38
34
|
version: '3.0'
|
|
39
35
|
type: :runtime
|
|
40
36
|
prerelease: false
|
|
41
37
|
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
-
none: false
|
|
43
38
|
requirements:
|
|
44
|
-
- -
|
|
39
|
+
- - '>='
|
|
45
40
|
- !ruby/object:Gem::Version
|
|
46
41
|
version: '3.0'
|
|
47
42
|
- !ruby/object:Gem::Dependency
|
|
48
43
|
name: railties
|
|
49
44
|
requirement: !ruby/object:Gem::Requirement
|
|
50
|
-
none: false
|
|
51
45
|
requirements:
|
|
52
|
-
- -
|
|
46
|
+
- - '>='
|
|
53
47
|
- !ruby/object:Gem::Version
|
|
54
48
|
version: '3.0'
|
|
55
49
|
type: :runtime
|
|
56
50
|
prerelease: false
|
|
57
51
|
version_requirements: !ruby/object:Gem::Requirement
|
|
58
|
-
none: false
|
|
59
52
|
requirements:
|
|
60
|
-
- -
|
|
53
|
+
- - '>='
|
|
61
54
|
- !ruby/object:Gem::Version
|
|
62
55
|
version: '3.0'
|
|
63
56
|
- !ruby/object:Gem::Dependency
|
|
64
57
|
name: test-unit
|
|
65
58
|
requirement: !ruby/object:Gem::Requirement
|
|
66
|
-
none: false
|
|
67
59
|
requirements:
|
|
68
|
-
- -
|
|
60
|
+
- - '>='
|
|
69
61
|
- !ruby/object:Gem::Version
|
|
70
62
|
version: '0'
|
|
71
63
|
type: :development
|
|
72
64
|
prerelease: false
|
|
73
65
|
version_requirements: !ruby/object:Gem::Requirement
|
|
74
|
-
none: false
|
|
75
66
|
requirements:
|
|
76
|
-
- -
|
|
67
|
+
- - '>='
|
|
77
68
|
- !ruby/object:Gem::Version
|
|
78
69
|
version: '0'
|
|
79
70
|
- !ruby/object:Gem::Dependency
|
|
80
71
|
name: mocha
|
|
81
72
|
requirement: !ruby/object:Gem::Requirement
|
|
82
|
-
none: false
|
|
83
73
|
requirements:
|
|
84
|
-
- -
|
|
74
|
+
- - '>='
|
|
85
75
|
- !ruby/object:Gem::Version
|
|
86
76
|
version: '0'
|
|
87
77
|
type: :development
|
|
88
78
|
prerelease: false
|
|
89
79
|
version_requirements: !ruby/object:Gem::Requirement
|
|
90
|
-
none: false
|
|
91
80
|
requirements:
|
|
92
|
-
- -
|
|
81
|
+
- - '>='
|
|
93
82
|
- !ruby/object:Gem::Version
|
|
94
83
|
version: '0'
|
|
95
84
|
description: Render versioned views automagically based on the clients requested version.
|
|
@@ -102,28 +91,32 @@ extra_rdoc_files: []
|
|
|
102
91
|
files:
|
|
103
92
|
- .gitignore
|
|
104
93
|
- .travis.yml
|
|
94
|
+
- CHANGELOG.md
|
|
105
95
|
- Gemfile
|
|
106
96
|
- Gemfile.lock
|
|
107
97
|
- README.md
|
|
108
98
|
- Rakefile
|
|
109
99
|
- images/versioncake-logo450x100.png
|
|
110
100
|
- lib/versioncake.rb
|
|
111
|
-
- lib/versioncake/
|
|
112
|
-
- lib/versioncake/
|
|
113
|
-
- lib/versioncake/
|
|
114
|
-
- lib/versioncake/
|
|
115
|
-
- lib/versioncake/
|
|
116
|
-
- lib/versioncake/
|
|
101
|
+
- lib/versioncake/configuration.rb
|
|
102
|
+
- lib/versioncake/controller_additions.rb
|
|
103
|
+
- lib/versioncake/railtie.rb
|
|
104
|
+
- lib/versioncake/strategies/custom_strategy.rb
|
|
105
|
+
- lib/versioncake/strategies/extraction_strategy.rb
|
|
106
|
+
- lib/versioncake/strategies/http_accept_parameter_strategy.rb
|
|
107
|
+
- lib/versioncake/strategies/http_header_strategy.rb
|
|
108
|
+
- lib/versioncake/strategies/query_parameter_strategy.rb
|
|
109
|
+
- lib/versioncake/strategies/request_parameter_strategy.rb
|
|
117
110
|
- lib/versioncake/version.rb
|
|
111
|
+
- lib/versioncake/versioned_request.rb
|
|
112
|
+
- lib/versioncake/view_additions.rb
|
|
118
113
|
- test/app/controllers/renders_controller.rb
|
|
119
114
|
- test/app/views/renders/index.html.erb
|
|
120
115
|
- test/app/views/renders/index.v1.html.erb
|
|
121
116
|
- test/app/views/renders/index.v2.html.erb
|
|
122
117
|
- test/config.ru
|
|
123
118
|
- test/config/application.rb
|
|
124
|
-
- test/config/database.yml
|
|
125
119
|
- test/config/routes.rb
|
|
126
|
-
- test/db/test.sqlite3
|
|
127
120
|
- test/fixtures/partials/_versioned.erb
|
|
128
121
|
- test/fixtures/partials/_versioned.v1.erb
|
|
129
122
|
- test/fixtures/partials/_versioned.v2.erb
|
|
@@ -139,29 +132,35 @@ files:
|
|
|
139
132
|
- test/script/rails
|
|
140
133
|
- test/template/render_test.rb
|
|
141
134
|
- test/test_helper.rb
|
|
135
|
+
- test/unit/configuration_test.rb
|
|
136
|
+
- test/unit/strategies/extraction_strategy_test.rb
|
|
137
|
+
- test/unit/strategies/http_accept_parameter_strategy_test.rb
|
|
138
|
+
- test/unit/strategies/http_header_strategy_test.rb
|
|
139
|
+
- test/unit/strategies/query_parameter_strategy_test.rb
|
|
140
|
+
- test/unit/strategies/request_parameter_strategy_test.rb
|
|
141
|
+
- test/unit/versioned_request_test.rb
|
|
142
142
|
- versioncake.gemspec
|
|
143
143
|
homepage: https://github.com/bwillis/versioncake
|
|
144
144
|
licenses: []
|
|
145
|
+
metadata: {}
|
|
145
146
|
post_install_message:
|
|
146
147
|
rdoc_options: []
|
|
147
148
|
require_paths:
|
|
148
149
|
- lib
|
|
149
150
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
150
|
-
none: false
|
|
151
151
|
requirements:
|
|
152
|
-
- -
|
|
152
|
+
- - '>='
|
|
153
153
|
- !ruby/object:Gem::Version
|
|
154
154
|
version: '0'
|
|
155
155
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
|
-
none: false
|
|
157
156
|
requirements:
|
|
158
|
-
- -
|
|
157
|
+
- - '>='
|
|
159
158
|
- !ruby/object:Gem::Version
|
|
160
159
|
version: '0'
|
|
161
160
|
requirements: []
|
|
162
161
|
rubyforge_project:
|
|
163
|
-
rubygems_version:
|
|
162
|
+
rubygems_version: 2.0.3
|
|
164
163
|
signing_key:
|
|
165
|
-
specification_version:
|
|
164
|
+
specification_version: 4
|
|
166
165
|
summary: Easily render versions of your rails views.
|
|
167
166
|
test_files: []
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
require 'action_controller'
|
|
2
|
-
|
|
3
|
-
module ActionController #:nodoc:
|
|
4
|
-
module Versioning
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
attr_accessor :requested_version, :is_latest_version, :derived_version
|
|
8
|
-
|
|
9
|
-
included do
|
|
10
|
-
prepend_before_filter :set_version
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
protected
|
|
14
|
-
def set_version
|
|
15
|
-
@requested_version = ActionView::Template::Versions.extract_version request
|
|
16
|
-
|
|
17
|
-
if @requested_version.nil?
|
|
18
|
-
@derived_version = ActionView::Template::Versions.latest_version
|
|
19
|
-
elsif ActionView::Template::Versions.supports_version? @requested_version
|
|
20
|
-
@derived_version = @requested_version
|
|
21
|
-
elsif @requested_version > ActionView::Template::Versions.latest_version
|
|
22
|
-
raise ActionController::RoutingError.new("No route match for version")
|
|
23
|
-
else
|
|
24
|
-
raise ActionController::RoutingError.new("Version is deprecated")
|
|
25
|
-
end
|
|
26
|
-
@_lookup_context.versions = ActionView::Template::Versions.supported_versions(@derived_version)
|
|
27
|
-
@is_latest_version = @derived_version == ActionView::Template::Versions.latest_version
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
ActionController::Base.send(:include, ActionController::Versioning)
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
require 'action_view'
|
|
2
|
-
|
|
3
|
-
ActionView::LookupContext.class_eval do
|
|
4
|
-
|
|
5
|
-
# register an addition detail for the lookup context to understand,
|
|
6
|
-
# this will allow us to have the versions available upon lookup in
|
|
7
|
-
# the resolver.
|
|
8
|
-
register_detail(:versions){ ActionView::Template::Versions.supported_versions }
|
|
9
|
-
|
|
10
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require 'rails'
|
|
2
|
-
|
|
3
|
-
class ActionViewVersions < Rails::Railtie
|
|
4
|
-
initializer "view_versions" do |app|
|
|
5
|
-
ActiveSupport.on_load(:action_view) do
|
|
6
|
-
if app.config.respond_to?(:view_versions)
|
|
7
|
-
ActionView::Template::Versions.supported_version_numbers = app.config.view_versions
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
if app.config.respond_to?(:view_version_extraction_strategy)
|
|
11
|
-
ActionView::Template::Versions.extraction_strategy = app.config.view_version_extraction_strategy
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
if app.config.respond_to?(:view_version_string)
|
|
15
|
-
ActionView::Template::Versions.version_string = app.config.view_version_string
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
require 'active_support/core_ext/module/attribute_accessors.rb'
|
|
2
|
-
|
|
3
|
-
module ActionView
|
|
4
|
-
class Template
|
|
5
|
-
module Versions
|
|
6
|
-
|
|
7
|
-
mattr_accessor :version_string
|
|
8
|
-
self.version_string = "api_version"
|
|
9
|
-
|
|
10
|
-
mattr_accessor :supported_version_numbers
|
|
11
|
-
self.supported_version_numbers = []
|
|
12
|
-
|
|
13
|
-
mattr_accessor :extraction_strategy
|
|
14
|
-
self.extraction_strategy = [:query_parameter]
|
|
15
|
-
|
|
16
|
-
def self.extract_version(request)
|
|
17
|
-
version = nil
|
|
18
|
-
extraction_strategy.each do |strategy|
|
|
19
|
-
version = apply_strategy(request, strategy)
|
|
20
|
-
break unless version.nil?
|
|
21
|
-
end
|
|
22
|
-
version
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def self.apply_strategy(request, strategy)
|
|
26
|
-
case strategy
|
|
27
|
-
when Proc
|
|
28
|
-
strategy.call(request)
|
|
29
|
-
when :http_accept_parameter
|
|
30
|
-
if request.headers.has_key?("HTTP_ACCEPT") &&
|
|
31
|
-
match = request.headers["HTTP_ACCEPT"].match(%{#{@@version_string}=([0-9])})
|
|
32
|
-
match[1].to_i
|
|
33
|
-
end
|
|
34
|
-
when :http_header
|
|
35
|
-
if request.headers.has_key? "HTTP_X_#{@@version_string.upcase}"
|
|
36
|
-
request.headers["HTTP_X_#{@@version_string.upcase}"].to_i
|
|
37
|
-
end
|
|
38
|
-
when :query_parameter
|
|
39
|
-
if request.query_parameters.has_key? @@version_string.to_sym
|
|
40
|
-
request.query_parameters[@@version_string.to_sym].to_i
|
|
41
|
-
end
|
|
42
|
-
when :request_parameter
|
|
43
|
-
if request.request_parameters.has_key? @@version_string.to_sym
|
|
44
|
-
request.request_parameters[@@version_string.to_sym].to_i
|
|
45
|
-
end
|
|
46
|
-
else
|
|
47
|
-
raise "Unknown extraction strategy #{strategy}"
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def self.extraction_strategy=(val)
|
|
52
|
-
case val
|
|
53
|
-
when Array
|
|
54
|
-
@@extraction_strategy = val
|
|
55
|
-
else
|
|
56
|
-
@@extraction_strategy = Array.wrap(val)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def self.supported_version_numbers=(val)
|
|
61
|
-
case val
|
|
62
|
-
when Range
|
|
63
|
-
@@supported_version_numbers = val.to_a
|
|
64
|
-
when Array
|
|
65
|
-
@@supported_version_numbers = val
|
|
66
|
-
else
|
|
67
|
-
@@supported_version_numbers = Array.wrap(val)
|
|
68
|
-
end
|
|
69
|
-
@@supported_version_numbers.sort!.reverse!
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def self.supported_versions(requested_version_number=nil)
|
|
73
|
-
supported_version_numbers.collect do |supported_version_number|
|
|
74
|
-
if requested_version_number.nil? || supported_version_number <= requested_version_number
|
|
75
|
-
:"v#{supported_version_number}"
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def self.supports_version?(version)
|
|
81
|
-
supported_version_numbers.include? version
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def self.latest_version
|
|
85
|
-
supported_version_numbers.first
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
data/test/config/database.yml
DELETED
data/test/db/test.sqlite3
DELETED
|
File without changes
|