rack-api-versioning 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p374@rack-api-versioning"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.18.5 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rafter_api_version.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nick Zalabak
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,60 @@
1
+ # RackApiVersioning
2
+
3
+ Version your Rack based API's using headers either through middleware or routing constraints.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-api-versioning'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-api-versioning
18
+
19
+ ## Usage in Rails
20
+ ```ruby
21
+ Your::Application.routes.draw do
22
+ constraints RackApiVersioning::Constraint.new( :target_version => 3,
23
+ :default_version => 2,
24
+ :app_name => "awesome-app",
25
+ :media_type => "json" ) do
26
+ scope :module => :v3 do
27
+ resources :orders, :only => [:create, :show] do
28
+
29
+ end
30
+ end
31
+ end
32
+ ```
33
+ ## Usage in any Rack app
34
+ ```ruby
35
+ Rack::Builder.new do
36
+
37
+ map '/' do
38
+ use RackApiVersioning::Middleware,
39
+ :app_name => "awesome-app",
40
+ :default_version => 1,
41
+ :target_version => 2
42
+
43
+ run lambda { |env|
44
+ [200, {"Content-Type" => "text/html"}, "Testing RackApiVersioning"]
45
+ }
46
+ end
47
+
48
+ map "/new-improved-api" do
49
+ use RackApiVersioning::Middleware,
50
+ :app_name => "awesome-app",
51
+ :default_version => 2,
52
+ :target_version => 3
53
+
54
+ run lambda { |env|
55
+ [200, {"Content-Type" => "text/html"}, "Testing RackApiVersioning"]
56
+ }
57
+ end
58
+
59
+ end
60
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+
3
+ Dir.glob(File.expand_path(File.dirname(__FILE__) + "/rack_api_versioning/*.rb")) { |file| require file }
@@ -0,0 +1,63 @@
1
+ ##
2
+ # Use an instance of RackApiVersioning::Constraint in a Rackup file or in Rails routing.
3
+
4
+ module RackApiVersioning
5
+ class Constraint
6
+ ##
7
+ # There are four options:
8
+ # :target_version => This is the API version you want to use,
9
+ # when left unspecified it defaults to 1.
10
+ #
11
+ # :default_version => This is the API version to failover to,
12
+ # when left unspecified it defaults to 1.
13
+ #
14
+ # :app_name => The app name that should be included in the 'Accept'
15
+ # header, when left unspecified it defaults to "api".
16
+ #
17
+ # :media_type => The desired media type the API should respond with,
18
+ # when left unspecified it defaults to "*".
19
+ #
20
+ def initialize(options = {})
21
+ @target_version = options[:target_version] || 1
22
+ @default_version = options[:default_version] || 1
23
+ @app_name = options[:app_name] || "api"
24
+ @media_type = options[:media_type] || "*"
25
+ end
26
+
27
+ ##
28
+ # Valid versioned API headers look like this:
29
+ # Accept: application/vnd.api-v1+json
30
+ # If no API version header is provided the default API version will be used.
31
+ def matches?(request)
32
+ versioned_accept_header?(request) || default_version?(request)
33
+ end
34
+
35
+ private
36
+
37
+ def versioned_accept_header?(request)
38
+ accept = accept_header(request)
39
+ accept && accept.match(/application\/vnd\.#{@app_name}-v#{@target_version}\+#{@media_type}/)
40
+ end
41
+
42
+ def unversioned_accept_header?(request)
43
+ accept = accept_header(request)
44
+ (accept.nil? || accept == "" ) || accept.match(/application\/vnd\.#{@app_name}/).nil?
45
+ end
46
+
47
+ def accept_header(request)
48
+ if request.respond_to?(:headers)
49
+ # support for Rails ActionDispatch::Request
50
+ request.headers['Accept']
51
+
52
+ else
53
+ # support for the Rack env request
54
+ request['Accept']
55
+ end
56
+ end
57
+
58
+ def default_version?(request)
59
+ @target_version == @default_version && unversioned_accept_header?(request)
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,47 @@
1
+ ##
2
+ # Use pure Rack middleware to handle your API versioning
3
+
4
+ module RackApiVersioning
5
+
6
+ class Middleware
7
+
8
+ ##
9
+ # Valid options include the same options the RackApiVersioning::Constraint
10
+ # class uses. Specifically:
11
+ #
12
+ # :target_version => This is the API version you want to use,
13
+ # when left unspecified it defaults to 1.
14
+ #
15
+ # :default_version => This is the API version to failover to,
16
+ # when left unspecified it defaults to 1.
17
+ #
18
+ # :app_name => The app name that should be included in the 'Accept'
19
+ # header, when left unspecified it defaults to "api".
20
+ #
21
+ # :media_type => The desired media type the API should respond with,
22
+ # when left unspecified it defaults to "*".
23
+ #
24
+ def initialize(app, options = {})
25
+ @app = app
26
+ @api_version_constraint = Constraint.new(options)
27
+ end
28
+
29
+ def call(env)
30
+ if @api_version_constraint.matches?(env)
31
+ @app.call(env)
32
+ else
33
+ api_version_error
34
+ end
35
+ end
36
+
37
+ ##
38
+ # Subclass and override this method if you'd like an alternate behavior.
39
+ def api_version_error
40
+ body_text = "The request did not contain an appropriate API version header."
41
+ [400, {'Content-Type' => 'text/plain; charset=utf-8',
42
+ 'Content-Length' => body_text.size.to_s}, [body_text]]
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ module RackApiVersioning
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rack_api_versioning/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nick Zalabak"]
6
+ gem.email = ["techwhizbang@gmail.com"]
7
+ gem.description = %q{Version your Rack based API's using headers either through middleware or routing constraints}
8
+ gem.summary = %q{Version your Rack based API's using headers either through middleware or routing constraints}
9
+ gem.homepage = "https://github.com/techwhizbang/rack-api-versioning"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rack-api-versioning"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RackApiVersioning::VERSION
17
+ gem.add_dependency %q<rack>, [">= 1.0"]
18
+ gem.add_development_dependency %q<rake>, ["0.9.2.2"]
19
+ gem.add_development_dependency %q<rspec>, ["~> 2.12.0"]
20
+ gem.add_development_dependency %q<rack-test>, ["~> 0.6.2"]
21
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe RackApiVersioning::Constraint do
4
+
5
+ describe "#match?" do
6
+
7
+ context "when a versioned API request header is provided" do
8
+
9
+ subject(:api_version) {
10
+ RackApiVersioning::Constraint.new( :default_version => 1,
11
+ :target_version => 2,
12
+ :app_name => "sweet-api" ) }
13
+
14
+ let(:request_with_good_header) do
15
+ stub('request', :headers => {"Accept" => "application/vnd.sweet-api-v2+json"})
16
+ end
17
+
18
+ let(:request_with_bad_header) do
19
+ stub('request', :headers => {"Accept" => "application/vnd.wrong-api-v2.json"})
20
+ end
21
+
22
+ it 'returns true when the format matches properly' do
23
+ api_version.matches?(request_with_good_header).should be_true
24
+ end
25
+
26
+ it 'returns false if the format is incorrect' do
27
+ api_version.matches?(request_with_bad_header).should be_false
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ context "when a versioned API request is missing" do
35
+
36
+ let(:request_with_no_header) { stub('request', :headers => {}) }
37
+
38
+ subject(:api_versions_same) {
39
+ RackApiVersioning::Constraint.new( :default_version => 5000,
40
+ :target_version => 5000 ) }
41
+
42
+ subject(:api_versions_diff) {
43
+ RackApiVersioning::Constraint.new( :default_version => 4999,
44
+ :target_version => 5000 ) }
45
+
46
+
47
+ it 'returns true if the default and target version are the same' do
48
+ api_versions_same.matches?(request_with_no_header).should be_true
49
+ end
50
+
51
+ it 'returns false if the default and target versions are different' do
52
+ api_versions_diff.matches?(request_with_no_header).should be_false
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe RackApiVersioning::Middleware do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ Rack::Builder.new do
9
+
10
+ map '/' do
11
+ use RackApiVersioning::Middleware,
12
+ :app_name => "awesome-app",
13
+ :default_version => 1,
14
+ :target_version => 2
15
+
16
+ run lambda { |env|
17
+ [200, {"Content-Type" => "text/html"}, "Testing RackApiVersioning"]
18
+ }
19
+ end
20
+
21
+ map "/new-improved-api" do
22
+ use RackApiVersioning::Middleware,
23
+ :app_name => "awesome-app",
24
+ :default_version => 2,
25
+ :target_version => 3
26
+
27
+ run lambda { |env|
28
+ [200, {"Content-Type" => "text/html"}, "Testing RackApiVersioning"]
29
+ }
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ context "when the request hits '/'" do
36
+
37
+ context "when the API versioning header is included" do
38
+
39
+ it 'responds successfully to the request' do
40
+ get "/", {}, {'Accept' => "application/vnd.awesome-app-v2+json"}
41
+ last_response.ok?.should be_true
42
+ end
43
+
44
+ end
45
+
46
+ context "when the API versioning header is excluded" do
47
+
48
+ it 'responds with a 400 status code' do
49
+ get "/", {}, {}
50
+ last_response.status.should == 400
51
+ end
52
+
53
+ it 'responds with a plain text error message' do
54
+ get "/", {}, {}
55
+ last_response.body.should == "The request did not contain an appropriate API version header."
56
+ end
57
+
58
+ end
59
+
60
+ context "when the API versioning header is wrong" do
61
+
62
+ it 'responds with a 400 status code' do
63
+ get "/", {}, {'Accept' => "application/vnd.bad-header-v1+xml"}
64
+ last_response.status.should == 400
65
+ end
66
+
67
+ it 'responds with a plain text error message' do
68
+ get "/", {}, {'Accept' => "application/vnd.bad-header-v1+xml"}
69
+ last_response.body.should == "The request did not contain an appropriate API version header."
70
+ end
71
+
72
+ end
73
+
74
+ context "when the target version is wrong" do
75
+
76
+ it 'responds with a 400 status code' do
77
+ get "/", {}, {'Accept' => "application/vnd.awesome-app-v4+json"}
78
+ last_response.status.should == 400
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ context "when the request hits '/new-improved-api" do
86
+
87
+ context "when the API versioning header is included" do
88
+
89
+ it 'responds successfully to the request' do
90
+ get "/new-improved-api", {}, {'Accept' => "application/vnd.awesome-app-v3+json"}
91
+ last_response.ok?.should be_true
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'rake'
4
+ require "rack/test"
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/rack-api-versioning")
7
+
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-api-versioning
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Zalabak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.2.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.12.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.12.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.6.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.6.2
78
+ description: Version your Rack based API's using headers either through middleware
79
+ or routing constraints
80
+ email:
81
+ - techwhizbang@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - .rvmrc
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/rack-api-versioning.rb
94
+ - lib/rack_api_versioning/constraint.rb
95
+ - lib/rack_api_versioning/middleware.rb
96
+ - lib/rack_api_versioning/version.rb
97
+ - rack_api_versioning.gemspec
98
+ - spec/constraint_spec.rb
99
+ - spec/middleware_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/techwhizbang/rack-api-versioning
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: 423395989637865473
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ segments:
123
+ - 0
124
+ hash: 423395989637865473
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.25
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Version your Rack based API's using headers either through middleware or
131
+ routing constraints
132
+ test_files:
133
+ - spec/constraint_spec.rb
134
+ - spec/middleware_spec.rb
135
+ - spec/spec_helper.rb