api-versions 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +44 -4
- data/lib/api-versions.rb +14 -17
- data/lib/api-versions/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
API-Versions
|
2
2
|
================
|
3
|
-
If you have multiple versions of an API, it is not very DRY to include the same resources over and over again.
|
3
|
+
If you have multiple versions of an API, it is not very DRY to include the same resources over and over again.
|
4
|
+
In your Gemfile:
|
5
|
+
|
6
|
+
gem "api-versions", "~> 0.0.2"
|
7
|
+
|
8
|
+
In your routes.rb file:
|
4
9
|
|
5
10
|
include ApiVersions
|
6
|
-
has_common_resources
|
7
11
|
|
8
12
|
Further down...
|
9
13
|
|
@@ -26,8 +30,44 @@ Further down...
|
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
29
|
-
|
30
|
-
|
33
|
+
A more complicated example
|
34
|
+
--------------------------
|
35
|
+
namespace :api do
|
36
|
+
self.api_version = 1
|
37
|
+
self.vendor = "bulletin"
|
38
|
+
|
39
|
+
constraints ApiVersionCheck.new(:version => 1) do
|
40
|
+
scope :module => :v1 do
|
41
|
+
cache_resources :as => :v1 do
|
42
|
+
resources :authorizations, :only => [ :create ]
|
43
|
+
resources :foo
|
44
|
+
resources :bar
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Version 2 of the API has everything in Version 1, plus my_new_resource
|
50
|
+
# Version 2 will cache this entire package of resources
|
51
|
+
constraints ApiVersionCheck.new(:version => 2) do
|
52
|
+
scope :module => :v2 do
|
53
|
+
cache_resources :as => :v2 do
|
54
|
+
resources :my_new_resource
|
55
|
+
inherit_resources :from => :v1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Version 3 of the API has everything in API Version 2, and by
|
61
|
+
# virtue of API Version 2 having everything in Version 1, Version 3
|
62
|
+
# also has everything in Version 1.
|
63
|
+
|
64
|
+
constraints ApiVersionCheck.new(:version => 3) do
|
65
|
+
scope :module => :v3 do
|
66
|
+
inherit_resources :from => :v2
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
31
71
|
|
32
72
|
License
|
33
73
|
=======
|
data/lib/api-versions.rb
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
require "api-versions/version"
|
2
2
|
|
3
3
|
module ApiVersions
|
4
|
-
|
5
|
-
|
4
|
+
|
5
|
+
def inherit_resources(args)
|
6
|
+
[*args[:from]].each do |block|
|
7
|
+
@resource_cache[block].call
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def cache_resources(args, &block)
|
12
|
+
@resource_cache ||= {}
|
13
|
+
@resource_cache.merge!(args[:as] => block)
|
14
|
+
block.call
|
6
15
|
end
|
7
16
|
|
8
17
|
def api_version=(version)
|
@@ -13,22 +22,10 @@ module ApiVersions
|
|
13
22
|
@@vendor = vendor
|
14
23
|
end
|
15
24
|
|
16
|
-
module InstanceMethods
|
17
|
-
def inherit_resources(args)
|
18
|
-
@resource_cache[args[:from]].call
|
19
|
-
end
|
20
|
-
|
21
|
-
def cache_resources(args, &block)
|
22
|
-
@resource_cache ||= {}
|
23
|
-
@resource_cache.merge!(args[:as] => block)
|
24
|
-
block.call
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
25
|
class ApiVersionCheck
|
29
26
|
|
30
27
|
def initialize(args = {})
|
31
|
-
@
|
28
|
+
@process_version = args[:version]
|
32
29
|
end
|
33
30
|
|
34
31
|
def matches?(request)
|
@@ -42,11 +39,11 @@ module ApiVersions
|
|
42
39
|
end
|
43
40
|
|
44
41
|
def matches_version?(request)
|
45
|
-
!!(request.headers['Accept'] =~ /version\s*?=\s*?#{@
|
42
|
+
!!(request.headers['Accept'] =~ /version\s*?=\s*?#{@process_version}\b/)
|
46
43
|
end
|
47
44
|
|
48
45
|
def unversioned?(request)
|
49
|
-
@
|
46
|
+
@process_version == @@version && !(request.headers['Accept'] =~ /version\s*?=\s*?\d*\b/i)
|
50
47
|
end
|
51
48
|
|
52
49
|
end
|
data/lib/api-versions/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-versions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Useful for API versioning.
|
15
15
|
email:
|