versioner_rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f89b1f3de9544fde3fe300ce5abae24998de6608
4
+ data.tar.gz: 7144e9065e745853bbf2efb79bc425a1177c7d8a
5
+ SHA512:
6
+ metadata.gz: 220c951c5678f4de58f1666212bfeaceaa0073df372e685fdd00233adb36fe786c50a1c1e6491ce9b8f71c670749acafeb0665ce7f78295be464ffc8bfd4a9c8
7
+ data.tar.gz: d170343b9a2cd7a7efcbde19bda8decb6c47aca48cb831d07c247b277a491865dc63ba8decd1071c019eaf04d231269b4c2e52ca200d5605162b926001598405
@@ -0,0 +1,10 @@
1
+ /.idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 VTS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # versioner-rails
2
+
3
+ API versioning that promotes convention over configuration.
4
+
5
+ The premise of this gem is that consumers of your API need versioning and different shapes of your resources. Without proper thought into versioning and shaping, your codebase can quickly resolve into a redundant and confusing state. This gem tries to solve that problem by allowing the API owner to use simple conventions -- Accept headers and ActiveModelSerializer namespacing -- to achieve controller reuse by controllers delegating resource versioning and shaping to the serializer level.
6
+
7
+
8
+ ## example
9
+
10
+
11
+
12
+ Let's say we have a resource of type `Foo` and `foos_controller.rb` that includes our gem and has a `show` action:
13
+
14
+ ``` Ruby
15
+ class FoosController < ActionController::Base
16
+ versioner Serializers::Foo
17
+
18
+ def show
19
+ render json: Foo.new(first_name: 'Shawn'), serializer: versioner_serializer
20
+ end
21
+
22
+ end
23
+ ```
24
+
25
+ A client on v1 would like a list of `foos` in short form:
26
+
27
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=1 shape=short'`
28
+
29
+ A client on v2 would like a list of `foos` in short form:
30
+
31
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=2 shape=short'`
32
+
33
+ A client on v1 would like a list of `foos` in full form:
34
+
35
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=1 shape=full'`
36
+
37
+ A client on v2 would like a list of `foos` in default form:
38
+
39
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=2`
40
+
41
+ Assuming we have the following ActiveModelSerializer directory structure, we wouldn't have to change the above controller at all to fulfill these requests:
42
+ ```
43
+ app
44
+ serializers
45
+ foo
46
+ v1
47
+ foo_short_serializer.rb
48
+ foo_full_serializer.rb
49
+ v2
50
+ foo_full_serializer.rb
51
+ foo_serializer.rb
52
+ ```
53
+
54
+ To specify that the versioner should be applied to or excluded from given controller actions, use the :only and :except parameters.
55
+ ```
56
+ versioner Serializers::Foo, :except => [:index, :show]
57
+ versioner Serializers::Foo, :only => :delete
58
+ ```
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.3
@@ -0,0 +1,37 @@
1
+ require 'versioner_rails/version'
2
+
3
+ module VersionerRails
4
+
5
+ def versioner mod, **opts
6
+ normalize_options(opts)
7
+ self.prepend_before_filter opts do
8
+ if request.accept
9
+ version = request.accept[/version\s?=\s?(\d+)/, 1]
10
+ shape = request.accept[/shape\s?=\s?(\w+)/, 1]
11
+ mod_version = mod.const_get("V#{version}")
12
+ resource_name = mod.name.split('::').last
13
+ if shape
14
+ @versioner_serializer = mod_version.const_get("#{resource_name}#{shape.camelize}Serializer")
15
+ else
16
+ @versioner_serializer = mod_version.const_get("#{resource_name}Serializer")
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def normalize_options(opts)
25
+ opts.keep_if do |k, _v|
26
+ [:except, :only].include?(k)
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ if defined? ActionController::Base
33
+ ActionController::Base.class_eval do
34
+ attr_accessor :versioner_serializer
35
+ extend VersionerRails
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module VersionerRails
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'versioner_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'versioner_rails'
8
+ spec.version = VersionerRails::VERSION
9
+ spec.authors = ['Shawn O\'Mara']
10
+ spec.email = ['shaw3257@gmail.com']
11
+
12
+ spec.summary = 'API versioning that promotes convention over configuration'
13
+
14
+ spec.description = 'The premise of this gem is that consumers of your API need versioning
15
+ and different shapes of your resources. Without proper thought into versioning
16
+ and shaping, your codebase can quickly resolve into a redundant and confusing state.
17
+ This gem tries to solve that problem by allowing the API owner to use simple
18
+ conventions -- Accept headers and ActiveModelSerializer namespacing -- to achieve
19
+ controller reuse by controllers delegating resource versioning and shaping
20
+ to the serializer level.'
21
+
22
+ spec.homepage = 'https://github.com/viewthespace/versioner-rails'
23
+ spec.license = 'MIT'
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'actionpack'
29
+ spec.add_dependency 'activesupport'
30
+ spec.add_dependency 'active_model_serializers'
31
+
32
+ spec.add_development_dependency 'rspec-rails'
33
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: versioner_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shawn O'Mara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: active_model_serializers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: "The premise of this gem is that consumers of your API need versioning
70
+ \n and different shapes of your resources. Without proper
71
+ thought into versioning \n and shaping, your codebase can
72
+ quickly resolve into a redundant and confusing state. \n This
73
+ gem tries to solve that problem by allowing the API owner to use simple \n conventions
74
+ -- Accept headers and ActiveModelSerializer namespacing -- to achieve \n controller
75
+ reuse by controllers delegating resource versioning and shaping \n to
76
+ the serializer level."
77
+ email:
78
+ - shaw3257@gmail.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - Gemfile
85
+ - LICENSE.md
86
+ - README.md
87
+ - circle.yml
88
+ - lib/versioner_rails.rb
89
+ - lib/versioner_rails/version.rb
90
+ - versioner_rails.gemspec
91
+ homepage: https://github.com/viewthespace/versioner-rails
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: API versioning that promotes convention over configuration
115
+ test_files: []