rocket_shorts 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +2 -0
- data/lib/rocket_shorts.rb +19 -0
- data/lib/rocket_shorts/base.rb +58 -0
- data/lib/rocket_shorts/controller/versioning.rb +38 -0
- data/lib/rocket_shorts/railtie.rb +12 -0
- data/lib/rocket_shorts/routing.rb +32 -0
- data/lib/rocket_shorts/routing_constraints.rb +19 -0
- data/lib/rocket_shorts/version.rb +3 -0
- data/rocket_shorts.gemspec +23 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/.gitkeep +0 -0
- metadata +133 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Justin Smestad
|
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,50 @@
|
|
1
|
+
# RocketShorts
|
2
|
+
|
3
|
+
This is an extension or set of overrides that sit on top of [RocketPants](https://github.com/filtersquad/rocket_pants). This is not an elegant extension by any means, but rather a set of modules that leverage the RocketPants load order to monkey-patch functionality.
|
4
|
+
|
5
|
+
## Why RocketShorts over RocketPants?
|
6
|
+
|
7
|
+
There are a few differentiators: (neither is the 'right way', its personal preference)
|
8
|
+
|
9
|
+
**Note** Support for points 3 and 4 are planned, but not included in the current gem.
|
10
|
+
|
11
|
+
1. Version control is done through the `Accept` HTTP Header instead of a `:version` parameter.
|
12
|
+
2. Support for a default version. Making HTTP testing possible for a single version.
|
13
|
+
3. Pagination should use the `link` HTTP Header and not the content body.
|
14
|
+
4. Clean up the payload body to avoid nesting under a `resource` key.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
gem 'rocket_shorts'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install rocket_shorts
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Remove `rocket_pants` from your Gemfile and replace with `rocket_shorts`.
|
33
|
+
|
34
|
+
Add a `config/inititalizers/rocket_shorts.rb` and specify what header format
|
35
|
+
your API should look for.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# config/initializers/rocket_shorts.rb
|
39
|
+
require 'rocket\_shorts'
|
40
|
+
|
41
|
+
RocketShorts.header_format = /application\/vnd\.yourcompany\.v/
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "rocket_pants"
|
2
|
+
|
3
|
+
require "rocket_shorts/version"
|
4
|
+
|
5
|
+
module RocketShorts
|
6
|
+
autoload :Base, 'rocket_shorts/base'
|
7
|
+
autoload :Routing, 'rocket_shorts/routing'
|
8
|
+
autoload :RoutingConstraints, 'rocket_shorts/routing_constraints'
|
9
|
+
autoload :Versioning, 'rocket_shorts/controller/versioning'
|
10
|
+
|
11
|
+
ActionDispatch::Routing::Mapper.send :include, RocketShorts::Routing
|
12
|
+
|
13
|
+
require 'rocket_shorts/railtie' if defined?(Rails::Railtie)
|
14
|
+
|
15
|
+
|
16
|
+
mattr_accessor :header_format
|
17
|
+
|
18
|
+
# self.header_format = /^application\/vnd\.example\.v/
|
19
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rocket_pants/errors'
|
2
|
+
|
3
|
+
module RocketShorts
|
4
|
+
class Base < ActionController::Metal
|
5
|
+
|
6
|
+
abstract!
|
7
|
+
|
8
|
+
MODULES = [
|
9
|
+
ActionController::HideActions,
|
10
|
+
ActionController::UrlFor,
|
11
|
+
ActionController::Redirecting,
|
12
|
+
ActionController::ConditionalGet,
|
13
|
+
ActionController::RackDelegation,
|
14
|
+
ActionController::RecordIdentifier,
|
15
|
+
ActionController::HttpAuthentication::Basic::ControllerMethods,
|
16
|
+
ActionController::HttpAuthentication::Digest::ControllerMethods,
|
17
|
+
ActionController::HttpAuthentication::Token::ControllerMethods,
|
18
|
+
RocketPants::UrlFor,
|
19
|
+
RocketPants::Respondable,
|
20
|
+
RocketPants::HeaderMetadata,
|
21
|
+
RocketPants::Linking,
|
22
|
+
RocketShorts::Versioning,
|
23
|
+
RocketPants::Instrumentation,
|
24
|
+
RocketPants::Caching,
|
25
|
+
# Include earliest as possible in the request.
|
26
|
+
AbstractController::Callbacks,
|
27
|
+
ActionController::Rescue,
|
28
|
+
RocketPants::ErrorHandling,
|
29
|
+
RocketPants::Rescuable,
|
30
|
+
RocketPants::JSONP
|
31
|
+
# FormatVerification # TODO: Implement Format Verification
|
32
|
+
].compact
|
33
|
+
|
34
|
+
# If possible, include the Rails controller methods in Airbrake to make it useful.
|
35
|
+
begin
|
36
|
+
require 'airbrake'
|
37
|
+
require 'airbrake/rails/controller_methods'
|
38
|
+
MODULES << Airbrake::Rails::ControllerMethods
|
39
|
+
rescue LoadError => e
|
40
|
+
end
|
41
|
+
|
42
|
+
MODULES.each do |mixin|
|
43
|
+
include mixin
|
44
|
+
end
|
45
|
+
|
46
|
+
# Bug fix for rails - include compatibility.
|
47
|
+
config_accessor :protected_instance_variables
|
48
|
+
self.protected_instance_variables = %w(@assigns @performed_redirect @performed_render
|
49
|
+
@variables_added @request_origin @url @parent_controller @action_name
|
50
|
+
@before_filter_chain_aborted @_headers @_params @_response)
|
51
|
+
|
52
|
+
ActiveSupport.run_load_hooks(:rocket_pants, self)
|
53
|
+
|
54
|
+
# Methods for integration purposes.
|
55
|
+
def self.helper_method(*); end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module RocketShorts
|
2
|
+
module Versioning
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :_version_range
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def version(version)
|
12
|
+
version = version..version if version.is_a?(Integer)
|
13
|
+
self._version_range = version
|
14
|
+
before_filter :verify_api_version
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def version
|
22
|
+
if !instance_variable_defined?(:@version)
|
23
|
+
@version = begin
|
24
|
+
header = request.headers['Accept'].match(/#{RocketShorts.header_format}(\d+?)/)
|
25
|
+
version = header[1] rescue nil
|
26
|
+
version.present? && Integer(version)
|
27
|
+
rescue ArgumentError
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
@version
|
32
|
+
end
|
33
|
+
|
34
|
+
def verify_api_version
|
35
|
+
error! :invalid_version if version.present? && !_version_range.include?(version)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module RocketPants
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.rocket_shorts = ActiveSupport::OrderedOptions.new
|
5
|
+
config.rocket_shorts.header_format = /application\/vnd\.example\.v/
|
6
|
+
|
7
|
+
initializer "rocket_shorts.configuration" do |app|
|
8
|
+
rs_config = app.config.rocket_shorts
|
9
|
+
RocketShorts.header_format = rs_config.header_format
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# This modifies RocketPants routing to use
|
2
|
+
# accept headers and namespaces instead of a :version parameter.
|
3
|
+
#
|
4
|
+
# (see RocketPants::Routing)
|
5
|
+
require 'rocket_pants/routing'
|
6
|
+
|
7
|
+
module RocketShorts
|
8
|
+
module Routing
|
9
|
+
|
10
|
+
# Scopes a set of given api routes, allowing for option versions.
|
11
|
+
# @param [Hash] options options to pass through to the route e.g. `:module`.
|
12
|
+
# @option options [Array<Integer>, Integer] :versions the versions to support
|
13
|
+
# @option options [Array<Integer>, Integer] :version the single version to support
|
14
|
+
# @raise [ArgumentError] raised when the version isn't provided.
|
15
|
+
def rocket_pants(options = {}, &blk)
|
16
|
+
versions = (Array(options.delete(:versions)) + Array(options.delete(:version))).flatten.map(&:to_s)
|
17
|
+
versions.each do |version|
|
18
|
+
raise ArgumentError, "Got invalid version: '#{version}'" unless version =~ /\A\d+\Z/
|
19
|
+
end
|
20
|
+
versions_regexp = /(#{versions.uniq.join("|")})/
|
21
|
+
raise ArgumentError, 'please provide atleast one version' if versions.empty?
|
22
|
+
options = options.deep_merge({
|
23
|
+
constraints: RoutingConstraints.new(versions: versions_regexp, default: !!options.delete(:default)),
|
24
|
+
})
|
25
|
+
namespace :api, defaults: {format: 'json'} do
|
26
|
+
scope options, &blk
|
27
|
+
end
|
28
|
+
end
|
29
|
+
alias api rocket_pants
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RocketShorts
|
2
|
+
class RoutingConstraints
|
3
|
+
def initialize(options)
|
4
|
+
@version = options[:version]
|
5
|
+
@versions = options[:versions]
|
6
|
+
@default = options[:default]
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(req)
|
10
|
+
puts 'yoyoyo'
|
11
|
+
req.headers['Accept'].match(/#{RocketShorts.header_format}#{@versions}/)
|
12
|
+
# if @versions.present?
|
13
|
+
# @default ||
|
14
|
+
# else
|
15
|
+
# @default || req.headers['Accept'].match(/#{RocketShorts.header_format}#{@version}/)
|
16
|
+
# end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rocket_shorts/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Justin Smestad"]
|
6
|
+
gem.email = ["justin.smestad@gmail.com"]
|
7
|
+
gem.description = %q{Because RocketPants needs another clothing option.}
|
8
|
+
gem.summary = %q{Because RocketPants needs another clothing option.}
|
9
|
+
gem.homepage = "https://github.com/jsmestad/rocket_shorts"
|
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 = "rocket_shorts"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RocketShorts::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'rocket_pants', '~> 1.5.3'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'rspec-rails'
|
22
|
+
gem.add_development_dependency 'yard'
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rocket_shorts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Justin Smestad
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rocket_pants
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.3
|
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.5.3
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
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'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '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: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
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'
|
78
|
+
description: Because RocketPants needs another clothing option.
|
79
|
+
email:
|
80
|
+
- justin.smestad@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/rocket_shorts.rb
|
91
|
+
- lib/rocket_shorts/base.rb
|
92
|
+
- lib/rocket_shorts/controller/versioning.rb
|
93
|
+
- lib/rocket_shorts/railtie.rb
|
94
|
+
- lib/rocket_shorts/routing.rb
|
95
|
+
- lib/rocket_shorts/routing_constraints.rb
|
96
|
+
- lib/rocket_shorts/version.rb
|
97
|
+
- rocket_shorts.gemspec
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/support/.gitkeep
|
100
|
+
homepage: https://github.com/jsmestad/rocket_shorts
|
101
|
+
licenses: []
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: 2663302009248484271
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
hash: 2663302009248484271
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.23
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Because RocketPants needs another clothing option.
|
130
|
+
test_files:
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/support/.gitkeep
|
133
|
+
has_rdoc:
|