shapeable 0.2.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: 249116685b8033f80052635f2483dc5a35046537
4
+ data.tar.gz: 6d3212f5162741786d818022b40669ad61c6b5e1
5
+ SHA512:
6
+ metadata.gz: 01ccd24321df0ddbb2ae20344f34f35c02cff8d8776dd93cbcd699f06795fe98c4a5f4dc030f58f5e313d6b77d932c4680440979ca76c43ca69527afa30f86af
7
+ data.tar.gz: 81a86272c1e050c4d0f598f5b960ef0fd23f7c9ffd5e2c694d26d14f2909bc223bc3e33f3520ff2ae433e62f047bc881899621794d9f507ad599c6042062fe48
@@ -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,63 @@
1
+ # Shapeable
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
+ Let's say we have a resource of type `Foo` and `foos_controller.rb` that includes our gem and has a `show` action:
12
+
13
+ ``` Ruby
14
+ class FoosController < ActionController::Base
15
+ acts_as_shapeable(path: Serializers::Foo)
16
+
17
+ def show
18
+ render json: Foo.new(first_name: 'Shawn'), serializer: shape(default_version: 1, default_shape: 'short')
19
+ end
20
+
21
+ end
22
+ ```
23
+
24
+ A client on v1 would like a list of `foos` in short form:
25
+
26
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=1 shape=short'`
27
+
28
+ A client on v2 would like a list of `foos` in short form:
29
+
30
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=2 shape=short'`
31
+
32
+ A client on v1 would like a list of `foos` in full form:
33
+
34
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript; version=1 shape=full'`
35
+
36
+ A client on v2 would like a list of `foos` in default form, in this case short:
37
+
38
+ `curl http://localhost:3000/foos -H 'Accept: application/javascript;`
39
+
40
+ Assuming we have the following ActiveModelSerializer directory structure, we wouldn't have to change the above controller at all to fulfill these requests:
41
+
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
+
55
+ The `shape` method returns the serializer constant.
56
+
57
+ Both `acts_as_shapeable` and `shape` accept the following arguments:
58
+
59
+ * `path`: The top level module where the serializers are defined
60
+ * `default_version`: The default version in cases where the header is not specified
61
+ * `default_shape`: The deafault shape in cases where the shape is not specified
62
+
63
+ All three options must be defined either on `acts_as_shapeable` or `shape`. The options defined on `shape` have greater precedence than those on `acts_as_shapeable`.
@@ -0,0 +1,3 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.2.3
@@ -0,0 +1,48 @@
1
+ require 'shapeable/version'
2
+
3
+ module Shapeable
4
+
5
+ def acts_as_shapeable(**opts)
6
+
7
+ normalize_options(opts)
8
+ acts_as_shapeable_opts = opts || {}
9
+
10
+ define_method(:shape) do |opts|
11
+ opts = acts_as_shapeable_opts.merge(opts)
12
+ default_shape = opts[:default_shape]
13
+ default_version = opts[:default_version]
14
+ path = opts[:path]
15
+ raise ArgumentError, "specify a default shape" unless default_shape
16
+ raise ArgumentError, "specify a default version" unless default_version
17
+ raise ArgumentError, "specify a path" unless path
18
+ resource = path.name.split('::').last.constantize
19
+ if request.accept
20
+ version = request.accept[/version\s?=\s?(\d+)/, 1].to_i.presence || default_version
21
+ shape = request.accept[/shape\s?=\s?(\w+)/, 1] || default_shape
22
+ begin
23
+ serializer = path.const_get("V#{version}").const_get("#{resource}#{shape.camelize}Serializer")
24
+ rescue NameError
25
+ raise InvalidShapeError, 'invalid shape'
26
+ end
27
+ end
28
+ serializer
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def normalize_options(opts)
35
+ opts.keep_if do |k, _v|
36
+ [:path, :default_shape, :default_version].include?(k)
37
+ end
38
+ end
39
+
40
+ class InvalidShapeError < NameError; end
41
+
42
+ end
43
+
44
+ if defined? ActionController::Base
45
+ ActionController::Base.class_eval do
46
+ extend Shapeable
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Shapeable
2
+ VERSION = '0.2.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 'shapeable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'shapeable'
8
+ spec.version = Shapeable::VERSION
9
+ spec.authors = ['Shawn O\'Mara', 'Dean Nasseri']
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/shapeable'
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,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shapeable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Shawn O'Mara
8
+ - Dean Nasseri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: active_model_serializers
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec-rails
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: |-
71
+ The premise of this gem is that consumers of your API need versioning
72
+ and different shapes of your resources. Without proper thought into versioning
73
+ and shaping, your codebase can quickly resolve into a redundant and confusing state.
74
+ This gem tries to solve that problem by allowing the API owner to use simple
75
+ conventions -- Accept headers and ActiveModelSerializer namespacing -- to achieve
76
+ controller reuse by controllers delegating resource versioning and shaping
77
+ to the serializer level.
78
+ email:
79
+ - shaw3257@gmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - Gemfile
86
+ - LICENSE.md
87
+ - README.md
88
+ - circle.yml
89
+ - lib/shapeable.rb
90
+ - lib/shapeable/version.rb
91
+ - shapeable.gemspec
92
+ homepage: https://github.com/viewthespace/shapeable
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: API versioning that promotes convention over configuration
116
+ test_files: []