dry_crud_jsonapi_swagger 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 807ceabf80b721ba30a118e1329834c7d3bbd59b01a3cda459ff9f1988c147bb
4
- data.tar.gz: e004fe507b0d66740bd4123e59e53cede709043dc0837397ba655ae40cbc288f
3
+ metadata.gz: c9d805f02243eba631d4656adc5a7d7570367c11cdf7833f998d9beaf37336e6
4
+ data.tar.gz: 76ff302864cd450f719890d3742c547ac995177f7174ce0f04727176d4cae6a6
5
5
  SHA512:
6
- metadata.gz: 91ad6961a51fbcf661c1bc719dd9f9d7d3b69a3c3f92320cf9b31b54532261eefab8f276f674fb724be976d670995ebc6611447b20f6e97cd3fb50e18cf65fc7
7
- data.tar.gz: 0d59851eb55a49cbe99a39f15839e5d06ac48e5b0f8cf14c5eae9bee2ef61303b981e42027bf65aef72ad3c7fd7d5acda557b5bed1f51cefc782bada597846f2
6
+ metadata.gz: 9f6a9025c590e41addc7fe4fba525c97632ea10f6acb60d336a6d592d47bb5ff2ad4103a53d871eabb8f31dc8ebf5712f8433c94653fa6ca66a7cda7a1c877ab
7
+ data.tar.gz: b7e56393467e77e5979b0d3d958bb739c9befb8de9a1c5197a8795c245eac5f53973fed6b925ffa205f44c5c0c83ca37eb91d94c265732be8716221be75fca60
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # DryCrudJsonapiSwagger
2
- Short description and motivation.
3
-
4
- ## Usage
5
- How to use my plugin.
2
+ This Gem provides a Rails Engine to generate the api documentation for Applications based on [`dry_crud_jsonapi`].
3
+ The documentation is exposed as a swagger v2 specification and can be viewed with the included, interactive swagger web interface.
6
4
 
7
5
  ## Installation
8
6
  Add this line to your application's Gemfile:
@@ -21,8 +19,20 @@ Or install it yourself as:
21
19
  $ gem install dry_crud_jsonapi_swagger
22
20
  ```
23
21
 
22
+ ## Usage
23
+ * Implement the `json:api` in your application with [`dry_crud_jsonapi`]
24
+ * Mount the engine in your `routes.rb`:
25
+
26
+ mount DryCrudJsonapiSwagger::Engine => '/apidocs'
27
+
28
+ The swagger webinterface is available on the mount path, i.e. `/apidocs`.
29
+ To get the swagger v2 specification, just add `/swagger.json` to the mount path, i.e. `/apidocs/swagger.json`.
30
+
24
31
  ## Contributing
25
32
  Contribution directions go here.
26
33
 
27
34
  ## License
28
35
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
+
37
+
38
+ [`dry_crud_jsonapi`]: https://github.com/puzzle/dry_crud_jsonapi
@@ -24,8 +24,8 @@ module DryCrudJsonapiSwagger
24
24
  end
25
25
 
26
26
  def json_api_controllers
27
- ListController.descendants.select { |model| model.include?(DryCrudJsonapi) }.select do |controller|
28
- controller.model_class rescue false
27
+ ActionController::Base.descendants.select do |controller|
28
+ controller.include?(DryCrudJsonapi) && controller&.model_class.present?
29
29
  end
30
30
  end
31
31
  end
@@ -37,21 +37,20 @@ module DryCrudJsonapiSwagger
37
37
  nested_class.model_class.model_name
38
38
  end
39
39
 
40
- def description(controller = controller_class)
41
- # relationships = controller.model_class
42
- # .reflect_on_all_associations(:belongs_to)
43
- # .map(&:name).sort
44
- #
45
- # relationships += controller.model_class
46
- # .reflect_on_all_associations(:has_many)
47
- # .map(&:name).sort
40
+ def include_description(controller = controller_class)
41
+ <<~DESC
42
+ Available primary relations:
43
+ #{includes(controller.model_class).map { |inc| "* #{inc}" }.join("\n")}
48
44
 
49
- relationships = controller.model_class
50
- .reflect_on_all_associations
51
- .map(&:name).sort
45
+ Separate values with a comma
52
46
 
53
- 'The following relationships are available: ' \
54
- "#{relationships.join(', ')} (separate values with a comma)"
47
+ To include sub-relations, specify the relationship chain with the elements separated by '.'
48
+ i.e. "employee.address.town"
49
+ DESC
50
+ end
51
+
52
+ def includes(model_class)
53
+ model_class.reflect_on_all_associations.reject(&:polymorphic?).map(&:name).sort
55
54
  end
56
55
 
57
56
  def path_spec(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
@@ -64,7 +63,7 @@ module DryCrudJsonapiSwagger
64
63
 
65
64
  swagger_doc.operation :get do
66
65
  key :summary, summary
67
- helper.setup_tags(self)
66
+ helper.setup_tag(self)
68
67
  helper.parameters(self, helper, type)
69
68
  response 200 do
70
69
  key :description, summary + ' Response'
@@ -73,16 +72,17 @@ module DryCrudJsonapiSwagger
73
72
  end
74
73
  end
75
74
 
76
- def setup_tags(swagger_doc)
77
- swagger_doc.key :tags, [
78
- 'All',
79
- TagsSetup.path_tag(@path)
80
- ]
75
+ def setup_tag(swagger_doc)
76
+ tag = TagsSetup.new.path_tag(@path)
77
+ swagger_doc.key :tags, [tag] if tag.present?
81
78
  end
82
79
 
83
80
  def parameters(swagger_doc, helper, type)
84
81
  case type.to_sym
85
- when :show, :nested then parameter_id swagger_doc, helper
82
+ when :index
83
+ parameter_custom swagger_doc, type
84
+ when :show, :nested
85
+ parameter_id swagger_doc, helper
86
86
  end
87
87
 
88
88
  parameter_include swagger_doc, helper, type
@@ -100,15 +100,32 @@ module DryCrudJsonapiSwagger
100
100
 
101
101
  def parameter_include(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
102
102
  desc = case type.to_sym
103
- when :index, :show then description
104
- when :nested then description helper.nested_class
103
+ when :index, :show then include_description
104
+ when :nested then include_description(helper.nested_class)
105
105
  end
106
106
  swagger_doc.parameter do
107
- key :name, :include
108
- key :in, :query
109
- key :description, desc
110
- key :required, false
111
- key :type, :string
107
+ key :name, :include
108
+ key :in, :query
109
+ key :description, desc
110
+ key :required, false
111
+ key :type, :array
112
+ key :collectionFormat, :csv
113
+ items do
114
+ key :type, :string
115
+ end
116
+ end
117
+ end
118
+
119
+ def parameter_custom(swagger_doc, type)
120
+ controller_class.swagger_params[type].each do |param|
121
+ swagger_doc.parameter do
122
+ key :name, param.name
123
+ key :in, :query
124
+ key :description, param.description
125
+ key :required, param.required
126
+ key :type, param.type
127
+ key :enum, param.enum if param.enum.present?
128
+ end
112
129
  end
113
130
  end
114
131
 
@@ -126,4 +143,4 @@ module DryCrudJsonapiSwagger
126
143
  end
127
144
  end
128
145
  end
129
- end
146
+ end
@@ -1,3 +1,5 @@
1
+ require 'yaml'
2
+
1
3
  module DryCrudJsonapiSwagger
2
4
  class TagsSetup
3
5
 
@@ -9,37 +11,46 @@ module DryCrudJsonapiSwagger
9
11
  setup_tags
10
12
  end
11
13
 
12
- def self.path_tag(path)
13
- new.get_tag_by_path(path)
14
+ def path_tag(path)
15
+ customized_tag(path) || derived_tag(path)
14
16
  end
15
17
 
16
- def get_tag_by_path(path)
17
- tags.each do |tag|
18
- next if tag['include'].blank?
18
+ private
19
19
 
20
- tag['include'].each do |inc|
21
- return tag['name'] if path =~ /#{inc}/i
22
- end
20
+ def customized_tag(path)
21
+ customized_tags.each do |tag|
22
+ next if tag['include'].blank?
23
23
 
24
+ return tag['name'] if tag['include'].any? { |inc| path =~ /#{inc}/i }
24
25
  end
26
+
27
+ nil
25
28
  end
26
29
 
27
- private
28
-
30
+ def derived_tag(path)
31
+ path.gsub(%r(/{[^}]+}), '').gsub('/', ' ').strip.titleize
32
+ end
29
33
 
30
34
  def setup_tags
31
- tags.each do |tag|
35
+ customized_tags.each do |tag|
32
36
  @swagger_doc.tags tag.slice('name', 'description', 'externalDocs')
33
37
  end
34
38
  end
35
39
 
36
- def tags
37
- @tags ||= load_tags['tags']
40
+ def customized_tags
41
+ @customized_tags ||= load_customized_tags
38
42
  end
39
43
 
40
- def load_tags
41
- require 'yaml'
42
- YAML.load_file(Rails.root.join('config', 'swagger-tags.yml')) # TODO: what is this for? fill yml with sensible values
44
+ def load_customized_tags
45
+ return [] unless File.exist?(Rails.root.join('config', 'swagger-tags.yml'))
46
+
47
+ YAML.load_file(Rails.root.join('config', 'swagger-tags.yml')).yield_self do |tags_data|
48
+ raise('Your config/swagger-tags.yml contains malformed data') unless
49
+ Array.wrap(tags_data&.keys).include?('tags') &&
50
+ Array.wrap(tags_data['tags']).all? { |tag| tag.is_a?(Hash) }
51
+
52
+ Array.wrap(tags_data['tags'])
53
+ end
43
54
  end
44
55
  end
45
- end
56
+ end
@@ -1,5 +1,17 @@
1
1
  require "dry_crud_jsonapi_swagger/engine"
2
+ require "dry_crud_jsonapi_swagger/param"
2
3
 
3
4
  module DryCrudJsonapiSwagger
4
- # Your code goes here...
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :swagger_params, instance_writer: false
9
+ self.swagger_params = Hash.new { |hash, key| hash[key] = [] }
10
+ end
11
+
12
+ class_methods do
13
+ def swagger_param(action, name, type: type, description: nil, required: false, enum: nil)
14
+ swagger_params[action.to_sym] << Param.new(name, type, description, required, enum)
15
+ end
16
+ end
5
17
  end
@@ -0,0 +1,3 @@
1
+ module DryCrudJsonapiSwagger
2
+ Param = Struct.new(:name, :type, :description, :required, :enum)
3
+ end
@@ -1,3 +1,3 @@
1
1
  module DryCrudJsonapiSwagger
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_crud_jsonapi_swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Illi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-17 00:00:00.000000000 Z
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - config/routes.rb
100
100
  - lib/dry_crud_jsonapi_swagger.rb
101
101
  - lib/dry_crud_jsonapi_swagger/engine.rb
102
+ - lib/dry_crud_jsonapi_swagger/param.rb
102
103
  - lib/dry_crud_jsonapi_swagger/version.rb
103
104
  homepage: https://rubygems.org/gems/dry_crud_jsonapi_swagger
104
105
  licenses: