api-versions 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,21 +1,17 @@
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 in your Rails app, it is not very DRY to include the same resources over and over again.
4
4
  In your Gemfile:
5
5
 
6
- gem "api-versions", "~> 0.0.2"
6
+ gem "api-versions", "~> 0.0.4"
7
7
 
8
8
  In your routes.rb file:
9
9
 
10
- include ApiVersions
11
-
12
- Further down...
13
-
14
10
  namespace :api do
15
- self.api_version = 1 # If no API version is specified in the Accept header, this is what will be used.
16
- self.vendor = "myvendor" # For HTTP Accept Header application/vnd.myvendor+json;version=1
11
+ default_api_version 1 # If no API version is specified in the Accept header, this is what will be used.
12
+ api_vendor "myvendor" # For HTTP Accept Header application/vnd.myvendor+json;version=1
17
13
 
18
- constraints ApiVersionCheck.new(:version => 1) do
14
+ constraints api_version_check(:version => 1) do
19
15
  scope :module => :v1 do
20
16
  cache_resources :as => :v1 do
21
17
  resources :authorizations, :only => [ :create ]
@@ -23,20 +19,39 @@ Further down...
23
19
  end
24
20
  end
25
21
 
26
- constraints ApiVersionCheck.new(:version => 2) do
22
+ constraints api_version_check(:version => 2) do
27
23
  scope :module => :v2 do
28
24
  inherit_resources :from => :v1
29
25
  end
30
26
  end
31
27
  end
32
28
 
29
+ rake routes outputs:
30
+
31
+ api_authorizations GET /api/authorizations(.:format) api/v1/authorizations#index
32
+ POST /api/authorizations(.:format) api/v1/authorizations#create
33
+ new_api_authorization GET /api/authorizations/new(.:format) api/v1/authorizations#new
34
+ edit_api_authorization GET /api/authorizations/:id/edit(.:format) api/v1/authorizations#edit
35
+ api_authorization GET /api/authorizations/:id(.:format) api/v1/authorizations#show
36
+ PUT /api/authorizations/:id(.:format) api/v1/authorizations#update
37
+ DELETE /api/authorizations/:id(.:format) api/v1/authorizations#destroy
38
+ GET /api/authorizations(.:format) api/v2/authorizations#index
39
+ POST /api/authorizations(.:format) api/v2/authorizations#create
40
+ GET /api/authorizations/new(.:format) api/v2/authorizations#new
41
+ GET /api/authorizations/:id/edit(.:format) api/v2/authorizations#edit
42
+ GET /api/authorizations/:id(.:format) api/v2/authorizations#show
43
+ PUT /api/authorizations/:id(.:format) api/v2/authorizations#update
44
+ DELETE /api/authorizations/:id(.:format) api/v2/authorizations#destroy
45
+
46
+ Then the client simply sets the Accept header "application/vnd.myvendor+json;version=1". If no version is specified, the default version you set will be assumed. You'll of course still need to copy all of your controllers over, even if they haven't changed from version to version. At least you'll remove a bit of the mess in your routes file.
47
+
33
48
  A more complicated example
34
49
  --------------------------
35
50
  namespace :api do
36
- self.api_version = 1
37
- self.vendor = "bulletin"
51
+ default_api_version 1
52
+ api_vendor "myvendor"
38
53
 
39
- constraints ApiVersionCheck.new(:version => 1) do
54
+ constraints api_version_check(:version => 1) do
40
55
  scope :module => :v1 do
41
56
  cache_resources :as => :v1 do
42
57
  resources :authorizations, :only => [ :create ]
@@ -48,7 +63,7 @@ A more complicated example
48
63
 
49
64
  # Version 2 of the API has everything in Version 1, plus my_new_resource
50
65
  # Version 2 will cache this entire package of resources
51
- constraints ApiVersionCheck.new(:version => 2) do
66
+ constraints api_version_check(:version => 2) do
52
67
  scope :module => :v2 do
53
68
  cache_resources :as => :v2 do
54
69
  resources :my_new_resource
@@ -61,7 +76,7 @@ A more complicated example
61
76
  # virtue of API Version 2 having everything in Version 1, Version 3
62
77
  # also has everything in Version 1.
63
78
 
64
- constraints ApiVersionCheck.new(:version => 3) do
79
+ constraints api_version_check(:version => 3) do
65
80
  scope :module => :v3 do
66
81
  inherit_resources :from => :v2
67
82
  end
@@ -1,5 +1,5 @@
1
1
  module Api
2
2
  module Versions
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
data/lib/api-versions.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  require "api-versions/version"
2
2
 
3
+ class Engine < Rails::Engine
4
+ initializer "api_versions" do
5
+ config.to_prepare { ActionDispatch::Routing::Mapper.send :include, ApiVersions }
6
+ end
7
+ end
8
+
3
9
  module ApiVersions
4
10
 
5
11
  def inherit_resources(args)
@@ -15,14 +21,23 @@ module ApiVersions
15
21
  end
16
22
 
17
23
  def api_version=(version)
18
- @@version = version
24
+ ApiVersions::ApiVersionCheck.api_version = version
19
25
  end
26
+ alias_method :default_api_version, :api_version=
27
+
20
28
 
21
29
  def vendor=(vendor)
22
- @@vendor = vendor
30
+ ApiVersions::ApiVersionCheck.api_vendor = vendor
31
+ end
32
+ alias_method :api_vendor, :vendor=
33
+
34
+ def api_version_check(*args)
35
+ ApiVersions::ApiVersionCheck.new(*args)
23
36
  end
24
37
 
25
38
  class ApiVersionCheck
39
+
40
+ cattr_accessor :api_version, :api_vendor
26
41
 
27
42
  def initialize(args = {})
28
43
  @process_version = args[:version]
@@ -35,7 +50,7 @@ module ApiVersions
35
50
  private
36
51
 
37
52
  def accepts_proper_format?(request)
38
- !!(request.headers['Accept'] =~ /^application\/vnd\.#{@@vendor}\+json/)
53
+ !!(request.headers['Accept'] =~ /^application\/vnd\.#{self.class.api_vendor}\+json/)
39
54
  end
40
55
 
41
56
  def matches_version?(request)
@@ -43,7 +58,7 @@ module ApiVersions
43
58
  end
44
59
 
45
60
  def unversioned?(request)
46
- @process_version == @@version && !(request.headers['Accept'] =~ /version\s*?=\s*?\d*\b/i)
61
+ @process_version == self.class.api_version && !(request.headers['Accept'] =~ /version\s*?=\s*?\d*\b/i)
47
62
  end
48
63
 
49
64
  end
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.3
4
+ version: 0.0.4
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-02-09 00:00:00.000000000 Z
12
+ date: 2012-03-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Useful for API versioning.
15
15
  email: