jsonapi_swagger_helpers 0.1.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b1b29089e312228c47932a15abdab6101c4333f0
4
+ data.tar.gz: b796543726b7546998ce7506914acb976ac7b4b6
5
+ SHA512:
6
+ metadata.gz: f9ea55809a9cda5a9344aba41453daed0d01e0c91a24947a7e19e320612d6d784631d7e6592d007a67ac48c7c1307431b11e97d7e5d6ee1b5daf931f6fb37896
7
+ data.tar.gz: 2ea9c3543e2282678de72f05f9852e74ea8d09b90aee1fd60c6475aea9b768f210d5b8fd0b3ab2b97928f80d11da51200b5d5dadea8a3a859180566fc69bbfbe
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/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jsonapi_swagger_helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Lee Richmond
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,3 @@
1
+ # JsonapiSwaggerHelpers
2
+
3
+ [![Build Status](https://travis-ci.org/jsonapi-suite/jsonapi_swagger_helpers.svg?branch=master)](https://travis-ci.org/jsonapi-suite/jsonapi_swagger_helpers)
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/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonapi_swagger_helpers"
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 "irb"
14
+ IRB.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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsonapi_swagger_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonapi_swagger_helpers"
8
+ spec.version = JsonapiSwaggerHelpers::VERSION
9
+ spec.authors = ["Lee Richmond"]
10
+ spec.email = ["lrichmond1@bloomberg.net"]
11
+
12
+ spec.summary = %q{Swagger helpers for jsonapi.org-compatible APIs}
13
+ spec.description = %q{Requires jsonapi_suite of libraries}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'swagger-blocks', '~> 1.3'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,122 @@
1
+ require 'swagger/blocks'
2
+ require "jsonapi_swagger_helpers/version"
3
+ require "jsonapi_swagger_helpers/schema_helpers"
4
+ require "jsonapi_swagger_helpers/strong_resource_mixin"
5
+ require "jsonapi_swagger_helpers/resource_mixin"
6
+ require "jsonapi_swagger_helpers/docs_controller_mixin"
7
+
8
+ module JsonapiSwaggerHelpers
9
+ def self.prepended(klass)
10
+ klass.send(:include, StrongResourceMixin)
11
+ end
12
+
13
+ def self.docs_controller
14
+ @docs_controller ||= ::DocsController
15
+ end
16
+
17
+ def self.docs_controller=(controller)
18
+ @docs_controller = controller
19
+ end
20
+
21
+ def jsonapi_link
22
+ "<br/><p><a href='http://jsonapi.org'>JSONAPI-compliant</a> endpoint.</p><br />"
23
+ end
24
+
25
+ def validation_messages(messages)
26
+ string = "<p><b>Validations:</b><ul>"
27
+ messages.each do |message|
28
+ string << "<li>#{message}</li>"
29
+ end
30
+ string << "</ul></p>"
31
+ end
32
+
33
+ def jsonapi_index(controller)
34
+ jsonapi_includes(controller, :index)
35
+ jsonapi_filters(controller)
36
+ jsonapi_pagination
37
+ jsonapi_sorting
38
+ end
39
+
40
+ def jsonapi_show(controller)
41
+ id_in_url
42
+ jsonapi_includes(controller, :show)
43
+ end
44
+
45
+ def id_in_url
46
+ parameter do
47
+ key :name, :id
48
+ key :in, :path
49
+ key :type, :string
50
+ key :required, true
51
+ key :description, 'record id'
52
+ end
53
+ end
54
+
55
+ def jsonapi_filters(controller)
56
+ filter_names = []
57
+ controller._jsonapi_compliable.filters.each_pair do |name, opts|
58
+ filter_names << name
59
+ end
60
+
61
+ filter_names.each do |filter_name|
62
+ parameter do
63
+ key :name, "filter[#{filter_name}]"
64
+ key :in, :query
65
+ key :type, :string
66
+ key :required, false
67
+ key :description, "<a href='http://jsonapi.org/format/#fetching-filtering'>JSONAPI filter</a>"
68
+
69
+ items do
70
+ key :model, :string
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def jsonapi_pagination
77
+ parameter do
78
+ key :name, "page[size]"
79
+ key :in, :query
80
+ key :type, :string
81
+ key :required, false
82
+ key :description, "<a href='http://jsonapi.org/format/#fetching-pagination'>JSONAPI page size</a>"
83
+ end
84
+
85
+ parameter do
86
+ key :name, "page[number]"
87
+ key :in, :query
88
+ key :type, :string
89
+ key :required, false
90
+ key :description, "<a href='http://jsonapi.org/format/#fetching-pagination'>JSONAPI page number</a>"
91
+ end
92
+ end
93
+
94
+ def jsonapi_sorting
95
+ parameter do
96
+ key :name, :sort
97
+ key :in, :query
98
+ key :type, :string
99
+ key :required, false
100
+ key :description, "<a href='http://jsonapi.org/format/#fetching-sorting'>JSONAPI sort</a>"
101
+ end
102
+ end
103
+
104
+ def jsonapi_includes(controller, action)
105
+ includes = controller._jsonapi_compliable.sideloads[:whitelist]
106
+
107
+ if includes
108
+ directive = includes[action]
109
+ includes = directive.to_string
110
+
111
+ parameter do
112
+ key :name, :include
113
+ key :in, :query
114
+ key :type, :string
115
+ key :required, false
116
+ key :description, "<a href='http://jsonapi.org/format/#fetching-includes'>JSONAPI includes</a>: \"#{includes}\""
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ Swagger::Blocks::OperationNode.prepend(JsonapiSwaggerHelpers)
@@ -0,0 +1,13 @@
1
+ module JsonapiSwaggerHelpers
2
+ module DocsControllerMixin
3
+ def self.included(klass)
4
+ klass.send(:include, Swagger::Blocks)
5
+ klass.extend(ResourceMixin)
6
+ end
7
+
8
+ def index
9
+ klasses = [self.class, JsonapiSwaggerHelpers::StrongResourceMixin::Schemas]
10
+ render json: Swagger::Blocks.build_root_json(klasses)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,76 @@
1
+ module JsonapiSwaggerHelpers
2
+ module ResourceMixin
3
+
4
+ def jsonapi_resource(base_path, tags: [], descriptions: {}, only: [], except: [])
5
+ actions = [:index, :show, :create, :update, :destroy]
6
+
7
+ unless only.empty?
8
+ actions.select! { |a| only.include?(a) }
9
+ end
10
+
11
+ unless except.empty?
12
+ actions.reject! { |a| except.include?(a) }
13
+ end
14
+
15
+ prefix = @swagger_root_node.data[:basePath]
16
+ full_path = [prefix, base_path].join('/').gsub('//', '/')
17
+ controller = controller_for(full_path)
18
+
19
+ if [:create, :index].any? { |a| actions.include?(a) }
20
+ swagger_path base_path do
21
+ if actions.include?(:index)
22
+ operation :get do
23
+ key :tags, tags
24
+ key :description, descriptions[:index]
25
+ jsonapi_index(controller)
26
+ end
27
+ end
28
+
29
+ if actions.include?(:create)
30
+ operation :post do
31
+ key :tags, tags
32
+ key :description, descriptions[:create]
33
+ strong_resource(controller, :create)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ if [:show, :update, :destroy].any? { |a| actions.include?(a) }
40
+ swagger_path "#{base_path}/{id}" do
41
+ if actions.include?(:show)
42
+ operation :get do
43
+ key :tags, tags
44
+ key :description, descriptions[:show]
45
+ jsonapi_show(controller)
46
+ end
47
+ end
48
+
49
+ if actions.include?(:update)
50
+ operation :put do
51
+ key :tags, tags
52
+ key :description, descriptions[:update]
53
+ id_in_url
54
+ strong_resource(controller, :update)
55
+ end
56
+ end
57
+
58
+ if actions.include?(:destroy)
59
+ operation :delete do
60
+ key :tags, tags
61
+ key :description, descriptions[:destroy]
62
+ id_in_url
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def controller_for(path)
70
+ path = path.sub('{id}', '1')
71
+ route = Rails.application.routes.recognize_path(path)
72
+ "#{route[:controller]}_controller".classify.constantize
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,78 @@
1
+ module JsonapiSwaggerHelpers
2
+ class SchemaHelpers
3
+ def self.attributes_schema
4
+ lambda do |attributes|
5
+ property :attributes do
6
+ key :type, :object
7
+
8
+ attributes.each_pair do |name, attr_type|
9
+ property name do
10
+ key :name, name
11
+ key :type, attr_type
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.relationships_schema
19
+ lambda do |relationships|
20
+ property :relationships do
21
+ key :type, :object
22
+
23
+ relationships.each_pair do |relation_name, opts|
24
+ property relation_name do
25
+ property :data do
26
+ if opts[:array]
27
+ key :type, :array
28
+ items do
29
+ if opts[:id]
30
+ property :id do
31
+ key :type, :string
32
+ end
33
+ end
34
+
35
+ property :type do
36
+ key :type, :string
37
+ key :required, true
38
+ key :enum, [opts[:jsonapi_type]]
39
+ end
40
+
41
+ if opts[:attributes]
42
+ instance_exec(opts[:attributes], &SchemaHelpers.attributes_schema)
43
+ end
44
+
45
+ if opts[:relationships]
46
+ instance_exec(opts[:relationships], &SchemaHelpers.relationships_schema)
47
+ end
48
+ end
49
+ else
50
+ if opts[:id]
51
+ property :id do
52
+ key :type, :string
53
+ end
54
+ end
55
+
56
+ property :type do
57
+ key :type, :string
58
+ key :required, true
59
+ key :enum, [opts[:jsonapi_type]]
60
+ end
61
+
62
+ if opts[:attributes]
63
+ instance_exec(opts[:attributes], &SchemaHelpers.attributes_schema)
64
+ end
65
+
66
+ if opts[:relationships]
67
+ instance_exec(opts[:relationships], &SchemaHelpers.relationships_schema)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,120 @@
1
+ module JsonapiSwaggerHelpers
2
+ module StrongResourceMixin
3
+
4
+ class Schemas
5
+ include Swagger::Blocks
6
+
7
+ def self.schema(name, &blk)
8
+ swagger_schema(name, &blk)
9
+ end
10
+ end
11
+
12
+ def strong_resource(controller, action)
13
+ update = action == :update
14
+ resource = controller._strong_resources[action]
15
+ opts = { jsonapi_type: resource.jsonapi_type }
16
+
17
+ if (attributes = strong_resource_attributes(resource, update)).present?
18
+ opts[:attributes] = attributes
19
+ end
20
+
21
+ if (relationships = strong_resource_relationships(resource, update)).present?
22
+ opts[:relationships] = relationships
23
+ end
24
+
25
+ jsonapi_payload(SecureRandom.uuid.to_sym, opts)
26
+ end
27
+
28
+ def strong_resource_attributes(resource, is_update = false)
29
+ attributes = {}
30
+ resource.attributes.each_pair do |name, opts|
31
+ type = StrongResources.config.strong_params[opts[:type]][:swagger]
32
+ attributes[name] = type
33
+
34
+ if is_update
35
+ if resource.destroy?
36
+ attributes[:_destroy] = :boolean
37
+ end
38
+
39
+ if resource.delete?
40
+ attributes[:_delete] = :boolean
41
+ end
42
+ end
43
+ end
44
+
45
+ attributes = attributes.slice(*resource.only) if !!resource.only
46
+ attributes = attributes.except(*resource.except) if !!resource.except
47
+ attributes
48
+ end
49
+
50
+ def strong_resource_relationships(resource, is_update = false)
51
+ {}.tap do |relations|
52
+ resource.relations.each_pair do |relation_name, opts|
53
+ resource = opts[:resource]
54
+
55
+ payload = {
56
+ jsonapi_type: resource.jsonapi_type,
57
+ id: true,
58
+ array: resource.has_many?
59
+ }
60
+
61
+ if (attributes = strong_resource_attributes(resource, is_update)).present?
62
+ payload[:attributes] = attributes
63
+ end
64
+
65
+ if (relationships = strong_resource_relationships(resource, is_update)).present?
66
+ payload[:relationships] = relationships
67
+ end
68
+
69
+ relations[relation_name] = payload
70
+ end
71
+ end
72
+ end
73
+
74
+ def jsonapi_payload(schema_name, payload)
75
+ jsonapi_input_schema(schema_name, payload)
76
+
77
+ parameter do
78
+ key :name, :payload
79
+ key :in, :body
80
+
81
+ schema do
82
+ key :'$ref', schema_name
83
+ end
84
+ end
85
+ end
86
+
87
+ def jsonapi_input_schema(schema_name,
88
+ id: false,
89
+ jsonapi_type:,
90
+ attributes: nil,
91
+ relationships: nil)
92
+ Schemas.schema(schema_name) do
93
+ property :data do
94
+ key :type, :object
95
+
96
+ property :type do
97
+ key :type, :string
98
+ key :required, true
99
+ key :enum, [jsonapi_type]
100
+ end
101
+
102
+ if id
103
+ property :id do
104
+ key :type, :string
105
+ end
106
+ end
107
+
108
+ if attributes
109
+ instance_exec(attributes, &SchemaHelpers.attributes_schema)
110
+ end
111
+
112
+ if relationships
113
+ instance_exec(relationships, &SchemaHelpers.relationships_schema)
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module JsonapiSwaggerHelpers
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonapi_swagger_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Lee Richmond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: swagger-blocks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Requires jsonapi_suite of libraries
70
+ email:
71
+ - lrichmond1@bloomberg.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - jsonapi_swagger_helpers.gemspec
86
+ - lib/jsonapi_swagger_helpers.rb
87
+ - lib/jsonapi_swagger_helpers/docs_controller_mixin.rb
88
+ - lib/jsonapi_swagger_helpers/resource_mixin.rb
89
+ - lib/jsonapi_swagger_helpers/schema_helpers.rb
90
+ - lib/jsonapi_swagger_helpers/strong_resource_mixin.rb
91
+ - lib/jsonapi_swagger_helpers/version.rb
92
+ homepage:
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.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Swagger helpers for jsonapi.org-compatible APIs
116
+ test_files: []
117
+ has_rdoc: