jsonapi_swagger_helpers 0.3.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a9dd06a7c8afca90ba9b3b340598a61ee7473d4
4
- data.tar.gz: a4e56e637f81f4da21a6e784c9a4d2dae0b85953
3
+ metadata.gz: 6e5e96630c96f25da98ee7a9ff41551e282da46d
4
+ data.tar.gz: 144a9a7a03e9f768864958a9d0ae29a9410a533b
5
5
  SHA512:
6
- metadata.gz: 002f847d7f43fa3433932b72fab3e1efba37aafa120d2ba7332b9df78c9792a7e9075d5e30a6f56592d69c49a8ab5a213a03ad55463cce5db5040720d2896fd1
7
- data.tar.gz: edc488adacd76b2281ad138410e37e3150fb380b889550ec171f8e0e90207e5dd917c81fb94f40335189d790f7a50f648f9865d517a68969e0f29841699e54de
6
+ metadata.gz: 53242cf8560b129aa133be07917aa1cc38ecd15d7151a69d29e4d1d80118f63e90834c8d00310fd236754beb04088c17674c5c3b336982159d4f625c6bd9b350
7
+ data.tar.gz: 9b07e484928616360f8a1415c9ce7b650edf803628a208ac295d2a847a131241f8bf051217e84f66dfa3bb5e522d3b240c4dcb20059f34342cab9f54ba1a0f05
@@ -1,5 +1,6 @@
1
1
  require 'swagger/blocks'
2
2
  require "jsonapi_spec_helpers"
3
+ require "jsonapi_swagger_helpers/errors"
3
4
  require "jsonapi_swagger_helpers/configuration"
4
5
  require "jsonapi_swagger_helpers/payload_definition"
5
6
  require "jsonapi_swagger_helpers/util"
@@ -1,10 +1,12 @@
1
- class JsonapiSwaggerHelpers::Configuration
2
- def type_mapping
3
- @type_mapping ||= {
4
- string: [String],
5
- integer: [Integer, Bignum],
6
- float: [Float],
7
- boolean: [TrueClass, FalseClass]
8
- }
1
+ module JsonapiSwaggerHelpers
2
+ class Configuration
3
+ def type_mapping
4
+ @type_mapping ||= {
5
+ string: [String],
6
+ integer: [Integer, Bignum],
7
+ float: [Float],
8
+ boolean: [TrueClass, FalseClass]
9
+ }
10
+ end
9
11
  end
10
12
  end
@@ -1,25 +1,27 @@
1
- class JsonapiSwaggerHelpers::CreateAction
2
- include JsonapiSwaggerHelpers::Writeable
1
+ module JsonapiSwaggerHelpers
2
+ class CreateAction
3
+ include JsonapiSwaggerHelpers::Writeable
3
4
 
4
- def action_name
5
- :create
6
- end
5
+ def action_name
6
+ :create
7
+ end
7
8
 
8
- def generate
9
- _self = self
9
+ def generate
10
+ _self = self
10
11
 
11
- define_schema
12
- @node.operation :post do
13
- key :description, _self.description
14
- key :operationId, _self.operation_id
15
- key :tags, _self.all_tags
12
+ define_schema
13
+ @node.operation :post do
14
+ key :description, _self.description
15
+ key :operationId, _self.operation_id
16
+ key :tags, _self.all_tags
16
17
 
17
- parameter do
18
- key :name, :payload
19
- key :in, :body
18
+ parameter do
19
+ key :name, :payload
20
+ key :in, :body
20
21
 
21
- schema do
22
- key :'$ref', :"#{_self.strong_resource.name}_create"
22
+ schema do
23
+ key :'$ref', :"#{_self.strong_resource.name}_create"
24
+ end
23
25
  end
24
26
  end
25
27
  end
@@ -1,19 +1,21 @@
1
- class JsonapiSwaggerHelpers::DestroyAction
2
- include JsonapiSwaggerHelpers::Writeable
1
+ module JsonapiSwaggerHelpers
2
+ class DestroyAction
3
+ include JsonapiSwaggerHelpers::Writeable
3
4
 
4
- def action_name
5
- :destroy
6
- end
5
+ def action_name
6
+ :destroy
7
+ end
7
8
 
8
- def generate
9
- _self = self
9
+ def generate
10
+ _self = self
10
11
 
11
- @node.operation :delete do
12
- key :description, _self.description
13
- key :operationId, _self.operation_id
14
- key :tags, _self.tags
12
+ @node.operation :delete do
13
+ key :description, _self.description
14
+ key :operationId, _self.operation_id
15
+ key :tags, _self.tags
15
16
 
16
- _self.util.id_in_url(self)
17
+ _self.util.id_in_url(self)
18
+ end
17
19
  end
18
20
  end
19
21
  end
@@ -0,0 +1,22 @@
1
+ module JsonapiSwaggerHelpers
2
+ module Errors
3
+ class TypeNotFound < StandardError
4
+ def initialize(payload_name, attribute)
5
+ @payload_name = payload_name
6
+ @attribute = attribute
7
+ end
8
+
9
+ def message
10
+ <<-STR
11
+ Could not find type mapping for payload "#{@payload_name}", key "#{@attribute}".
12
+
13
+ To add a custom mapping:
14
+
15
+ JsonapiSwaggerHelpers.config do |c|
16
+ c.type_mapping[:string] << MyCustomType
17
+ end
18
+ STR
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,42 +1,44 @@
1
- class JsonapiSwaggerHelpers::IndexAction
2
- include JsonapiSwaggerHelpers::Readable
1
+ module JsonapiSwaggerHelpers
2
+ class IndexAction
3
+ include JsonapiSwaggerHelpers::Readable
3
4
 
4
- def action_name
5
- :index
6
- end
5
+ def action_name
6
+ :index
7
+ end
7
8
 
8
- def generate
9
- _self = self
9
+ def generate
10
+ _self = self
10
11
 
11
- @node.operation :get do
12
- key :description, _self.full_description
13
- key :operationId, _self.operation_id
14
- key :tags, _self.all_tags
12
+ @node.operation :get do
13
+ key :description, _self.full_description
14
+ key :operationId, _self.operation_id
15
+ key :tags, _self.all_tags
15
16
 
16
- _self.util.jsonapi_sorting(self)
17
- _self.util.jsonapi_pagination(self)
17
+ _self.util.jsonapi_sorting(self)
18
+ _self.util.jsonapi_pagination(self)
18
19
 
19
- _self.util.each_filter(_self.resource) do |filter_label|
20
- _self.util.jsonapi_filter(self, filter_label)
21
- end
20
+ _self.util.each_filter(_self.resource) do |filter_label|
21
+ _self.util.jsonapi_filter(self, filter_label)
22
+ end
22
23
 
23
- _self.each_stat do |stat_name, calculations|
24
- _self.util.jsonapi_stat(self, stat_name, calculations)
25
- end
24
+ _self.each_stat do |stat_name, calculations|
25
+ _self.util.jsonapi_stat(self, stat_name, calculations)
26
+ end
26
27
 
27
- _self.util.jsonapi_fields(self, _self.jsonapi_type)
28
+ _self.util.jsonapi_fields(self, _self.jsonapi_type)
28
29
 
29
- if _self.has_extra_fields?
30
- _self.util.jsonapi_extra_fields(self, _self.resource)
31
- end
30
+ if _self.has_extra_fields?
31
+ _self.util.jsonapi_extra_fields(self, _self.resource)
32
+ end
32
33
 
33
- if _self.has_sideloads?
34
- _self.util.jsonapi_includes(self)
34
+ if _self.has_sideloads?
35
+ _self.util.jsonapi_includes(self)
35
36
 
36
- _self.each_association do |association_name, association_resource|
37
- _self.util.each_filter(association_resource, association_name) do |filter_label|
38
- _self.util.jsonapi_filter(self, filter_label)
39
- _self.util.jsonapi_fields(self, association_resource.config[:type])
37
+ _self.each_association do |association_name, association_resource|
38
+ _self.util.each_filter(association_resource, association_name) do |filter_label|
39
+ _self.util.jsonapi_filter(self, filter_label)
40
+ _self.util.jsonapi_fields(self, association_resource.config[:type])
41
+ end
40
42
  end
41
43
  end
42
44
  end
@@ -1,55 +1,57 @@
1
- class JsonapiSwaggerHelpers::PayloadDefinition
2
- attr_reader :payload
3
-
4
- # Given a spec payload like:
5
- #
6
- # key(:name, String)
7
- #
8
- # Return the corresponding swagger type, ie :string
9
- # If a key has multiple types, we'll pick the first swagger type that matches:
10
- #
11
- # key(:total, [String, Integer]) => :string
12
- def self.swagger_type_for(payload_name, attribute, type)
13
- types = Array(type)
14
- return :string if types.empty?
15
-
16
- type_mapping.each_pair do |swagger_type, klasses|
17
- if types.any? { |t| klasses.include?(t) }
18
- return swagger_type
1
+ module JsonapiSwaggerHelpers
2
+ class PayloadDefinition
3
+ attr_reader :payload
4
+
5
+ # Given a spec payload like:
6
+ #
7
+ # key(:name, String)
8
+ #
9
+ # Return the corresponding swagger type, ie :string
10
+ # If a key has multiple types, we'll pick the first swagger type that matches:
11
+ #
12
+ # key(:total, [String, Integer]) => :string
13
+ def self.swagger_type_for(payload_name, attribute, type)
14
+ types = Array(type)
15
+ return :string if types.empty?
16
+
17
+ type_mapping.each_pair do |swagger_type, klasses|
18
+ if types.any? { |t| klasses.include?(t) }
19
+ return swagger_type
20
+ end
19
21
  end
20
- end
21
22
 
22
- raise JsonapiSwaggerHelpers::Errors::TypeNotFound
23
- .new(payload_name, attribute)
24
- end
23
+ raise JsonapiSwaggerHelpers::Errors::TypeNotFound
24
+ .new(payload_name, attribute)
25
+ end
25
26
 
26
- def self.type_mapping
27
- JsonapiSwaggerHelpers.config.type_mapping
28
- end
27
+ def self.type_mapping
28
+ JsonapiSwaggerHelpers.config.type_mapping
29
+ end
29
30
 
30
- def initialize(payload)
31
- @payload = payload
32
- end
31
+ def initialize(payload)
32
+ @payload = payload
33
+ end
33
34
 
34
- def context
35
- JsonapiSwaggerHelpers.docs_controller
36
- end
35
+ def context
36
+ JsonapiSwaggerHelpers.docs_controller
37
+ end
37
38
 
38
- def jsonapi_type
39
- payload.type
40
- end
39
+ def jsonapi_type
40
+ payload.type
41
+ end
41
42
 
42
- def generate
43
- _self = self
43
+ def generate
44
+ _self = self
44
45
 
45
- context.send(:swagger_schema, payload.name) do
46
- payload = _self.payload
46
+ context.send(:swagger_schema, payload.name) do
47
+ payload = _self.payload
47
48
 
48
- payload.keys.each_pair do |attribute, config|
49
- property attribute do
50
- type = _self.class.swagger_type_for(payload.name, attribute, config[:type])
51
- key :type, type
52
- key :description, config[:description]
49
+ payload.keys.each_pair do |attribute, config|
50
+ property attribute do
51
+ type = _self.class.swagger_type_for(payload.name, attribute, config[:type])
52
+ key :type, type
53
+ key :description, config[:description]
54
+ end
53
55
  end
54
56
  end
55
57
  end
@@ -1,83 +1,85 @@
1
- module JsonapiSwaggerHelpers::Readable
2
- def self.included(klass)
3
- klass.class_eval do
4
- attr_reader :node,
5
- :controller,
6
- :resource,
7
- :description,
8
- :tags
1
+ module JsonapiSwaggerHelpers
2
+ module Readable
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ attr_reader :node,
6
+ :controller,
7
+ :resource,
8
+ :description,
9
+ :tags
10
+ end
9
11
  end
10
- end
11
12
 
12
- def initialize(node, controller, description: nil, tags: [])
13
- @node = node
14
- @controller = controller
15
- @resource = controller._jsonapi_compliable
16
- @description = description || default_description
17
- @tags = tags
18
- end
13
+ def initialize(node, controller, description: nil, tags: [])
14
+ @node = node
15
+ @controller = controller
16
+ @resource = controller._jsonapi_compliable
17
+ @description = description || default_description
18
+ @tags = tags
19
+ end
19
20
 
20
- def default_description
21
- "#{action_name.capitalize} Action"
22
- end
21
+ def default_description
22
+ "#{action_name.capitalize} Action"
23
+ end
23
24
 
24
- def operation_id
25
- "#{controller.name.gsub('::', '-')}-#{action_name}"
26
- end
25
+ def operation_id
26
+ "#{controller.name.gsub('::', '-')}-#{action_name}"
27
+ end
27
28
 
28
- def util
29
- JsonapiSwaggerHelpers::Util
30
- end
29
+ def util
30
+ JsonapiSwaggerHelpers::Util
31
+ end
31
32
 
32
- def include_directive
33
- util.include_directive_for(controller, action_name)
34
- end
33
+ def include_directive
34
+ util.include_directive_for(controller, action_name)
35
+ end
35
36
 
36
- def has_sideloads?
37
- include_directive.keys.length > 0
38
- end
37
+ def has_sideloads?
38
+ include_directive.keys.length > 0
39
+ end
39
40
 
40
- def has_extra_fields?
41
- resource.config[:extra_fields].keys.length > 1
42
- end
41
+ def has_extra_fields?
42
+ resource.config[:extra_fields].keys.length > 1
43
+ end
43
44
 
44
- def full_description
45
- "#{description}<br /><br />#{util.sideload_label(include_directive)}"
46
- end
45
+ def full_description
46
+ "#{description}<br /><br />#{util.sideload_label(include_directive)}"
47
+ end
47
48
 
48
- def all_tags
49
- tags + payload_tags
50
- end
49
+ def all_tags
50
+ tags + payload_tags
51
+ end
51
52
 
52
- def payload_tags
53
- util.payload_tags_for(resource, include_directive.to_hash)
54
- end
53
+ def payload_tags
54
+ util.payload_tags_for(resource, include_directive.to_hash)
55
+ end
55
56
 
56
- def operation_id
57
- "#{controller.name.gsub('::', '-')}-#{action_name}"
58
- end
57
+ def operation_id
58
+ "#{controller.name.gsub('::', '-')}-#{action_name}"
59
+ end
59
60
 
60
- def each_stat
61
- resource.config[:stats].each_pair do |stat_name, opts|
62
- calculations = opts.calculations.keys - [:keys]
63
- calculations = calculations.join(', ')
61
+ def each_stat
62
+ resource.config[:stats].each_pair do |stat_name, opts|
63
+ calculations = opts.calculations.keys - [:keys]
64
+ calculations = calculations.join(', ')
64
65
 
65
- yield stat_name, calculations
66
+ yield stat_name, calculations
67
+ end
66
68
  end
67
- end
68
69
 
69
- def each_association
70
- resource_map = util.all_resources(resource, include_directive)
71
- resource_map.each_pair do |association_name, association_resource|
72
- yield association_name, association_resource
70
+ def each_association
71
+ resource_map = util.all_resources(resource, include_directive)
72
+ resource_map.each_pair do |association_name, association_resource|
73
+ yield association_name, association_resource
74
+ end
73
75
  end
74
- end
75
76
 
76
- def jsonapi_type
77
- resource.config[:type]
78
- end
77
+ def jsonapi_type
78
+ resource.config[:type]
79
+ end
79
80
 
80
- def generate
81
- raise 'override me'
81
+ def generate
82
+ raise 'override me'
83
+ end
82
84
  end
83
85
  end
@@ -1,39 +1,41 @@
1
- class JsonapiSwaggerHelpers::ShowAction
2
- include JsonapiSwaggerHelpers::Readable
1
+ module JsonapiSwaggerHelpers
2
+ class ShowAction
3
+ include JsonapiSwaggerHelpers::Readable
3
4
 
4
- def action_name
5
- :show
6
- end
5
+ def action_name
6
+ :show
7
+ end
7
8
 
8
- def generate
9
- _self = self
9
+ def generate
10
+ _self = self
10
11
 
11
- @node.operation :get do
12
- key :description, _self.full_description
13
- key :operationId, _self.operation_id
14
- key :tags, _self.all_tags
12
+ @node.operation :get do
13
+ key :description, _self.full_description
14
+ key :operationId, _self.operation_id
15
+ key :tags, _self.all_tags
15
16
 
16
- _self.util.id_in_url(self)
17
- _self.util.jsonapi_fields(self, _self.jsonapi_type)
17
+ _self.util.id_in_url(self)
18
+ _self.util.jsonapi_fields(self, _self.jsonapi_type)
18
19
 
19
- if _self.has_extra_fields?
20
- _self.util.jsonapi_extra_fields(self, _self.resource)
21
- end
20
+ if _self.has_extra_fields?
21
+ _self.util.jsonapi_extra_fields(self, _self.resource)
22
+ end
22
23
 
23
- _self.each_stat do |stat_name, calculations|
24
- _self.util.jsonapi_stat(self, stat_name, calculations)
25
- end
24
+ _self.each_stat do |stat_name, calculations|
25
+ _self.util.jsonapi_stat(self, stat_name, calculations)
26
+ end
26
27
 
27
- if _self.has_sideloads?
28
- _self.util.jsonapi_includes(self)
28
+ if _self.has_sideloads?
29
+ _self.util.jsonapi_includes(self)
29
30
 
30
- _self.each_association do |association_name, association_resource|
31
- _self.util.each_filter(association_resource, association_name) do |filter_label|
32
- _self.util.jsonapi_filter(self, filter_label)
33
- _self.util.jsonapi_fields(self, association_resource.config[:type])
31
+ _self.each_association do |association_name, association_resource|
32
+ _self.util.each_filter(association_resource, association_name) do |filter_label|
33
+ _self.util.jsonapi_filter(self, filter_label)
34
+ _self.util.jsonapi_fields(self, association_resource.config[:type])
34
35
 
35
- if association_resource.config[:extra_fields].keys.length > 0
36
- _self.util.jsonapi_extra_fields(self, association_resource)
36
+ if association_resource.config[:extra_fields].keys.length > 0
37
+ _self.util.jsonapi_extra_fields(self, association_resource)
38
+ end
37
39
  end
38
40
  end
39
41
  end
@@ -1,27 +1,29 @@
1
- class JsonapiSwaggerHelpers::UpdateAction
2
- include JsonapiSwaggerHelpers::Writeable
1
+ module JsonapiSwaggerHelpers
2
+ class UpdateAction
3
+ include JsonapiSwaggerHelpers::Writeable
3
4
 
4
- def action_name
5
- :update
6
- end
5
+ def action_name
6
+ :update
7
+ end
7
8
 
8
- def generate
9
- _self = self
9
+ def generate
10
+ _self = self
10
11
 
11
- define_schema
12
- @node.operation :put do
13
- key :description, _self.description
14
- key :operationId, _self.operation_id
15
- key :tags, _self.all_tags
12
+ define_schema
13
+ @node.operation :put do
14
+ key :description, _self.description
15
+ key :operationId, _self.operation_id
16
+ key :tags, _self.all_tags
16
17
 
17
- _self.util.id_in_url(self)
18
+ _self.util.id_in_url(self)
18
19
 
19
- parameter do
20
- key :name, :payload
21
- key :in, :body
20
+ parameter do
21
+ key :name, :payload
22
+ key :in, :body
22
23
 
23
- schema do
24
- key :'$ref', :"#{_self.strong_resource.name}_update"
24
+ schema do
25
+ key :'$ref', :"#{_self.strong_resource.name}_update"
26
+ end
25
27
  end
26
28
  end
27
29
  end
@@ -1,165 +1,167 @@
1
- module JsonapiSwaggerHelpers::Util
2
- def self.controller_for(path)
3
- path = path.sub('{id}', '1')
4
- route = Rails.application.routes.recognize_path(path)
5
- "#{route[:controller]}_controller".classify.constantize
6
- end
1
+ module JsonapiSwaggerHelpers
2
+ class Util
3
+ def self.controller_for(path)
4
+ path = path.sub('{id}', '1')
5
+ route = Rails.application.routes.recognize_path(path)
6
+ "#{route[:controller]}_controller".classify.constantize
7
+ end
7
8
 
8
- def self.sideload_label(include_directive)
9
- sideloads = include_directive.to_string.split(",").sort.join(",<br/>")
9
+ def self.sideload_label(include_directive)
10
+ sideloads = include_directive.to_string.split(",").sort.join(",<br/>")
10
11
 
11
- <<-HTML
12
- <label>
13
- Possible sideloads:
14
- <span class="possible-sideloads">#{sideloads}</span>
15
- </label>
16
- HTML
17
- end
12
+ <<-HTML
13
+ <label>
14
+ Possible sideloads:
15
+ <span class="possible-sideloads">#{sideloads}</span>
16
+ </label>
17
+ HTML
18
+ end
18
19
 
19
- def self.each_filter(resource, association_name = nil)
20
- resource.config[:filters].each_pair do |filter_name, opts|
21
- if association_name
22
- yield "filter[#{association_name}][#{filter_name}]"
23
- else
24
- yield "filter[#{filter_name}]"
20
+ def self.each_filter(resource, association_name = nil)
21
+ resource.config[:filters].each_pair do |filter_name, opts|
22
+ if association_name
23
+ yield "filter[#{association_name}][#{filter_name}]"
24
+ else
25
+ yield "filter[#{filter_name}]"
26
+ end
25
27
  end
26
28
  end
27
- end
28
29
 
29
- def self.all_resources(resource, include_directive, memo = {})
30
- resource.sideloading.sideloads.each_pair do |name, sideload|
31
- next if memo[name] || !include_directive.key?(name)
30
+ def self.all_resources(resource, include_directive, memo = {})
31
+ resource.sideloading.sideloads.each_pair do |name, sideload|
32
+ next if memo[name] || !include_directive.key?(name)
32
33
 
33
- memo[name] = sideload.resource.class
34
- all_resources(sideload.resource.class, include_directive[name], memo)
34
+ memo[name] = sideload.resource.class
35
+ all_resources(sideload.resource.class, include_directive[name], memo)
36
+ end
37
+ memo
35
38
  end
36
- memo
37
- end
38
39
 
39
- def self.include_directive_for(controller, action)
40
- resource_class = controller._jsonapi_compliable
41
- includes = resource_class.sideloading.to_hash[:base]
42
- whitelist = resource_class.config[:sideload_whitelist]
40
+ def self.include_directive_for(controller, action)
41
+ resource_class = controller._jsonapi_compliable
42
+ includes = resource_class.sideloading.to_hash[:base]
43
+ whitelist = resource_class.config[:sideload_whitelist]
43
44
 
44
- if whitelist && whitelist[action]
45
- includes = JsonapiCompliable::Util::IncludeParams
46
- .scrub(includes, whitelist[action])
47
- end
45
+ if whitelist && whitelist[action]
46
+ includes = JsonapiCompliable::Util::IncludeParams
47
+ .scrub(includes, whitelist[action])
48
+ end
48
49
 
49
- JSONAPI::IncludeDirective.new(includes)
50
- end
50
+ JSONAPI::IncludeDirective.new(includes)
51
+ end
51
52
 
52
- def self.payloads_for(resource, include_hash)
53
- [].tap do |payloads|
54
- payloads << JsonapiSpecHelpers::Payload.by_type(resource.config[:type])
53
+ def self.payloads_for(resource, include_hash)
54
+ [].tap do |payloads|
55
+ payloads << JsonapiSpecHelpers::Payload.by_type(resource.config[:type])
55
56
 
56
- include_hash.each_pair do |name, nested|
57
- sideload = resource.sideloading.sideloads[name]
57
+ include_hash.each_pair do |name, nested|
58
+ sideload = resource.sideloading.sideloads[name]
58
59
 
59
- if sideload.polymorphic?
60
- sideload.polymorphic_groups.each_pair do |type, sl|
61
- payloads << payloads_for(sl.resource_class, nested)
60
+ if sideload.polymorphic?
61
+ sideload.polymorphic_groups.each_pair do |type, sl|
62
+ payloads << payloads_for(sl.resource_class, nested)
63
+ end
64
+ else
65
+ sideload_resource = sideload.resource_class
66
+ payloads << payloads_for(sideload_resource, nested)
62
67
  end
63
- else
64
- sideload_resource = sideload.resource_class
65
- payloads << payloads_for(sideload_resource, nested)
66
68
  end
67
- end
68
- end.flatten.uniq(&:name)
69
- end
70
-
71
- def self.payload_tags_for(resource, include_hash)
72
- payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" }
73
- end
69
+ end.flatten.uniq(&:name)
70
+ end
74
71
 
75
- def self.jsonapi_filter(node, label)
76
- node.parameter do
77
- key :description, '<a href="http://jsonapi.org/format/#fetching-filtering">JSONAPI Filter</a>'
78
- key :name, label
79
- key :in, :query
80
- key :type, :string
81
- key :required, false
72
+ def self.payload_tags_for(resource, include_hash)
73
+ payloads_for(resource, include_hash).map { |p| "payload-#{p.name}" }
82
74
  end
83
- end
84
75
 
85
- def self.jsonapi_sorting(node)
86
- node.parameter do
87
- key :description, '<a href="http://jsonapi.org/format/#fetching-sorting">JSONAPI Sorting</a>'
88
- key :name, :sort
89
- key :in, :query
90
- key :type, :string
91
- key :required, false
76
+ def self.jsonapi_filter(node, label)
77
+ node.parameter do
78
+ key :description, '<a href="http://jsonapi.org/format/#fetching-filtering">JSONAPI Filter</a>'
79
+ key :name, label
80
+ key :in, :query
81
+ key :type, :string
82
+ key :required, false
83
+ end
92
84
  end
93
- end
94
85
 
95
- def self.jsonapi_pagination(node)
96
- node.parameter do
97
- key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Size</a>'
98
- key :name, "page[size]"
99
- key :in, :query
100
- key :type, :string
101
- key :required, false
86
+ def self.jsonapi_sorting(node)
87
+ node.parameter do
88
+ key :description, '<a href="http://jsonapi.org/format/#fetching-sorting">JSONAPI Sorting</a>'
89
+ key :name, :sort
90
+ key :in, :query
91
+ key :type, :string
92
+ key :required, false
93
+ end
102
94
  end
103
95
 
104
- node.parameter do
105
- key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Number</a>'
106
- key :name, "page[number]"
107
- key :in, :query
108
- key :type, :string
109
- key :required, false
96
+ def self.jsonapi_pagination(node)
97
+ node.parameter do
98
+ key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Size</a>'
99
+ key :name, "page[size]"
100
+ key :in, :query
101
+ key :type, :string
102
+ key :required, false
103
+ end
104
+
105
+ node.parameter do
106
+ key :description, '<a href="http://jsonapi.org/format/#fetching-pagination">JSONAPI Page Number</a>'
107
+ key :name, "page[number]"
108
+ key :in, :query
109
+ key :type, :string
110
+ key :required, false
111
+ end
110
112
  end
111
- end
112
113
 
113
- def self.jsonapi_stat(node, name, calculations)
114
- node.parameter do
115
- key :name, "stats[#{name}]"
116
- key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-return-statistics\">JSONAPI Stats</a><br /><b>Possible Calculations:</b> #{calculations}"
117
- key :in, :query
118
- key :type, :string
119
- key :required, false
114
+ def self.jsonapi_stat(node, name, calculations)
115
+ node.parameter do
116
+ key :name, "stats[#{name}]"
117
+ key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-return-statistics\">JSONAPI Stats</a><br /><b>Possible Calculations:</b> #{calculations}"
118
+ key :in, :query
119
+ key :type, :string
120
+ key :required, false
121
+ end
120
122
  end
121
- end
122
123
 
123
- def self.jsonapi_includes(node)
124
- node.parameter do
125
- key :description, '<a href="http://jsonapi.org/format/#fetching-includes">JSONAPI Includes</a>'
126
- key :name, :include
127
- key :in, :query
128
- key :type, :string
129
- key :required, false
124
+ def self.jsonapi_includes(node)
125
+ node.parameter do
126
+ key :description, '<a href="http://jsonapi.org/format/#fetching-includes">JSONAPI Includes</a>'
127
+ key :name, :include
128
+ key :in, :query
129
+ key :type, :string
130
+ key :required, false
131
+ end
130
132
  end
131
- end
132
133
 
133
- def self.jsonapi_fields(node, jsonapi_type)
134
- node.parameter do
135
- key :description, '<a href="http://jsonapi.org/format/#fetching-sparse-fieldsets">JSONAPI Sparse Fieldset</a>'
136
- key :name, "fields[#{jsonapi_type}]"
137
- key :in, :query
138
- key :type, :string
139
- key :required, false
134
+ def self.jsonapi_fields(node, jsonapi_type)
135
+ node.parameter do
136
+ key :description, '<a href="http://jsonapi.org/format/#fetching-sparse-fieldsets">JSONAPI Sparse Fieldset</a>'
137
+ key :name, "fields[#{jsonapi_type}]"
138
+ key :in, :query
139
+ key :type, :string
140
+ key :required, false
141
+ end
140
142
  end
141
- end
142
143
 
143
- def self.jsonapi_extra_fields(node, resource)
144
- jsonapi_type = resource.config[:type]
144
+ def self.jsonapi_extra_fields(node, resource)
145
+ jsonapi_type = resource.config[:type]
145
146
 
146
- extra_field_names = resource.config[:extra_fields].keys.join(',')
147
- node.parameter do
148
- key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-conditionally-render-fields\">JSONAPI Extra Fields</a><br /><b>Possible Fields:</b> #{extra_field_names}"
149
- key :name, "extra_fields[#{jsonapi_type}]"
150
- key :in, :query
151
- key :type, :string
152
- key :required, false
147
+ extra_field_names = resource.config[:extra_fields].keys.join(',')
148
+ node.parameter do
149
+ key :description, "<a href=\"https://jsonapi-suite.github.io/jsonapi_suite/how-to-conditionally-render-fields\">JSONAPI Extra Fields</a><br /><b>Possible Fields:</b> #{extra_field_names}"
150
+ key :name, "extra_fields[#{jsonapi_type}]"
151
+ key :in, :query
152
+ key :type, :string
153
+ key :required, false
154
+ end
153
155
  end
154
- end
155
156
 
156
- def self.id_in_url(node)
157
- node.parameter do
158
- key :name, :id
159
- key :in, :path
160
- key :type, :string
161
- key :required, true
162
- key :description, 'record id'
157
+ def self.id_in_url(node)
158
+ node.parameter do
159
+ key :name, :id
160
+ key :in, :path
161
+ key :type, :string
162
+ key :required, true
163
+ key :description, 'record id'
164
+ end
163
165
  end
164
166
  end
165
167
  end
@@ -1,3 +1,3 @@
1
1
  module JsonapiSwaggerHelpers
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -1,89 +1,91 @@
1
- module JsonapiSwaggerHelpers::Writeable
2
- def self.included(klass)
3
- klass.class_eval do
4
- attr_reader :node,
5
- :controller,
6
- :resource,
7
- :description,
8
- :tags
1
+ module JsonapiSwaggerHelpers
2
+ module Writeable
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ attr_reader :node,
6
+ :controller,
7
+ :resource,
8
+ :description,
9
+ :tags
10
+ end
9
11
  end
10
- end
11
-
12
- def initialize(node, controller, description: nil, tags: [])
13
- @node = node
14
- @controller = controller
15
- @resource = controller._jsonapi_compliable
16
- @description = description || default_description
17
- @tags = tags
18
- end
19
12
 
20
- def util
21
- JsonapiSwaggerHelpers::Util
22
- end
23
-
24
- def action_name
25
- raise 'override me'
26
- end
13
+ def initialize(node, controller, description: nil, tags: [])
14
+ @node = node
15
+ @controller = controller
16
+ @resource = controller._jsonapi_compliable
17
+ @description = description || default_description
18
+ @tags = tags
19
+ end
27
20
 
28
- def default_description
29
- "#{action_name.to_s.capitalize} Action"
30
- end
21
+ def util
22
+ JsonapiSwaggerHelpers::Util
23
+ end
31
24
 
32
- def operation_id
33
- "#{controller.name.gsub('::', '-')}-#{action_name}"
34
- end
25
+ def action_name
26
+ raise 'override me'
27
+ end
35
28
 
36
- def all_tags
37
- tags + payload_tags
38
- end
29
+ def default_description
30
+ "#{action_name.to_s.capitalize} Action"
31
+ end
39
32
 
40
- def payload_tags
41
- tags = [:"payload-#{strong_resource.name}_#{action_name}"]
33
+ def operation_id
34
+ "#{controller.name.gsub('::', '-')}-#{action_name}"
35
+ end
42
36
 
43
- strong_resource.relations.each_pair do |relation_name, relation_config|
44
- tags << :"payload-#{strong_resource.name}_#{relation_name}_#{action_name}"
37
+ def all_tags
38
+ tags + payload_tags
45
39
  end
46
40
 
47
- tags
48
- end
41
+ def payload_tags
42
+ tags = [:"payload-#{strong_resource.name}_#{action_name}"]
49
43
 
50
- def context
51
- JsonapiSwaggerHelpers.docs_controller
52
- end
44
+ strong_resource.relations.each_pair do |relation_name, relation_config|
45
+ tags << :"payload-#{strong_resource.name}_#{relation_name}_#{action_name}"
46
+ end
53
47
 
54
- def strong_resource
55
- controller._strong_resources[action_name]
56
- end
48
+ tags
49
+ end
57
50
 
58
- def define_schema
59
- _self = self
60
- context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
61
- _self.strong_resource.attributes.each_pair do |attribute, config|
62
- property attribute do
63
- key :type, config[:type] # TODO - swagger type?
64
- end
65
- end
51
+ def context
52
+ JsonapiSwaggerHelpers.docs_controller
66
53
  end
67
54
 
68
- _self.strong_resource.relations.each_pair do |relation_name, relation_config|
69
- context.send(:swagger_schema, :"#{strong_resource.name}_#{relation_name}_#{action_name}") do
70
- relation_config[:resource].attributes.each_pair do |attribute, config|
55
+ def strong_resource
56
+ controller._strong_resources[action_name]
57
+ end
58
+
59
+ def define_schema
60
+ _self = self
61
+ context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
62
+ _self.strong_resource.attributes.each_pair do |attribute, config|
71
63
  property attribute do
72
64
  key :type, config[:type] # TODO - swagger type?
73
65
  end
74
66
  end
75
67
  end
68
+
69
+ _self.strong_resource.relations.each_pair do |relation_name, relation_config|
70
+ context.send(:swagger_schema, :"#{strong_resource.name}_#{relation_name}_#{action_name}") do
71
+ relation_config[:resource].attributes.each_pair do |attribute, config|
72
+ property attribute do
73
+ key :type, config[:type] # TODO - swagger type?
74
+ end
75
+ end
76
+ end
77
+ end
76
78
  end
77
- end
78
79
 
79
- def generate
80
- _self = self
80
+ def generate
81
+ _self = self
81
82
 
82
- define_schema
83
- @node.operation :post do
84
- key :description, _self.description
85
- key :operationId, _self.operation_id
86
- key :tags, _self.all_tags
83
+ define_schema
84
+ @node.operation :post do
85
+ key :description, _self.description
86
+ key :operationId, _self.operation_id
87
+ key :tags, _self.all_tags
88
+ end
87
89
  end
88
90
  end
89
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_swagger_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
@@ -102,6 +102,7 @@ files:
102
102
  - lib/jsonapi_swagger_helpers/create_action.rb
103
103
  - lib/jsonapi_swagger_helpers/destroy_action.rb
104
104
  - lib/jsonapi_swagger_helpers/docs_controller_mixin.rb
105
+ - lib/jsonapi_swagger_helpers/errors.rb
105
106
  - lib/jsonapi_swagger_helpers/index_action.rb
106
107
  - lib/jsonapi_swagger_helpers/payload_definition.rb
107
108
  - lib/jsonapi_swagger_helpers/readable.rb