rack-json-api-version 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +1 -0
- data/lib/rack/json/api/version/version.rb +9 -0
- data/lib/rack/json/api/version.rb +34 -0
- data/rack-json-api-version.gemspec +24 -0
- data/spec/version/version_spec.rb +67 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eec834773db4d47d5fd5d95ef7f9eb689ccec32e
|
4
|
+
data.tar.gz: 302b7ee16d55659b44a3ed6474208081cadee56d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d015b1e2dd95ea5ead7a5fc21b0e056c16a28e0b996e6ee69f3570e4221299ed3cb607ed3bd4d027f15f811bdf59d8da98f751a1202d9f779c0c5e1e881569f
|
7
|
+
data.tar.gz: d9c9233cac38d7d7bb4430d790dfa8bdc1cfbe63eeaf4da5ba099c62ce89625a211ae3cd86a38e2a19550d98d168197cc045d99fb94ead88fb9d93977362ba0e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Daniel Mantilla
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Rack::Json::Api::Version
|
2
|
+
|
3
|
+
Extract JSON API version number from request headers.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rack-json-api-version'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rack-json-api-version
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In `Rails` add this to your `application.rb' file
|
22
|
+
|
23
|
+
config.middleware.use Rack::Json::Api::Version, vendor: 'company_name'
|
24
|
+
|
25
|
+
Where `vendor` is your company name, without any dots
|
26
|
+
|
27
|
+
Request header `Accept` should be something like this: `application/vnd.my_company.v1.1+json`
|
28
|
+
|
29
|
+
In your controller you will have the version available by using
|
30
|
+
|
31
|
+
env['api.version'] # will return 1.1
|
32
|
+
|
33
|
+
# OR
|
34
|
+
|
35
|
+
request.env['api.version']
|
36
|
+
|
37
|
+
This gem will modify the `Accept` headers to `application/json` and keep the original value under the `ORIGINAL_HTTP_ACCEPT` key.
|
38
|
+
|
39
|
+
If the `Accept` headers contain `application/json`, `env['api.version']` will return `nil`. In this case `ORIGINAL_HTTP_ACCEPT` won't be set.
|
40
|
+
|
41
|
+
In general, if the `Accept` header does not conform to this structure `application/vnd.my_company.vn+json` nothing will be done to the headers.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( http://github.com/<my-github-username>/rack-json-api-version/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "rack/json/api/version/version"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
module Json
|
5
|
+
module Api
|
6
|
+
class InvalidVersion < StandardError; end
|
7
|
+
|
8
|
+
class Version
|
9
|
+
def initialize(app, opts={})
|
10
|
+
@app = app
|
11
|
+
@vendor = opts[:vendor] || raise('Vendor name required')
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
update(env)
|
16
|
+
@app.call env
|
17
|
+
end
|
18
|
+
|
19
|
+
def extract_version(env)
|
20
|
+
env['HTTP_ACCEPT'].to_s[/^application\/vnd\.#{@vendor}\.v(.+)\+json/, 1]
|
21
|
+
end
|
22
|
+
|
23
|
+
def update(env)
|
24
|
+
version = extract_version(env)
|
25
|
+
env['api.version'] = version
|
26
|
+
unless version.nil?
|
27
|
+
env['ORIGINAL_HTTP_ACCEPT'] = env['HTTP_ACCEPT']
|
28
|
+
env['HTTP_ACCEPT'] = 'application/json'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/json/api/version/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-json-api-version"
|
8
|
+
spec.version = Rack::Json::Api::Version::VERSION
|
9
|
+
spec.authors = ["Daniel Mantilla"]
|
10
|
+
spec.email = ["dan@appcabinet.com"]
|
11
|
+
spec.summary = %q{Middleware to extract the API version from request headers}
|
12
|
+
spec.description = %q{The API version should reside on the Accept headers. Check documentation for deatils}
|
13
|
+
spec.homepage = "http://github.com/dmantilla/rack-json-api-version"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack/json/api/version'
|
3
|
+
|
4
|
+
describe Rack::Json::Api::Version do
|
5
|
+
let(:app) { Object.new }
|
6
|
+
let(:klass) { Rack::Json::Api::Version }
|
7
|
+
|
8
|
+
describe '.initialize' do
|
9
|
+
it 'creates a new instance' do
|
10
|
+
expect { klass.new(app, vendor: 'abc' ) }.not_to raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'failures' do
|
14
|
+
it 'complains when the vendor is missing' do
|
15
|
+
expect { klass.new(app) }.to raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#extract_version' do
|
21
|
+
let(:version) { klass.new(app, vendor: 'abc') }
|
22
|
+
|
23
|
+
context 'valid versions' do
|
24
|
+
it 'is version 1' do
|
25
|
+
env = { 'HTTP_ACCEPT' => 'application/vnd.abc.v1+json'}
|
26
|
+
expect(version.extract_version(env)).to eq('1')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'is version 1.1' do
|
30
|
+
env = { 'HTTP_ACCEPT' => 'application/vnd.abc.v1.1+json'}
|
31
|
+
expect(version.extract_version(env)).to eq('1.1')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'version not found' do
|
36
|
+
it 'returns nil' do
|
37
|
+
env = { 'HTTP_ACCEPT' => 'application/json'}
|
38
|
+
expect(version.extract_version(env)).to eq(nil)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'returns nil' do
|
42
|
+
env = { 'HTTP_ACCEPT' => 'something'}
|
43
|
+
expect(version.extract_version(env)).to eq(nil)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#update' do
|
49
|
+
let(:version) { klass.new(app, vendor: 'abc') }
|
50
|
+
|
51
|
+
it 'does nothing' do
|
52
|
+
env = {}
|
53
|
+
version.update(env)
|
54
|
+
expect(env).to eq({'api.version' => nil})
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'sets the header and saves the original' do
|
58
|
+
env = {'HTTP_ACCEPT' => 'application/vnd.abc.v1+json'}
|
59
|
+
version.update(env)
|
60
|
+
expect(env).to eq({
|
61
|
+
'api.version' => '1',
|
62
|
+
'HTTP_ACCEPT' => 'application/json',
|
63
|
+
'ORIGINAL_HTTP_ACCEPT' => 'application/vnd.abc.v1+json'
|
64
|
+
})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-json-api-version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Mantilla
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: The API version should reside on the Accept headers. Check documentation
|
56
|
+
for deatils
|
57
|
+
email:
|
58
|
+
- dan@appcabinet.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/rack/json/api/version.rb
|
69
|
+
- lib/rack/json/api/version/version.rb
|
70
|
+
- rack-json-api-version.gemspec
|
71
|
+
- spec/version/version_spec.rb
|
72
|
+
homepage: http://github.com/dmantilla/rack-json-api-version
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.0
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Middleware to extract the API version from request headers
|
96
|
+
test_files:
|
97
|
+
- spec/version/version_spec.rb
|