praxis 2.0.pre.18 → 2.0.pre.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +54 -0
  3. data/.simplecov +3 -1
  4. data/.travis.yml +2 -1
  5. data/CHANGELOG.md +6 -0
  6. data/CONTRIBUTING.md +2 -79
  7. data/Gemfile +5 -1
  8. data/Guardfile +6 -4
  9. data/LICENSE +0 -2
  10. data/MAINTAINERS.md +1 -0
  11. data/README.md +15 -22
  12. data/Rakefile +4 -2
  13. data/bin/praxis +55 -58
  14. data/lib/praxis/action_definition/headers_dsl_compiler.rb +5 -6
  15. data/lib/praxis/action_definition.rb +64 -94
  16. data/lib/praxis/api_definition.rb +21 -29
  17. data/lib/praxis/api_general_info.rb +55 -66
  18. data/lib/praxis/application.rb +15 -32
  19. data/lib/praxis/blueprint.rb +64 -73
  20. data/lib/praxis/bootloader.rb +24 -33
  21. data/lib/praxis/bootloader_stages/environment.rb +5 -10
  22. data/lib/praxis/bootloader_stages/file_loader.rb +3 -6
  23. data/lib/praxis/bootloader_stages/plugin_config_load.rb +4 -6
  24. data/lib/praxis/bootloader_stages/plugin_config_prepare.rb +2 -2
  25. data/lib/praxis/bootloader_stages/plugin_loader.rb +3 -7
  26. data/lib/praxis/bootloader_stages/plugin_setup.rb +3 -3
  27. data/lib/praxis/bootloader_stages/routing.rb +5 -8
  28. data/lib/praxis/bootloader_stages/subgroup_loader.rb +2 -10
  29. data/lib/praxis/bootloader_stages/warn_unloaded_files.rb +15 -19
  30. data/lib/praxis/callbacks.rb +12 -11
  31. data/lib/praxis/collection.rb +10 -13
  32. data/lib/praxis/config.rb +17 -28
  33. data/lib/praxis/config_hash.rb +2 -1
  34. data/lib/praxis/controller.rb +7 -6
  35. data/lib/praxis/dispatcher.rb +34 -42
  36. data/lib/praxis/docs/open_api/info_object.rb +11 -8
  37. data/lib/praxis/docs/open_api/media_type_object.rb +18 -17
  38. data/lib/praxis/docs/open_api/operation_object.rb +6 -3
  39. data/lib/praxis/docs/open_api/parameter_object.rb +17 -14
  40. data/lib/praxis/docs/open_api/paths_object.rb +11 -9
  41. data/lib/praxis/docs/open_api/request_body_object.rb +14 -13
  42. data/lib/praxis/docs/open_api/response_object.rb +24 -18
  43. data/lib/praxis/docs/open_api/responses_object.rb +3 -1
  44. data/lib/praxis/docs/open_api/schema_object.rb +16 -16
  45. data/lib/praxis/docs/open_api/server_object.rb +5 -2
  46. data/lib/praxis/docs/open_api/tag_object.rb +6 -3
  47. data/lib/praxis/docs/open_api_generator.rb +92 -95
  48. data/lib/praxis/endpoint_definition.rb +60 -77
  49. data/lib/praxis/error_handler.rb +2 -2
  50. data/lib/praxis/exception.rb +2 -0
  51. data/lib/praxis/exceptions/config.rb +3 -1
  52. data/lib/praxis/exceptions/config_load.rb +2 -0
  53. data/lib/praxis/exceptions/config_validation.rb +3 -1
  54. data/lib/praxis/exceptions/invalid_configuration.rb +3 -1
  55. data/lib/praxis/exceptions/invalid_response.rb +3 -1
  56. data/lib/praxis/exceptions/invalid_trait.rb +3 -1
  57. data/lib/praxis/exceptions/stage_not_found.rb +3 -1
  58. data/lib/praxis/exceptions/validation.rb +4 -3
  59. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +163 -149
  60. data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +18 -13
  61. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +13 -9
  62. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +14 -11
  63. data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +12 -9
  64. data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +8 -5
  65. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +89 -65
  66. data/lib/praxis/extensions/attribute_filtering/filters_parser.rb +68 -62
  67. data/lib/praxis/extensions/attribute_filtering.rb +3 -1
  68. data/lib/praxis/extensions/field_expansion.rb +6 -4
  69. data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +10 -8
  70. data/lib/praxis/extensions/field_selection/field_selector.rb +91 -92
  71. data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +12 -12
  72. data/lib/praxis/extensions/field_selection.rb +3 -1
  73. data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +6 -4
  74. data/lib/praxis/extensions/pagination/header_generator.rb +16 -11
  75. data/lib/praxis/extensions/pagination/ordering_params.rb +29 -28
  76. data/lib/praxis/extensions/pagination/pagination_handler.rb +44 -42
  77. data/lib/praxis/extensions/pagination/pagination_params.rb +29 -48
  78. data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +8 -7
  79. data/lib/praxis/extensions/pagination.rb +10 -15
  80. data/lib/praxis/extensions/rails_compat/request_methods.rb +3 -4
  81. data/lib/praxis/extensions/rails_compat.rb +2 -0
  82. data/lib/praxis/extensions/rendering.rb +12 -12
  83. data/lib/praxis/field_expander.rb +8 -9
  84. data/lib/praxis/file_group.rb +8 -12
  85. data/lib/praxis/finalizable.rb +1 -0
  86. data/lib/praxis/handlers/json.rb +5 -2
  87. data/lib/praxis/handlers/plain.rb +2 -1
  88. data/lib/praxis/handlers/www_form.rb +6 -3
  89. data/lib/praxis/handlers/{xml-sample.rb → xml_sample.rb} +26 -22
  90. data/lib/praxis/mapper/active_model_compat.rb +13 -10
  91. data/lib/praxis/mapper/resource.rb +171 -180
  92. data/lib/praxis/mapper/selector_generator.rb +106 -112
  93. data/lib/praxis/mapper/sequel_compat.rb +70 -67
  94. data/lib/praxis/media_type.rb +2 -2
  95. data/lib/praxis/media_type_identifier.rb +26 -22
  96. data/lib/praxis/middleware_app.rb +18 -15
  97. data/lib/praxis/multipart/parser.rb +46 -51
  98. data/lib/praxis/multipart/part.rb +78 -110
  99. data/lib/praxis/notifications.rb +2 -4
  100. data/lib/praxis/plugin.rb +11 -18
  101. data/lib/praxis/plugin_concern.rb +12 -15
  102. data/lib/praxis/plugins/mapper_plugin.rb +15 -13
  103. data/lib/praxis/plugins/pagination_plugin.rb +8 -6
  104. data/lib/praxis/plugins/rails_plugin.rb +33 -28
  105. data/lib/praxis/renderer.rb +11 -15
  106. data/lib/praxis/request.rb +46 -47
  107. data/lib/praxis/request_stages/action.rb +4 -6
  108. data/lib/praxis/request_stages/load_request.rb +2 -4
  109. data/lib/praxis/request_stages/request_stage.rb +19 -23
  110. data/lib/praxis/request_stages/response.rb +4 -6
  111. data/lib/praxis/request_stages/validate.rb +3 -5
  112. data/lib/praxis/request_stages/validate_params_and_headers.rb +15 -16
  113. data/lib/praxis/request_stages/validate_payload.rb +25 -27
  114. data/lib/praxis/request_superclassing.rb +3 -3
  115. data/lib/praxis/resource_definition.rb +1 -0
  116. data/lib/praxis/response.rb +13 -25
  117. data/lib/praxis/response_definition.rb +77 -122
  118. data/lib/praxis/response_template.rb +11 -15
  119. data/lib/praxis/responses/http.rb +23 -44
  120. data/lib/praxis/responses/internal_server_error.rb +18 -21
  121. data/lib/praxis/responses/multipart_ok.rb +4 -9
  122. data/lib/praxis/responses/validation_error.rb +8 -15
  123. data/lib/praxis/route.rb +8 -10
  124. data/lib/praxis/router/rack.rb +13 -7
  125. data/lib/praxis/router/simple.rb +10 -5
  126. data/lib/praxis/router.rb +27 -34
  127. data/lib/praxis/routing_config.rb +52 -29
  128. data/lib/praxis/simple_media_type.rb +5 -8
  129. data/lib/praxis/stage.rb +17 -25
  130. data/lib/praxis/tasks/api_docs.rb +15 -15
  131. data/lib/praxis/tasks/console.rb +3 -1
  132. data/lib/praxis/tasks/environment.rb +2 -0
  133. data/lib/praxis/tasks/routes.rb +26 -24
  134. data/lib/praxis/tasks.rb +3 -1
  135. data/lib/praxis/trait.rb +37 -46
  136. data/lib/praxis/types/fuzzy_hash.rb +13 -14
  137. data/lib/praxis/types/media_type_common.rb +11 -10
  138. data/lib/praxis/types/multipart_array/part_definition.rb +14 -17
  139. data/lib/praxis/types/multipart_array.rb +88 -112
  140. data/lib/praxis/validation_handler.rb +5 -3
  141. data/lib/praxis/version.rb +3 -1
  142. data/lib/praxis.rb +4 -5
  143. data/praxis.gemspec +22 -21
  144. data/spec/functional_spec.rb +40 -52
  145. data/spec/praxis/action_definition_spec.rb +36 -46
  146. data/spec/praxis/api_definition_spec.rb +45 -47
  147. data/spec/praxis/api_general_info_spec.rb +28 -29
  148. data/spec/praxis/application_spec.rb +18 -14
  149. data/spec/praxis/blueprint_spec.rb +33 -34
  150. data/spec/praxis/bootloader_spec.rb +32 -30
  151. data/spec/praxis/callbacks_spec.rb +37 -37
  152. data/spec/praxis/collection_spec.rb +18 -25
  153. data/spec/praxis/config_hash_spec.rb +5 -4
  154. data/spec/praxis/config_spec.rb +27 -26
  155. data/spec/praxis/controller_spec.rb +8 -9
  156. data/spec/praxis/endpoint_definition_spec.rb +25 -32
  157. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +171 -114
  158. data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +22 -21
  159. data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +112 -60
  160. data/spec/praxis/extensions/attribute_filtering/filters_parser_spec.rb +37 -38
  161. data/spec/praxis/extensions/field_expansion_spec.rb +8 -10
  162. data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +14 -13
  163. data/spec/praxis/extensions/field_selection/field_selector_spec.rb +9 -16
  164. data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +50 -49
  165. data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +32 -31
  166. data/spec/praxis/extensions/rendering_spec.rb +9 -9
  167. data/spec/praxis/extensions/support/spec_resources_active_model.rb +32 -49
  168. data/spec/praxis/extensions/support/spec_resources_sequel.rb +48 -48
  169. data/spec/praxis/field_expander_spec.rb +6 -5
  170. data/spec/praxis/file_group_spec.rb +3 -1
  171. data/spec/praxis/handlers/json_spec.rb +6 -5
  172. data/spec/praxis/mapper/resource_spec.rb +27 -30
  173. data/spec/praxis/mapper/selector_generator_spec.rb +50 -50
  174. data/spec/praxis/media_type_identifier_spec.rb +13 -10
  175. data/spec/praxis/media_type_spec.rb +12 -12
  176. data/spec/praxis/middleware_app_spec.rb +23 -22
  177. data/spec/praxis/multipart/parser_spec.rb +7 -9
  178. data/spec/praxis/notifications_spec.rb +4 -4
  179. data/spec/praxis/plugin_concern_spec.rb +5 -6
  180. data/spec/praxis/renderer_spec.rb +10 -9
  181. data/spec/praxis/request_spec.rb +38 -41
  182. data/spec/praxis/request_stages/action_spec.rb +14 -15
  183. data/spec/praxis/request_stages/request_stage_spec.rb +30 -41
  184. data/spec/praxis/request_stages/validate_spec.rb +3 -1
  185. data/spec/praxis/response_definition_spec.rb +79 -92
  186. data/spec/praxis/response_spec.rb +28 -39
  187. data/spec/praxis/responses/internal_server_error_spec.rb +6 -9
  188. data/spec/praxis/responses/validation_error_spec.rb +17 -18
  189. data/spec/praxis/route_spec.rb +4 -7
  190. data/spec/praxis/router_spec.rb +69 -79
  191. data/spec/praxis/routing_config_spec.rb +15 -14
  192. data/spec/praxis/stage_spec.rb +56 -53
  193. data/spec/praxis/trait_spec.rb +17 -18
  194. data/spec/praxis/types/fuzzy_hash_spec.rb +11 -9
  195. data/spec/praxis/types/multipart_array/part_definition_spec.rb +3 -2
  196. data/spec/praxis/types/multipart_array_spec.rb +33 -48
  197. data/spec/spec_app/app/concerns/authenticated.rb +5 -5
  198. data/spec/spec_app/app/concerns/basic_api.rb +3 -1
  199. data/spec/spec_app/app/concerns/log_wrapper.rb +5 -3
  200. data/spec/spec_app/app/controllers/base_class.rb +6 -5
  201. data/spec/spec_app/app/controllers/instances.rb +31 -34
  202. data/spec/spec_app/app/controllers/volumes.rb +6 -6
  203. data/spec/spec_app/app/responses/multipart.rb +1 -2
  204. data/spec/spec_app/app/responses/other_response.rb +2 -2
  205. data/spec/spec_app/config/environment.rb +19 -6
  206. data/spec/spec_app/config.ru +4 -3
  207. data/spec/spec_app/design/api.rb +13 -15
  208. data/spec/spec_app/design/media_types/instance.rb +5 -5
  209. data/spec/spec_app/design/media_types/volume.rb +2 -1
  210. data/spec/spec_app/design/media_types/volume_snapshot.rb +2 -1
  211. data/spec/spec_app/design/resources/instances.rb +9 -15
  212. data/spec/spec_app/design/resources/volume_snapshots.rb +4 -5
  213. data/spec/spec_app/design/resources/volumes.rb +4 -5
  214. data/spec/spec_helper.rb +11 -13
  215. data/spec/support/be_deep_equal_matcher.rb +5 -0
  216. data/spec/support/spec_authorization_plugin.rb +7 -12
  217. data/spec/support/spec_blueprints.rb +2 -1
  218. data/spec/support/spec_complex_authentication_plugin.rb +17 -34
  219. data/spec/support/spec_endpoint_definitions.rb +2 -3
  220. data/spec/support/spec_media_types.rb +28 -35
  221. data/spec/support/spec_resources.rb +20 -18
  222. data/spec/support/spec_simple_authentication_plugin.rb +5 -9
  223. data/tasks/loader.thor +4 -2
  224. data/tasks/thor/app.rb +7 -5
  225. data/tasks/thor/example.rb +23 -22
  226. data/tasks/thor/model.rb +7 -7
  227. data/tasks/thor/scaffold.rb +23 -23
  228. data/tasks/thor/templates/generator/example_app/app/v1/resources/user.rb +0 -8
  229. data/tasks/thor/templates/generator/scaffold/implementation/resources/item.rb +1 -2
  230. metadata +72 -84
  231. data/MAINTAINERS +0 -2
  232. data/TODO.md +0 -25
  233. data/spec/praxis/api_resource_spec.rb +0 -0
  234. data/spec/praxis/dispatcher_spec.rb +0 -0
  235. data/spec/spec_app/app/responses/bulk_response.rb +0 -0
@@ -1,10 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Praxis
2
4
  module Docs
3
5
  module OpenApi
4
6
  class ServerObject
5
7
  # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#server-object
6
8
  attr_reader :url, :description, :variables
7
- def initialize(url: , description: nil, variables: [])
9
+
10
+ def initialize(url:, description: nil, variables: [])
8
11
  @url = url
9
12
  @description = description
10
13
  @variables = variables
@@ -12,7 +15,7 @@ module Praxis
12
15
  end
13
16
 
14
17
  def dump
15
- result = {url: url}
18
+ result = { url: url }
16
19
  result[:description] = description if description
17
20
  result[:variables] = variables unless variables.empty?
18
21
 
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Praxis
2
4
  module Docs
3
5
  module OpenApi
4
6
  class TagObject
5
7
  attr_reader :name, :description
6
- def initialize(name:,description: )
8
+
9
+ def initialize(name:, description:)
7
10
  @name = name
8
11
  @description = description
9
12
  end
@@ -11,8 +14,8 @@ module Praxis
11
14
  def dump
12
15
  {
13
16
  name: name,
14
- description: description,
15
- #externalDocs: ???,
17
+ description: description
18
+ # externalDocs: ???,
16
19
  }
17
20
  end
18
21
  end
@@ -1,42 +1,43 @@
1
- require_relative 'open_api/info_object.rb'
2
- require_relative 'open_api/server_object.rb'
3
- require_relative 'open_api/paths_object.rb'
4
- require_relative 'open_api/tag_object.rb'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'open_api/info_object'
4
+ require_relative 'open_api/server_object'
5
+ require_relative 'open_api/paths_object'
6
+ require_relative 'open_api/tag_object'
5
7
 
6
8
  module Praxis
7
9
  module Docs
8
-
9
10
  class OpenApiGenerator
10
11
  require 'active_support/core_ext/enumerable' # For index_by
11
12
 
12
- API_DOCS_DIRNAME = 'docs/openapi'
13
+ API_DOCS_DIRNAME = 'docs/openapi'
13
14
  EXCLUDED_TYPES_FROM_OUTPUT = Set.new([
14
- Attributor::Boolean,
15
- Attributor::CSV,
16
- Attributor::DateTime,
17
- Attributor::Date,
18
- Attributor::Float,
19
- Attributor::Hash,
20
- Attributor::Ids,
21
- Attributor::Integer,
22
- Attributor::Object,
23
- Attributor::String,
24
- Attributor::Symbol,
25
- Attributor::URI,
26
- ]).freeze
15
+ Attributor::Boolean,
16
+ Attributor::CSV,
17
+ Attributor::DateTime,
18
+ Attributor::Date,
19
+ Attributor::Float,
20
+ Attributor::Hash,
21
+ Attributor::Ids,
22
+ Attributor::Integer,
23
+ Attributor::Object,
24
+ Attributor::String,
25
+ Attributor::Symbol,
26
+ Attributor::URI
27
+ ]).freeze
27
28
 
28
29
  attr_reader :resources_by_version, :types_by_id, :infos_by_version, :doc_root_dir
29
30
 
30
31
  # substitutes ":params_like_so" for {params_like_so}
31
- def self.templatize_url( string )
32
+ def self.templatize_url(string)
32
33
  Mustermann.new(string).to_templates.first
33
34
  end
34
35
 
35
36
  def save!
36
37
  initialize_directories
37
38
  # Restrict the versions listed in the index file to the ones for which we have at least 1 resource
38
- write_index_file( for_versions: resources_by_version.keys )
39
- resources_by_version.keys.each do |version|
39
+ write_index_file(for_versions: resources_by_version.keys)
40
+ resources_by_version.each_key do |version|
40
41
  write_version_file(version)
41
42
  end
42
43
  end
@@ -44,7 +45,7 @@ module Praxis
44
45
  def initialize(root)
45
46
  require 'yaml'
46
47
  @root = root
47
- @resources_by_version = Hash.new do |h,k|
48
+ @resources_by_version = Hash.new do |h, k|
48
49
  h[k] = Set.new
49
50
  end
50
51
 
@@ -60,6 +61,7 @@ module Praxis
60
61
  Praxis::Application.instance.endpoint_definitions.map do |resource|
61
62
  # skip resources with doc_visibility of :none
62
63
  next if resource.metadata[:doc_visibility] == :none
64
+
63
65
  version = resource.version
64
66
  # TODO: it seems that we shouldn't hardcode n/a in Praxis
65
67
  # version = "unversioned" if version == "n/a"
@@ -68,70 +70,70 @@ module Praxis
68
70
  end
69
71
 
70
72
  def collect_types
71
- @types_by_id = ObjectSpace.each_object( Class ).select do |obj|
73
+ @types_by_id = ObjectSpace.each_object(Class).select do |obj|
72
74
  obj < Attributor::Type
73
75
  end.index_by(&:id)
74
76
  end
75
77
 
76
- def write_index_file( for_versions: )
78
+ def write_index_file(for_versions:)
77
79
  # TODO. create a simple html file that can link to the individual versions available
78
80
  end
79
81
 
80
82
  def scan_types_for_version(version, dumped_resources)
81
- found_media_types = resources_by_version[version].select{|r| r.media_type}.collect {|r| r.media_type.describe }
83
+ found_media_types = resources_by_version[version].select(&:media_type).collect { |r| r.media_type.describe }
82
84
 
83
85
  # We'll start by processing the rendered mediatypes
84
- processed_types = Set.new(resources_by_version[version].select do|r|
86
+ processed_types = Set.new(resources_by_version[version].select do |r|
85
87
  r.media_type && !r.media_type.is_a?(Praxis::SimpleMediaType)
86
88
  end.collect(&:media_type))
87
89
 
88
90
  newfound = Set.new
89
91
  found_media_types.each do |mt|
90
- newfound += scan_dump_for_types( { type: mt} , processed_types )
92
+ newfound += scan_dump_for_types({ type: mt }, processed_types)
91
93
  end
92
94
  # Then will process the rendered resources (noting)
93
- newfound += scan_dump_for_types( dumped_resources, Set.new )
95
+ newfound += scan_dump_for_types(dumped_resources, Set.new)
94
96
 
95
97
  # At this point we've done a scan of the dumped resources and mediatypes.
96
98
  # In that scan we've discovered a bunch of types, however, many of those might have appeared in the JSON
97
99
  # rendered in just shallow mode, so it is not guaranteed that we've seen all the available types.
98
100
  # For that we'll do a (non-shallow) dump of all the types we found, and scan them until the scans do not
99
101
  # yield types we haven't seen before
100
- while !newfound.empty? do
102
+ until newfound.empty?
101
103
  dumped = newfound.collect(&:describe)
102
104
  processed_types += newfound
103
- newfound = scan_dump_for_types( dumped, processed_types )
105
+ newfound = scan_dump_for_types(dumped, processed_types)
104
106
  end
105
107
  processed_types
106
108
  end
107
109
 
108
- def scan_dump_for_types( data, processed_types )
110
+ def scan_dump_for_types(data, processed_types)
109
111
  newfound_types = Set.new
110
112
  case data
111
113
  when Array
112
- data.collect{|item| newfound_types += scan_dump_for_types( item , processed_types ) }
114
+ data.collect { |item| newfound_types += scan_dump_for_types(item, processed_types) }
113
115
  when Hash
114
- if data.key?(:type) && data[:type].kind_of?(Hash) && ( [:id,:name,:family] - data[:type].keys ).empty?
116
+ if data.key?(:type) && data[:type].is_a?(Hash) && (%i[id name family] - data[:type].keys).empty?
115
117
  type_id = data[:type][:id]
116
- unless type_id.nil? || type_id == Praxis::SimpleMediaType.id #SimpleTypes shouldn't be collected
118
+ unless type_id.nil? || type_id == Praxis::SimpleMediaType.id # SimpleTypes shouldn't be collected
117
119
  unless types_by_id[type_id]
118
- raise "Error! We have detected a reference to a 'Type' with id='#{type_id}' which is not derived from Attributor::Type" +
119
- " Document generation cannot proceed."
120
+ raise "Error! We have detected a reference to a 'Type' with id='#{type_id}' which is not derived from Attributor::Type" \
121
+ ' Document generation cannot proceed.'
120
122
  end
121
123
  newfound_types << types_by_id[type_id] unless processed_types.include? types_by_id[type_id]
122
124
  end
123
125
  end
124
- data.values.map{|item| newfound_types += scan_dump_for_types( item , processed_types)}
126
+ data.values.map { |item| newfound_types += scan_dump_for_types(item, processed_types) }
125
127
  end
126
128
  newfound_types
127
129
  end
128
130
 
129
- def write_version_file( version )
131
+ def write_version_file(version)
130
132
  # version_info = infos_by_version[version]
131
133
  # # Hack, let's "inherit/copy" all traits of a version from the global definition
132
134
  # # Eventually traits should be defined for a version (and inheritable from global) so we'll emulate that here
133
135
  # version_info[:traits] = infos_by_version[:traits]
134
- dumped_resources = dump_resources( resources_by_version[version] )
136
+ dumped_resources = dump_resources(resources_by_version[version])
135
137
  processed_types = scan_types_for_version(version, dumped_resources)
136
138
 
137
139
  # Here we have:
@@ -140,15 +142,15 @@ module Praxis
140
142
 
141
143
  info_object = OpenApi::InfoObject.new(version: version, api_definition_info: @infos[version])
142
144
  # We only support a server in Praxis ... so we'll use the base path
143
- server_object = OpenApi::ServerObject.new( url: @infos[version].base_path )
144
-
145
- paths_object = OpenApi::PathsObject.new( resources: resources_by_version[version])
145
+ server_object = OpenApi::ServerObject.new(url: @infos[version].base_path)
146
+
147
+ paths_object = OpenApi::PathsObject.new(resources: resources_by_version[version])
146
148
 
147
149
  full_data = {
148
- openapi: "3.0.2",
150
+ openapi: '3.0.2',
149
151
  info: info_object.dump,
150
152
  servers: [server_object.dump],
151
- paths: paths_object.dump,
153
+ paths: paths_object.dump
152
154
  # responses: {}, #TODO!! what do we get here? the templates?...need to transform to "Responses Definitions Object"
153
155
  # securityDefinitions: {}, # NOTE: No security definitions in Praxis
154
156
  # security: [], # NOTE: No security definitions in Praxis
@@ -157,29 +159,25 @@ module Praxis
157
159
  # Create the top level tags by:
158
160
  # 1- First adding all the resource display names (and descriptions)
159
161
  tags_for_resources = resources_by_version[version].collect do |resource|
160
- OpenApi::TagObject.new(name: resource.display_name, description: resource.description ).dump
162
+ OpenApi::TagObject.new(name: resource.display_name, description: resource.description).dump
161
163
  end
162
164
  full_data[:tags] = tags_for_resources
163
165
  # 2- Then adding all of the top level traits but marking them special with the x-traitTag (of Redoc)
164
- tags_for_traits = (ApiDefinition.instance.traits).collect do |name, info|
165
- OpenApi::TagObject.new(name: name, description: info.description).dump.merge(:'x-traitTag' => true)
166
- end
167
- unless tags_for_traits.empty?
168
- full_data[:tags] = full_data[:tags] + tags_for_traits
166
+ tags_for_traits = ApiDefinition.instance.traits.collect do |name, info|
167
+ OpenApi::TagObject.new(name: name, description: info.description).dump.merge('x-traitTag': true)
169
168
  end
169
+ full_data[:tags] = full_data[:tags] + tags_for_traits unless tags_for_traits.empty?
170
170
 
171
171
  # Include only MTs (i.e., not custom types or simple types...)
172
- component_schemas = reusable_schema_objects(processed_types.select{|t| t < Praxis::MediaType})
173
-
172
+ component_schemas = reusable_schema_objects(processed_types.select { |t| t < Praxis::MediaType })
173
+
174
174
  # 3- Then adding all of the top level Mediatypes...so we can present them at the bottom, otherwise they don't show
175
- tags_for_mts = component_schemas.map do |(name, info)|
175
+ tags_for_mts = component_schemas.map do |(name, _info)|
176
176
  special_redoc_anchor = "<SchemaDefinition schemaRef=\"#/components/schemas/#{name}\" showReadOnly={true} showWriteOnly={true} />"
177
177
  guessed_display = name.split('-').last # TODO!!!the informational hash does not seem to come with the "description" value set...hmm
178
- OpenApi::TagObject.new(name: name, description: special_redoc_anchor).dump.merge(:'x-displayName' => guessed_display)
179
- end
180
- unless tags_for_mts.empty?
181
- full_data[:tags] = full_data[:tags] + tags_for_mts
178
+ OpenApi::TagObject.new(name: name, description: special_redoc_anchor).dump.merge('x-displayName': guessed_display)
182
179
  end
180
+ full_data[:tags] = full_data[:tags] + tags_for_mts unless tags_for_mts.empty?
183
181
 
184
182
  # Include all the reusable schemas in the components hash
185
183
  full_data[:components] = {
@@ -187,25 +185,25 @@ module Praxis
187
185
  }
188
186
 
189
187
  # REDOC specific grouping of sidebar
190
- resource_tags = { name: 'Resources', tags: tags_for_resources.map{|t| t[:name]} }
191
- schema_tags = { name: 'Models', tags: tags_for_mts.map{|t| t[:name]} }
188
+ resource_tags = { name: 'Resources', tags: tags_for_resources.map { |t| t[:name] } }
189
+ schema_tags = { name: 'Models', tags: tags_for_mts.map { |t| t[:name] } }
192
190
  full_data['x-tagGroups'] = [resource_tags, schema_tags]
193
191
 
194
192
  # if parameter_object = convert_to_parameter_object( version_info[:info][:base_params] )
195
193
  # full_data[:parameters] = parameter_object
196
194
  # end
197
- #puts JSON.pretty_generate( full_data )
195
+ # puts JSON.pretty_generate( full_data )
198
196
  # Write the file
199
- version_file = ( version == "n/a" ? "unversioned" : version )
197
+ version_file = (version == 'n/a' ? 'unversioned' : version)
200
198
  filename = File.join(doc_root_dir, version_file, 'openapi')
201
199
 
202
200
  puts "Generating Open API file : #{filename} (json and yml) "
203
201
  json_data = JSON.pretty_generate(full_data)
204
- File.open(filename+".json", 'w') {|f| f.write(json_data)}
205
- converted_full_data = JSON.parse( json_data ) # So symbols disappear
206
- File.open(filename+".yml", 'w') {|f| f.write(YAML.dump(converted_full_data))}
202
+ File.open("#{filename}.json", 'w') { |f| f.write(json_data) }
203
+ converted_full_data = JSON.parse(json_data) # So symbols disappear
204
+ File.open("#{filename}.yml", 'w') { |f| f.write(YAML.dump(converted_full_data)) }
207
205
 
208
- html =<<-EOB
206
+ html = <<-HTML
209
207
  <!DOCTYPE html>
210
208
  <html>
211
209
  <head>
@@ -214,7 +212,7 @@ module Praxis
214
212
  <meta charset="utf-8"/>
215
213
  <meta name="viewport" content="width=device-width, initial-scale=1">
216
214
  <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
217
-
215
+
218
216
  <!--
219
217
  ReDoc doesn't change outer page styles
220
218
  -->
@@ -230,7 +228,7 @@ module Praxis
230
228
  <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
231
229
  </body>
232
230
  </html>
233
- EOB
231
+ HTML
234
232
  html_file = File.join(doc_root_dir, version_file, 'index.html')
235
233
  File.write(html_file, html)
236
234
  end
@@ -239,18 +237,18 @@ module Praxis
239
237
  @doc_root_dir = File.join(@root, API_DOCS_DIRNAME)
240
238
 
241
239
  # remove previous data (and reset the directory)
242
- FileUtils.rm_rf @doc_root_dir if File.exists?(@doc_root_dir)
243
- FileUtils.mkdir_p @doc_root_dir unless File.exists? @doc_root_dir
244
- resources_by_version.keys.each do |version|
245
- FileUtils.mkdir_p @doc_root_dir + '/' + version
240
+ FileUtils.rm_rf @doc_root_dir if File.exist?(@doc_root_dir)
241
+ FileUtils.mkdir_p @doc_root_dir unless File.exist? @doc_root_dir
242
+ resources_by_version.each_key do |version|
243
+ FileUtils.mkdir_p "#{@doc_root_dir}/#{version}"
246
244
  end
247
- FileUtils.mkdir_p @doc_root_dir + '/unversioned' if resources_by_version.keys.include?('n/a')
245
+ FileUtils.mkdir_p "#{@doc_root_dir}/unversioned" if resources_by_version.keys.include?('n/a')
248
246
  end
249
247
 
250
- def normalize_media_types( mtis )
248
+ def normalize_media_types(mtis)
251
249
  mtis.collect do |mti|
252
- MediaTypeIdentifier.load(mti).to_s
253
- end
250
+ MediaTypeIdentifier.load(mti).to_s
251
+ end
254
252
  end
255
253
 
256
254
  def reusable_schema_objects(types)
@@ -265,33 +263,33 @@ module Praxis
265
263
  end
266
264
  end
267
265
 
268
- def convert_to_parameter_object( params )
266
+ def convert_to_parameter_object(params)
269
267
  # TODO!! actually convert each of them
270
- puts "TODO! convert to parameter object"
268
+ puts 'TODO! convert to parameter object'
271
269
  params
272
270
  end
273
271
 
274
- def convert_traits_to_tags( traits )
272
+ def convert_traits_to_tags(traits)
275
273
  traits.collect do |name, info|
276
274
  { name: name, description: info[:description] }
277
275
  end
278
276
  end
279
277
 
280
-
281
- def dump_responses_object( responses )
282
- responses.each_with_object({}) do |(name, info), hash|
283
- data = { description: info[:description] || "" }
284
- if payload = info[:payload]
285
- body_type= payload[:id]
278
+ def dump_responses_object(responses)
279
+ responses.each_with_object({}) do |(_name, info), hash|
280
+ data = { description: info[:description] || '' }
281
+ if (payload = info[:payload])
282
+ body_type = payload[:id]
286
283
  raise "WAIT! response payload doesn't have an existing id for the schema!!! (do an if, and describe it if so)" unless body_type
287
- data[:schema] = {"$ref" => "#/definitions/#{body_type}" }
284
+
285
+ data[:schema] = { '$ref' => "#/definitions/#{body_type}" }
288
286
  end
289
287
 
290
- # data[:schema] = ???TODO!!
291
- if headers_object = dump_response_headers_object( info[:headers] )
288
+ # data[:schema] = ???TODO!!
289
+ if (headers_object = dump_response_headers_object(info[:headers]))
292
290
  data[:headers] = headers_object
293
291
  end
294
- if info[:payload] && ( examples_object = dump_response_examples_object( info[:payload][:examples] ) )
292
+ if info[:payload] && (examples_object = dump_response_examples_object(info[:payload][:examples]))
295
293
  data[:examples] = examples_object
296
294
  end
297
295
  hash[info[:status]] = data
@@ -305,17 +303,17 @@ module Praxis
305
303
  # end
306
304
  # end
307
305
 
308
- def dump_response_examples_object( examples )
309
- examples.each_with_object({}) do |(name, info), hash|
306
+ def dump_response_examples_object(examples)
307
+ examples.each_with_object({}) do |(_name, info), hash|
310
308
  hash[info[:content_type]] = info[:body]
311
309
  end
312
310
  end
313
311
 
314
-
315
- def dump_resources( resources )
312
+ def dump_resources(resources)
316
313
  resources.each_with_object({}) do |r, hash|
317
314
  # Do not report undocumentable resources
318
315
  next if r.metadata[:doc_visibility] == :none
316
+
319
317
  context = [r.id]
320
318
  resource_description = r.describe(context: context)
321
319
 
@@ -328,13 +326,12 @@ module Praxis
328
326
  # skip actions with doc_visibility of :none
329
327
  next if action.metadata[:doc_visibility] == :none
330
328
 
331
- action_description = resource_description[:actions].find {|a| a[:name] == action_name }
329
+ resource_description[:actions].find { |a| a[:name] == action_name }
332
330
  end
333
331
 
334
332
  hash[r.id] = resource_description
335
333
  end
336
334
  end
337
-
338
335
  end
339
336
  end
340
337
  end