versioncake 0.3.2 → 0.4.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.
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -19,6 +19,8 @@ app/views/posts/
|
|
19
19
|
- Clients can request API versions through different strategies
|
20
20
|
- Dry your controller logic with exposed helpers
|
21
21
|
|
22
|
+
Check out https://github.com/bwillis/350-rest-api-versioning for a comparison of traditional versioning approaches and a versioncake implementation.
|
23
|
+
|
22
24
|
## Install
|
23
25
|
|
24
26
|
```
|
@@ -168,9 +170,14 @@ config.view_version_extraction_strategy = :query_parameter # [:http_header, :htt
|
|
168
170
|
```
|
169
171
|
These are the available strategies:
|
170
172
|
- **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.)
|
171
|
-
- **http_header**: Api version HTTP header ie. ```API-Version: 1```
|
173
|
+
- **http_header**: Api version HTTP header ie. ```X-API-Version: 1```
|
172
174
|
- **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)
|
173
|
-
- **custom**: `lambda {|request| request.headers["
|
175
|
+
- **custom**: `lambda {|request| request.headers["HTTP_X_MY_VERSION"].to_i }` takes the request object and must return an integer
|
176
|
+
|
177
|
+
The strategies use a default string of `api_version`, but that can be changed:
|
178
|
+
```ruby
|
179
|
+
config.view_version_string = "special_version_parameter_name"
|
180
|
+
```
|
174
181
|
|
175
182
|
### Version your views
|
176
183
|
|
@@ -10,6 +10,10 @@ class ActionViewVersions < Rails::Railtie
|
|
10
10
|
if app.config.respond_to?(:view_version_extraction_strategy)
|
11
11
|
ActionView::Template::Versions.extraction_strategy = app.config.view_version_extraction_strategy
|
12
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
|
13
17
|
end
|
14
18
|
end
|
15
19
|
end
|
@@ -4,7 +4,8 @@ module ActionView
|
|
4
4
|
class Template
|
5
5
|
module Versions
|
6
6
|
|
7
|
-
|
7
|
+
mattr_accessor :version_string
|
8
|
+
self.version_string = "api_version"
|
8
9
|
|
9
10
|
mattr_accessor :supported_version_numbers
|
10
11
|
self.supported_version_numbers = []
|
@@ -14,18 +15,18 @@ module ActionView
|
|
14
15
|
|
15
16
|
EXTRACTION_STRATEGIES = {
|
16
17
|
:query_parameter => lambda { |request|
|
17
|
-
if request.query_parameters.has_key?
|
18
|
-
request.query_parameters[
|
18
|
+
if request.query_parameters.has_key? @@version_string.to_sym
|
19
|
+
request.query_parameters[@@version_string.to_sym].to_i
|
19
20
|
end
|
20
21
|
},
|
21
22
|
:http_header => lambda { |request|
|
22
|
-
if request.headers.has_key? "
|
23
|
-
request.headers["
|
23
|
+
if request.headers.has_key? "HTTP_X_#{@@version_string.upcase}"
|
24
|
+
request.headers["HTTP_X_#{@@version_string.upcase}"].to_i
|
24
25
|
end
|
25
26
|
},
|
26
27
|
:http_accept_parameter => lambda { |request|
|
27
28
|
if request.headers.has_key?("HTTP_ACCEPT") &&
|
28
|
-
match = request.headers["HTTP_ACCEPT"].match(%{#{
|
29
|
+
match = request.headers["HTTP_ACCEPT"].match(%{#{@@version_string}=([0-9])})
|
29
30
|
match[1].to_i
|
30
31
|
end
|
31
32
|
}
|
data/lib/versioncake/version.rb
CHANGED
@@ -4,23 +4,33 @@ require "action_controller"
|
|
4
4
|
require "action_controller/test_case"
|
5
5
|
|
6
6
|
class RendersControllerTest < ActionController::TestCase
|
7
|
+
|
8
|
+
setup do
|
9
|
+
# change the version string for configuration testing
|
10
|
+
ActionView::Template::Versions.version_string = "version"
|
11
|
+
end
|
12
|
+
|
13
|
+
teardown do
|
14
|
+
ActionView::Template::Versions.version_string = "api_version"
|
15
|
+
end
|
16
|
+
|
7
17
|
test "render latest version of partial" do
|
8
18
|
get :index
|
9
19
|
assert_equal @response.body, "index.v2.html.erb"
|
10
20
|
end
|
11
21
|
|
12
22
|
test "exposes the requested version" do
|
13
|
-
get :index, "
|
23
|
+
get :index, "version" => "1"
|
14
24
|
assert_equal @controller.requested_version, 1
|
15
25
|
end
|
16
26
|
|
17
27
|
test "exposes latest version when requesting the latest" do
|
18
|
-
get :index, "
|
28
|
+
get :index, "version" => "3"
|
19
29
|
assert @controller.is_latest_version
|
20
30
|
end
|
21
31
|
|
22
32
|
test "reports not the latest version" do
|
23
|
-
get :index, "
|
33
|
+
get :index, "version" => "1"
|
24
34
|
assert !@controller.is_latest_version
|
25
35
|
end
|
26
36
|
|
@@ -66,19 +76,19 @@ tests RendersController
|
|
66
76
|
end
|
67
77
|
|
68
78
|
test "renders version 1 of the partial based on the header API-Version" do
|
69
|
-
@controller.request.stubs(:headers).returns({"
|
79
|
+
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "1"})
|
70
80
|
get :index
|
71
81
|
assert_equal @response.body, "index.v1.html.erb"
|
72
82
|
end
|
73
83
|
|
74
84
|
test "renders version 2 of the partial based on the header API-Version" do
|
75
|
-
@controller.request.stubs(:headers).returns({"
|
85
|
+
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "2"})
|
76
86
|
get :index
|
77
87
|
assert_equal @response.body, "index.v2.html.erb"
|
78
88
|
end
|
79
89
|
|
80
90
|
test "renders the latest available version (v2) of the partial based on the header API-Version" do
|
81
|
-
@controller.request.stubs(:headers).returns({"
|
91
|
+
@controller.request.stubs(:headers).returns({"HTTP_X_API_VERSION" => "3"})
|
82
92
|
get :index
|
83
93
|
assert_equal @response.body, "index.v2.html.erb"
|
84
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: versioncake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-10-
|
13
|
+
date: 2012-10-29 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: actionpack
|
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
requirements: []
|
162
162
|
rubyforge_project:
|
163
|
-
rubygems_version: 1.8.
|
163
|
+
rubygems_version: 1.8.19
|
164
164
|
signing_key:
|
165
165
|
specification_version: 3
|
166
166
|
summary: Easily render versions of your rails views.
|