grape-swagger-representable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 302c6de45081fa17e58c7c98afc4ce8e4a94da9e
4
+ data.tar.gz: 62c98b4e833dd0fe23ac3336b2d67283a3937323
5
+ SHA512:
6
+ metadata.gz: 72522b40d6499e2bad6cbcd71109532b778e4b9ee24f919c87f65ebc46c4ce447b06ea71deb915967d3d9618e41844e2f9933d7496685c12e158c250b3c791d2
7
+ data.tar.gz: a353a4b2c38cecad85fb66ae60a5c3b6bd583619d1da2ed18af87d14601feb3f77d8215d75a87b071482e33b8b02a8d89924fd7ab01d0d1d5cd6445fb7173881
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+
5
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,38 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-05-08 06:18:31 +0300 using RuboCop version 0.39.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Metrics/AbcSize:
11
+ Max: 30
12
+
13
+ # Offense count: 2
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 11
16
+
17
+ # Offense count: 65
18
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
19
+ # URISchemes: http, https
20
+ Metrics/LineLength:
21
+ Max: 152
22
+
23
+ # Offense count: 2
24
+ # Configuration parameters: CountComments.
25
+ Metrics/MethodLength:
26
+ Max: 36
27
+
28
+ # Offense count: 2
29
+ Metrics/PerceivedComplexity:
30
+ Max: 14
31
+
32
+ # Offense count: 2
33
+ Style/Documentation:
34
+ Exclude:
35
+ - 'spec/**/*'
36
+ - 'test/**/*'
37
+ - 'lib/grape-swagger/representable.rb'
38
+ - 'lib/grape-swagger/representable/parser.rb'
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ matrix:
6
+ include:
7
+ - rvm: 2.3.0
8
+ - rvm: 2.2
9
+ - rvm: 2.1
10
+ - rvm: ruby-head
11
+ - rvm: jruby-head
12
+ - rvm: jruby-19mode
13
+ - rvm: rbx-2
14
+ allow_failures:
15
+ - rvm: ruby-head
16
+ - rvm: jruby-head
17
+ before_install: gem install bundler -v 1.12.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grape-swagger-representable.gemspec
4
+ gemspec
5
+
6
+ gem 'grape-swagger', github: 'Bugagazavr/grape-swagger', branch: 'representers'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Kirill Zaitsev
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # GrapeSwagger::Representable [![Build Status](https://travis-ci.org/Bugagazavr/grape-swagger-representable.svg)](https://travis-ci.org/Bugagazavr/grape-swagger-representable)
2
+
3
+ A simple grape-swagger adapter to allow parse representers as response model
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'grape-swagger-representable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install grape-swagger-representable
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/pry` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/grape-swagger-representable.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
34
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/pry ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'grape-swagger/representable'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'pry'
14
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grape-swagger/representable/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'grape-swagger-representable'
8
+ s.version = GrapeSwagger::Representable::VERSION
9
+ s.authors = ['Kirill Zaitsev']
10
+ s.email = ['kirik910@gmail.com']
11
+
12
+ s.summary = 'Grape swagger adapter to support representable object parsing'
13
+ s.homepage = 'https://github.com/Bugagazavr/grape-swagger-representable'
14
+ s.license = 'MIT'
15
+
16
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ s.bindir = 'exe'
18
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_runtime_dependency 'grape-swagger', '>= 0.20.2'
22
+ s.add_runtime_dependency 'representable'
23
+
24
+ s.add_development_dependency 'bundler', '~> 1.12'
25
+ s.add_development_dependency 'rake', '~> 10.0'
26
+ s.add_development_dependency 'rspec', '~> 3.0'
27
+ s.add_development_dependency 'rack-test'
28
+ s.add_development_dependency 'rack-cors'
29
+ s.add_development_dependency 'rubocop'
30
+ s.add_development_dependency 'redcarpet' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
31
+ s.add_development_dependency 'rouge' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
32
+ s.add_development_dependency 'pry' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
33
+ s.add_development_dependency 'pry-byebug' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
34
+ end
@@ -0,0 +1,14 @@
1
+ require 'grape-swagger'
2
+ require 'representable'
3
+
4
+ require 'grape-swagger/representable/version'
5
+ require 'grape-swagger/representable/parser'
6
+
7
+ module Grape
8
+ module Swagger
9
+ module Representable
10
+ end
11
+ end
12
+ end
13
+
14
+ GrapeSwagger.model_parsers.register(::GrapeSwagger::Representable::Parser, ::Representable::Decorator)
@@ -0,0 +1,103 @@
1
+ module GrapeSwagger
2
+ module Representable
3
+ class Parser
4
+ attr_reader :model
5
+ attr_reader :endpoint
6
+
7
+ def initialize(model, endpoint)
8
+ @model = model
9
+ @endpoint = endpoint
10
+ end
11
+
12
+ def call
13
+ parse_representer(model)
14
+ end
15
+
16
+ private
17
+
18
+ def parse_representer_property(property)
19
+ is_a_collection = property.is_a?(::Representable::Hash::Binding::Collection)
20
+ documentation = property[:documentation] ? property[:documentation].dup : {}
21
+
22
+ if property[:decorator] && property[:nested]
23
+ representer_mapping(property[:decorator], documentation, is_a_collection, false, property[:nested])
24
+ elsif property[:decorator]
25
+ representer_mapping(property[:decorator], documentation, is_a_collection, true)
26
+ elsif property[:nested]
27
+ representer_mapping(property[:nested], documentation, is_a_collection)
28
+ else
29
+ memo = {
30
+ description: documentation[:desc] || ''
31
+ }
32
+
33
+ data_type = GrapeSwagger::DocMethods::DataType.call(documentation[:type])
34
+ if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
35
+ data = GrapeSwagger::DocMethods::DataType.mapping(data_type)
36
+ memo[:type] = data.first
37
+ memo[:format] = data.last
38
+ else
39
+ memo[:type] = data_type
40
+ end
41
+
42
+ memo[:enum] = documentation[:values] if documentation[:values].is_a?(Array)
43
+
44
+ if is_a_collection || documentation[:is_array]
45
+ memo = {
46
+ type: :array,
47
+ items: memo
48
+ }
49
+ end
50
+
51
+ memo
52
+ end
53
+ end
54
+
55
+ def representer_mapping(representer, documentation, is_a_collection = false, is_a_decorator = false, nested = nil)
56
+ if nested.nil? && is_a_decorator
57
+ name = endpoint.send(:expose_params_from_model, representer)
58
+
59
+ if is_a_collection || documentation[:is_array]
60
+ {
61
+ type: :array,
62
+ items: {
63
+ '$ref' => "#/definitions/#{name}"
64
+ },
65
+ description: documentation[:desc] || ''
66
+ }
67
+ else
68
+ {
69
+ '$ref' => "#/definitions/#{name}",
70
+ description: documentation[:desc] || ''
71
+ }
72
+ end
73
+ else
74
+ attributes = parse_representer(representer)
75
+ attributes = attributes.deep_merge!(parse_representer(nested)) if nested
76
+
77
+ if is_a_collection
78
+ {
79
+ type: :array,
80
+ items: {
81
+ type: :object,
82
+ properties: attributes
83
+ },
84
+ description: documentation[:desc] || ''
85
+ }
86
+ else
87
+ {
88
+ type: :object,
89
+ properties: attributes,
90
+ description: documentation[:desc] || ''
91
+ }
92
+ end
93
+ end
94
+ end
95
+
96
+ def parse_representer(representer)
97
+ representer.map.each_with_object({}) do |value, property|
98
+ property[value.name] = parse_representer_property(value)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,5 @@
1
+ module GrapeSwagger
2
+ module Representable
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-swagger-representable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Zaitsev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape-swagger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: representable
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-cors
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: redcarpet
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rouge
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry-byebug
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description:
182
+ email:
183
+ - kirik910@gmail.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rspec"
190
+ - ".rubocop.yml"
191
+ - ".rubocop_todo.yml"
192
+ - ".travis.yml"
193
+ - Gemfile
194
+ - LICENSE.txt
195
+ - README.md
196
+ - Rakefile
197
+ - bin/pry
198
+ - bin/setup
199
+ - grape-swagger-representable.gemspec
200
+ - lib/grape-swagger/representable.rb
201
+ - lib/grape-swagger/representable/parser.rb
202
+ - lib/grape-swagger/representable/version.rb
203
+ homepage: https://github.com/Bugagazavr/grape-swagger-representable
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.5.1
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Grape swagger adapter to support representable object parsing
227
+ test_files: []