apipie-rails 0.5.19 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +31 -0
  3. data/.github/workflows/rubocop-challenger.yml +28 -0
  4. data/.github/workflows/rubocop.yml +18 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +128 -0
  7. data/.rubocop_todo.yml +2056 -0
  8. data/.vscode/settings.json +3 -0
  9. data/CHANGELOG.md +161 -0
  10. data/Gemfile +20 -0
  11. data/README.rst +117 -23
  12. data/Rakefile +0 -5
  13. data/apipie-rails.gemspec +18 -9
  14. data/app/controllers/apipie/apipies_controller.rb +14 -29
  15. data/app/helpers/apipie_helper.rb +1 -1
  16. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
  17. data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
  18. data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
  19. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
  20. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
  21. data/app/views/apipie/apipies/_deprecation.html.erb +16 -0
  22. data/app/views/apipie/apipies/_params.html.erb +7 -1
  23. data/config/locales/en.yml +8 -0
  24. data/config/locales/ko.yml +31 -0
  25. data/gemfiles/Gemfile.tools +9 -0
  26. data/lib/apipie/apipie_module.rb +7 -7
  27. data/lib/apipie/application.rb +132 -97
  28. data/lib/apipie/configuration.rb +43 -33
  29. data/lib/apipie/dsl_definition.rb +44 -33
  30. data/lib/apipie/error_description.rb +3 -3
  31. data/lib/apipie/errors.rb +17 -17
  32. data/lib/apipie/extractor/collector.rb +5 -6
  33. data/lib/apipie/extractor/recorder.rb +35 -8
  34. data/lib/apipie/extractor/writer.rb +15 -15
  35. data/lib/apipie/extractor.rb +6 -9
  36. data/lib/apipie/generator/config.rb +12 -0
  37. data/lib/apipie/generator/generator.rb +2 -0
  38. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  39. data/lib/apipie/generator/swagger/config.rb +80 -0
  40. data/lib/apipie/generator/swagger/context.rb +38 -0
  41. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  42. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +89 -0
  43. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  44. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  45. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  46. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  47. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  48. data/lib/apipie/generator/swagger/operation_id.rb +51 -0
  49. data/lib/apipie/generator/swagger/param_description/builder.rb +114 -0
  50. data/lib/apipie/generator/swagger/param_description/composite.rb +119 -0
  51. data/lib/apipie/generator/swagger/param_description/description.rb +15 -0
  52. data/lib/apipie/generator/swagger/param_description/in.rb +37 -0
  53. data/lib/apipie/generator/swagger/param_description/name.rb +18 -0
  54. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  55. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  56. data/lib/apipie/generator/swagger/param_description/type.rb +128 -0
  57. data/lib/apipie/generator/swagger/param_description.rb +18 -0
  58. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  59. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  60. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  61. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  62. data/lib/apipie/generator/swagger/schema.rb +63 -0
  63. data/lib/apipie/generator/swagger/swagger.rb +2 -0
  64. data/lib/apipie/generator/swagger/type.rb +16 -0
  65. data/lib/apipie/generator/swagger/type_extractor.rb +51 -0
  66. data/lib/apipie/generator/swagger/warning.rb +74 -0
  67. data/lib/apipie/generator/swagger/warning_writer.rb +54 -0
  68. data/lib/apipie/helpers.rb +3 -3
  69. data/lib/apipie/markup.rb +9 -8
  70. data/lib/apipie/method_description/api.rb +12 -0
  71. data/lib/apipie/method_description/apis_service.rb +82 -0
  72. data/lib/apipie/method_description.rb +12 -56
  73. data/lib/apipie/param_description/deprecation.rb +24 -0
  74. data/lib/apipie/param_description.rb +57 -24
  75. data/lib/apipie/resource_description.rb +42 -14
  76. data/lib/apipie/response_description.rb +3 -3
  77. data/lib/apipie/response_description_adapter.rb +12 -10
  78. data/lib/apipie/routing.rb +1 -1
  79. data/lib/apipie/rspec/response_validation_helper.rb +3 -3
  80. data/lib/apipie/static_dispatcher.rb +10 -2
  81. data/lib/apipie/swagger_generator.rb +28 -691
  82. data/lib/apipie/validator.rb +41 -11
  83. data/lib/apipie/version.rb +1 -1
  84. data/lib/apipie-rails.rb +36 -5
  85. data/lib/generators/apipie/install/install_generator.rb +1 -1
  86. data/lib/generators/apipie/views_generator.rb +1 -1
  87. data/lib/tasks/apipie.rake +37 -32
  88. data/rel-eng/gem_release.ipynb +41 -9
  89. data/spec/controllers/api/v2/architectures_controller_spec.rb +10 -3
  90. data/spec/controllers/api/v2/empty_middle_controller_spec.rb +23 -0
  91. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +18 -2
  92. data/spec/controllers/api/v2/sub/footguns_controller_spec.rb +19 -0
  93. data/spec/controllers/included_param_group_controller_spec.rb +13 -0
  94. data/spec/{lib/swagger/response_validation_spec.rb → controllers/pets_controller_spec.rb} +26 -32
  95. data/spec/controllers/users_controller_spec.rb +47 -6
  96. data/spec/dummy/Rakefile +1 -1
  97. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +2 -1
  98. data/spec/dummy/app/controllers/api/v2/base_controller.rb +6 -0
  99. data/spec/dummy/app/controllers/api/v2/empty_middle_controller.rb +14 -0
  100. data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +2 -2
  101. data/spec/dummy/app/controllers/api/v2/sub/footguns_controller.rb +30 -0
  102. data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
  103. data/spec/dummy/app/controllers/{concerns/extending_concern.rb → extending_concern.rb} +0 -2
  104. data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
  105. data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
  106. data/spec/dummy/app/controllers/pets_controller.rb +5 -5
  107. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  108. data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +0 -2
  109. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  110. data/spec/dummy/app/controllers/users_controller.rb +17 -5
  111. data/spec/dummy/app/helpers/random_param_group.rb +8 -0
  112. data/spec/dummy/components/test_engine/test_engine.gemspec +1 -1
  113. data/spec/dummy/config/application.rb +2 -5
  114. data/spec/dummy/config/boot.rb +2 -2
  115. data/spec/dummy/config/environment.rb +1 -1
  116. data/spec/dummy/config/environments/development.rb +0 -3
  117. data/spec/dummy/config/environments/production.rb +0 -3
  118. data/spec/dummy/config/environments/test.rb +0 -5
  119. data/spec/dummy/config/initializers/apipie.rb +2 -2
  120. data/spec/dummy/config/routes.rb +8 -0
  121. data/spec/dummy/config.ru +1 -1
  122. data/spec/dummy/script/rails +2 -2
  123. data/spec/{controllers → lib/apipie}/apipies_controller_spec.rb +95 -23
  124. data/spec/lib/apipie/application_spec.rb +62 -0
  125. data/spec/lib/apipie/configuration_spec.rb +38 -0
  126. data/spec/lib/apipie/extractor/collector_spec.rb +57 -0
  127. data/spec/lib/apipie/extractor/recorder_spec.rb +77 -0
  128. data/spec/lib/{extractor → apipie/extractor}/writer_spec.rb +8 -6
  129. data/spec/lib/{extractor → apipie}/extractor_spec.rb +1 -1
  130. data/spec/lib/{file_handler_spec.rb → apipie/file_handler_spec.rb} +7 -0
  131. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  132. data/spec/lib/apipie/generator/swagger/context_spec.rb +56 -0
  133. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +119 -0
  134. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  135. data/spec/lib/apipie/generator/swagger/operation_id_spec.rb +63 -0
  136. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +215 -0
  137. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +95 -0
  138. data/spec/lib/apipie/generator/swagger/param_description/description_spec.rb +79 -0
  139. data/spec/lib/apipie/generator/swagger/param_description/in_spec.rb +86 -0
  140. data/spec/lib/apipie/generator/swagger/param_description/name_spec.rb +81 -0
  141. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +183 -0
  142. data/spec/lib/apipie/generator/swagger/param_description_spec.rb +28 -0
  143. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  144. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  145. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  146. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  147. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  148. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +38 -0
  149. data/spec/lib/apipie/generator/swagger/warning_spec.rb +51 -0
  150. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +71 -0
  151. data/spec/lib/apipie/method_description/apis_service_spec.rb +60 -0
  152. data/spec/lib/apipie/method_description_spec.rb +133 -0
  153. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  154. data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
  155. data/spec/lib/{param_description_spec.rb → apipie/param_description_spec.rb} +332 -6
  156. data/spec/lib/{param_group_spec.rb → apipie/param_group_spec.rb} +6 -5
  157. data/spec/lib/apipie/resource_description_spec.rb +91 -0
  158. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  159. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  160. data/spec/lib/{validator_spec.rb → apipie/validator_spec.rb} +48 -12
  161. data/spec/lib/rake_spec.rb +3 -5
  162. data/spec/lib/swagger/openapi_2_0_schema.json +8 -1
  163. data/spec/lib/swagger/rake_swagger_spec.rb +30 -10
  164. data/spec/lib/swagger/swagger_dsl_spec.rb +18 -12
  165. data/spec/lib/validators/array_validator_spec.rb +1 -1
  166. data/spec/spec_helper.rb +10 -32
  167. data/spec/support/custom_bool_validator.rb +17 -0
  168. data/spec/{controllers → test_engine}/memes_controller_spec.rb +1 -1
  169. metadata +173 -125
  170. data/.travis.yml +0 -41
  171. data/Gemfile +0 -1
  172. data/Gemfile.rails41 +0 -7
  173. data/Gemfile.rails42 +0 -14
  174. data/Gemfile.rails50 +0 -9
  175. data/Gemfile.rails51 +0 -9
  176. data/Gemfile.rails60 +0 -10
  177. data/Gemfile.rails61 +0 -10
  178. data/spec/lib/application_spec.rb +0 -49
  179. data/spec/lib/method_description_spec.rb +0 -98
  180. data/spec/lib/resource_description_spec.rb +0 -48
  181. /data/spec/lib/{extractor → apipie/extractor/recorder}/middleware_spec.rb +0 -0
@@ -0,0 +1,63 @@
1
+ class Apipie::Generator::Swagger::Schema
2
+ # @param [Array<Apipie::ResourceDescription>] resource_descriptions
3
+ def initialize(resource_descriptions, version:, language:, clear_warnings:)
4
+ @resource_descriptions = resource_descriptions
5
+ @language = language
6
+ @clear_warnings = clear_warnings
7
+ @swagger = {
8
+ swagger: '2.0',
9
+ info: {
10
+ title: Apipie.configuration.app_name.to_s,
11
+ description: "#{Apipie.app_info(version, @language)}#{Apipie.configuration.copyright}",
12
+ version: version.to_s,
13
+ 'x-copyright': Apipie.configuration.copyright
14
+ },
15
+ basePath: Apipie.api_base_url(version),
16
+ consumes: [],
17
+ paths: {},
18
+ definitions: {},
19
+ schemes: Apipie.configuration.generator.swagger.schemes,
20
+ tags: [],
21
+ securityDefinitions: Apipie.configuration.generator.swagger.security_definitions,
22
+ security: Apipie.configuration.generator.swagger.global_security
23
+ }
24
+ end
25
+
26
+ def generate
27
+ if Apipie.configuration.generator.swagger.api_host.present?
28
+ @swagger[:host] = Apipie.configuration.generator.swagger.api_host
29
+ end
30
+
31
+ if Apipie.configuration.generator.swagger.content_type_input == :json
32
+ @swagger[:consumes] = ['application/json']
33
+ @swagger[:info][:title] += ' (params in:body)'
34
+ else
35
+ @swagger[:consumes] = ['application/x-www-form-urlencoded', 'multipart/form-data']
36
+ @swagger[:info][:title] += ' (params in:formData)'
37
+ end
38
+
39
+ if @clear_warnings
40
+ Apipie::Generator::Swagger::WarningWriter.instance.clear!
41
+ end
42
+
43
+ @swagger.merge!(tags_and_paths)
44
+
45
+ if Apipie.configuration.generator.swagger.generate_x_computed_id_field?
46
+ @swagger[:info]['x-computed-id'] =
47
+ Apipie::Generator::Swagger::ComputedInterfaceId.instance.id
48
+ end
49
+
50
+ @swagger[:definitions] =
51
+ Apipie::Generator::Swagger::ReferencedDefinitions.instance.definitions
52
+
53
+ @swagger
54
+ end
55
+
56
+ private
57
+
58
+ def tags_and_paths
59
+ Apipie::Generator::Swagger::ResourceDescriptionComposite
60
+ .new(@resource_descriptions, language: @language)
61
+ .to_swagger
62
+ end
63
+ end
@@ -0,0 +1,2 @@
1
+ module Apipie::Generator::Swagger
2
+ end
@@ -0,0 +1,16 @@
1
+ class Apipie::Generator::Swagger::Type
2
+ attr_reader :str_format
3
+
4
+ def initialize(type, str_format = nil)
5
+ @type = type
6
+ @str_format = str_format
7
+ end
8
+
9
+ def to_s
10
+ @type
11
+ end
12
+
13
+ def ==(other)
14
+ other.to_s == self.to_s
15
+ end
16
+ end
@@ -0,0 +1,51 @@
1
+ class Apipie::Generator::Swagger::TypeExtractor
2
+ TYPES = {
3
+ numeric: 'number',
4
+ hash: 'object',
5
+ array: 'array',
6
+ enum: 'enum',
7
+
8
+ # see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
9
+ integer: Apipie::Generator::Swagger::Type.new('integer', 'int32'),
10
+ long: Apipie::Generator::Swagger::Type.new('integer', 'int64'),
11
+ number: Apipie::Generator::Swagger::Type.new('number'),
12
+ float: Apipie::Generator::Swagger::Type.new('number', 'float'),
13
+ double: Apipie::Generator::Swagger::Type.new('number', 'double'),
14
+ string: Apipie::Generator::Swagger::Type.new('string'),
15
+ byte: Apipie::Generator::Swagger::Type.new('string', 'byte'),
16
+ binary: Apipie::Generator::Swagger::Type.new('string', 'binary'),
17
+ boolean: Apipie::Generator::Swagger::Type.new('boolean'),
18
+ date: Apipie::Generator::Swagger::Type.new('string', 'date'),
19
+ dateTime: Apipie::Generator::Swagger::Type.new('string', 'date-time'),
20
+ password: Apipie::Generator::Swagger::Type.new('string', 'password')
21
+ }
22
+
23
+ # @param [Apipie::Validator::BaseValidator, ResponseDescriptionAdapter::PropDesc::Validator, nil] validator
24
+ def initialize(validator)
25
+ @validator = validator
26
+ end
27
+
28
+ def extract
29
+ expected_type =
30
+ if string?
31
+ :string
32
+ elsif enum?
33
+ :enum
34
+ else
35
+ @validator.expected_type.to_sym
36
+ end
37
+
38
+ TYPES[expected_type] || @validator.expected_type
39
+ end
40
+
41
+ private
42
+
43
+ def string?
44
+ @validator.blank?
45
+ end
46
+
47
+ def enum?
48
+ @validator.is_a?(Apipie::Validator::EnumValidator) ||
49
+ (@validator.respond_to?(:is_enum?) && @validator.is_enum?)
50
+ end
51
+ end
@@ -0,0 +1,74 @@
1
+ class Apipie::Generator::Swagger::Warning
2
+ MISSING_METHOD_SUMMARY_CODE = 100
3
+ ADDED_MISSING_SLASH_CODE = 101
4
+ NO_RETURN_CODES_SPECIFIED_CODE = 102
5
+ HASH_WITHOUT_INTERNAL_TYPESPEC_CODE = 103
6
+ OPTIONAL_PARAM_IN_PATH_CODE = 104
7
+ OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE = 105
8
+ PARAM_IGNORED_IN_FORM_DATA_CODE = 106
9
+ PATH_PARAM_NOT_DESCRIBED_CODE = 107
10
+
11
+ CODES = {
12
+ missing_method_summary: MISSING_METHOD_SUMMARY_CODE,
13
+ added_missing_slash: ADDED_MISSING_SLASH_CODE,
14
+ no_return_codes_specified: NO_RETURN_CODES_SPECIFIED_CODE,
15
+ hash_without_internal_typespec: HASH_WITHOUT_INTERNAL_TYPESPEC_CODE,
16
+ optional_param_in_path: OPTIONAL_PARAM_IN_PATH_CODE,
17
+ optional_without_default_value: OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE,
18
+ param_ignored_in_form_data: PARAM_IGNORED_IN_FORM_DATA_CODE,
19
+ path_param_not_described_code: PATH_PARAM_NOT_DESCRIBED_CODE
20
+ }
21
+
22
+ MESSAGES = {
23
+ MISSING_METHOD_SUMMARY_CODE => "Missing short description for method",
24
+ ADDED_MISSING_SLASH_CODE => "Added missing / at beginning of path: %{path}",
25
+ HASH_WITHOUT_INTERNAL_TYPESPEC_CODE => "The parameter :%{parameter} is a generic Hash without an internal type specification",
26
+ NO_RETURN_CODES_SPECIFIED_CODE => "No return codes ('errors') specified",
27
+ OPTIONAL_PARAM_IN_PATH_CODE => "The parameter :%{parameter} is 'in-path'. Ignoring 'not required' in DSL",
28
+ OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE => "The parameter :%{parameter} is optional but default value is not specified (use :default_value => ...)",
29
+ PARAM_IGNORED_IN_FORM_DATA_CODE => "Ignoring param :%{parameter} -- cannot include Hash without fields in a formData specification",
30
+ PATH_PARAM_NOT_DESCRIBED_CODE => "The parameter :%{name} appears in the path %{path} but is not described"
31
+ }
32
+
33
+ attr_reader :code
34
+
35
+ def initialize(code, info_message, method_id)
36
+ @code = code
37
+ @info_message = info_message
38
+ @method_id = method_id
39
+ end
40
+
41
+ def id
42
+ "#{@method_id}#{@code}#{@info_message}"
43
+ end
44
+
45
+ def warning_message
46
+ "WARNING (#{@code}): [#{@method_id}] -- #{@info_message}\n"
47
+ end
48
+
49
+ def warn
50
+ Warning.warn(warning_message)
51
+ end
52
+
53
+ def warn_through_writer
54
+ Apipie::Generator::Swagger::WarningWriter.instance.warn(self)
55
+ end
56
+
57
+ # @param [Integer] code
58
+ # @param [Hash] message_attributes
59
+ #
60
+ # @return [Apipie::Generator::Swagger::Warning]
61
+ def self.for_code(code, method_id, message_attributes = {})
62
+ if !CODES.value?(code)
63
+ raise ArgumentError, 'Unknown warning code'
64
+ end
65
+
66
+ info_message = if message_attributes.present?
67
+ self::MESSAGES[code] % message_attributes
68
+ else
69
+ self::MESSAGES[code]
70
+ end
71
+
72
+ Apipie::Generator::Swagger::Warning.new(code, info_message, method_id)
73
+ end
74
+ end
@@ -0,0 +1,54 @@
1
+ class Apipie::Generator::Swagger::WarningWriter
2
+ include Singleton
3
+
4
+ def initialize
5
+ @issued_warnings = []
6
+ end
7
+
8
+ # @param [Apipie::Generator::Swagger::Warning] warning
9
+ def warn(warning)
10
+ return if muted_warning?(warning)
11
+
12
+ warning.warn
13
+
14
+ @issued_warnings << warning.id
15
+ end
16
+
17
+ def issued_warnings?
18
+ @issued_warnings.count > 0
19
+ end
20
+
21
+ def clear!
22
+ @issued_warnings = []
23
+
24
+ self
25
+ end
26
+
27
+ private
28
+
29
+ # @param [Apipie::Generator::Swagger::Warning] warning
30
+ #
31
+ # @return [TrueClass, FalseClass]
32
+ def muted_warning?(warning)
33
+ @issued_warnings.include?(warning.id) ||
34
+ suppressed_warning?(warning.code) ||
35
+ suppress_warnings?
36
+ end
37
+
38
+ # @param [Integer] warning_number
39
+ #
40
+ # @return [TrueClass, FalseClass]
41
+ def suppressed_warning?(warning_number)
42
+ suppress_warnings_config.is_a?(Array) && suppress_warnings_config.include?(warning_number)
43
+ end
44
+
45
+ # @return [TrueClass, FalseClass]
46
+ def suppress_warnings?
47
+ suppress_warnings_config == true
48
+ end
49
+
50
+ # @return [FalseClass, TrueClass, Array]
51
+ def suppress_warnings_config
52
+ Apipie.configuration.generator.swagger.suppress_warnings
53
+ end
54
+ end
@@ -25,10 +25,10 @@ module Apipie
25
25
  @url_prefix << request_script_name
26
26
  @url_prefix << Apipie.configuration.doc_base_url
27
27
  end
28
- path = path.sub(/^\//,"")
28
+ path = path.sub(%r{^/},"")
29
29
  ret = "#{@url_prefix}/#{path}"
30
- ret.insert(0,"/") unless ret =~ /\A[.\/]/
31
- ret.sub!(/\/*\Z/,"")
30
+ ret.insert(0,"/") unless ret =~ %r{\A[./]}
31
+ ret.sub!(%r{/*\Z},"")
32
32
  ret
33
33
  end
34
34
 
data/lib/apipie/markup.rb CHANGED
@@ -4,6 +4,11 @@ module Apipie
4
4
 
5
5
  class RDoc
6
6
 
7
+ def initialize
8
+ require 'rdoc'
9
+ require 'rdoc/markup/to_html'
10
+ end
11
+
7
12
  def to_html(text)
8
13
  rdoc.convert(text)
9
14
  end
@@ -11,14 +16,10 @@ module Apipie
11
16
  private
12
17
 
13
18
  def rdoc
14
- @rdoc ||= begin
15
- require 'rdoc'
16
- require 'rdoc/markup/to_html'
17
- if Gem::Version.new(::RDoc::VERSION) < Gem::Version.new('4.0.0')
18
- ::RDoc::Markup::ToHtml.new()
19
- else
20
- ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
21
- end
19
+ if Gem::Version.new(::RDoc::VERSION) < Gem::Version.new('4.0.0')
20
+ ::RDoc::Markup::ToHtml.new()
21
+ else
22
+ ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
22
23
  end
23
24
  end
24
25
  end
@@ -0,0 +1,12 @@
1
+ class Apipie::MethodDescription::Api
2
+ attr_accessor :short_description, :path, :http_method, :from_routes,
3
+ :options, :returns
4
+
5
+ def initialize(method, path, desc, options)
6
+ @http_method = method.to_s
7
+ @path = path
8
+ @short_description = desc
9
+ @from_routes = options[:from_routes]
10
+ @options = options
11
+ end
12
+ end
@@ -0,0 +1,82 @@
1
+ # Service that builds the appropriate Apipie::MethodDescription::Api
2
+ # required by Apipie::MethodDescription
3
+ #
4
+ class Apipie::MethodDescription::ApisService
5
+ # @param [Apipie::ResourceDescription] resource
6
+ # @param [Symbol] controller_action
7
+ # @param [Hash] dsl
8
+ def initialize(resource, controller_action, dsl)
9
+ @resource = resource
10
+ @controller_action = controller_action
11
+ @dsl = dsl
12
+ end
13
+
14
+ # @return [Array<Apipie::MethodDescription::Api>]
15
+ def call
16
+ api_args.map do |http_method, path, desc, opts|
17
+ Apipie::MethodDescription::Api.new(
18
+ http_method,
19
+ concern_subst(path),
20
+ concern_subst(desc),
21
+ opts
22
+ )
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def concern_subst(path)
29
+ return if path.blank?
30
+
31
+ if from_concern?
32
+ @resource.controller._apipie_perform_concern_subst(path)
33
+ else
34
+ path
35
+ end
36
+ end
37
+
38
+ # @return [Array<Array>]
39
+ def api_args
40
+ return @dsl[:api_args] if !api_from_routes?
41
+
42
+ api_args = @dsl[:api_args].dup
43
+
44
+ api_args_from_routes = routes.map do |route_info|
45
+ [
46
+ route_info[:verb],
47
+ route_info[:path],
48
+ route_info[:desc],
49
+ (route_info[:options] || {}).merge(from_routes: true)
50
+ ]
51
+ end
52
+
53
+ api_args.concat(api_args_from_routes)
54
+ end
55
+
56
+ def api_from_routes?
57
+ @dsl[:api_from_routes].present?
58
+ end
59
+
60
+ def from_concern?
61
+ @dsl[:from_concern] == true
62
+ end
63
+
64
+ def description
65
+ @dsl[:api_from_routes][:desc]
66
+ end
67
+
68
+ def options
69
+ @dsl[:api_from_routes][:options]
70
+ end
71
+
72
+ # @return [Array<Hash>]
73
+ def routes
74
+ Apipie.routes_for_action(
75
+ @resource.controller,
76
+ @controller_action,
77
+ { desc: description, options: options }
78
+ )
79
+ end
80
+ end
81
+
82
+
@@ -1,22 +1,5 @@
1
- require 'set'
2
1
  module Apipie
3
-
4
2
  class MethodDescription
5
-
6
- class Api
7
-
8
- attr_accessor :short_description, :path, :http_method, :from_routes, :options, :returns
9
-
10
- def initialize(method, path, desc, options)
11
- @http_method = method.to_s
12
- @path = path
13
- @short_description = desc
14
- @from_routes = options[:from_routes]
15
- @options = options
16
- end
17
-
18
- end
19
-
20
3
  attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :headers, :show
21
4
  attr_accessor :metadata
22
5
 
@@ -24,12 +7,9 @@ module Apipie
24
7
  @method = method.to_s
25
8
  @resource = resource
26
9
  @from_concern = dsl_data[:from_concern]
27
- @apis = api_data(dsl_data).map do |mthd, path, desc, opts|
28
- MethodDescription::Api.new(mthd, concern_subst(path), concern_subst(desc), opts)
29
- end
10
+ @apis = ApisService.new(resource, method, dsl_data).call
30
11
 
31
- desc = dsl_data[:description] || ''
32
- @full_description = Apipie.markup_to_html(desc)
12
+ @full_description = dsl_data[:description] || ''
33
13
 
34
14
  @errors = dsl_data[:errors].map do |args|
35
15
  Apipie::ErrorDescription.from_dsl_data(args)
@@ -53,12 +33,12 @@ module Apipie
53
33
 
54
34
  @params_ordered = dsl_data[:params].map do |args|
55
35
  Apipie::ParamDescription.from_dsl_data(self, args)
56
- end.reject{|p| p.response_only? }
36
+ end.reject(&:response_only?)
57
37
 
58
38
  @params_ordered = ParamDescription.unify(@params_ordered)
59
39
  @headers = dsl_data[:headers]
60
40
 
61
- @show = if dsl_data.has_key? :show
41
+ @show = if dsl_data.key? :show
62
42
  dsl_data[:show]
63
43
  else
64
44
  true
@@ -102,7 +82,7 @@ module Apipie
102
82
  parent = Apipie.get_resource_description(@resource.controller.superclass)
103
83
 
104
84
  # get tags from parent resource description
105
- parent_tags = [parent, @resource].compact.flat_map { |resource| resource._tag_list_arg }
85
+ parent_tags = [parent, @resource].compact.flat_map(&:_tag_list_arg)
106
86
  Apipie::TagListDescription.new((parent_tags + @tag_list).uniq.compact)
107
87
  end
108
88
 
@@ -178,14 +158,14 @@ module Apipie
178
158
  @formats || @resource._formats
179
159
  end
180
160
 
181
- def to_json(lang=nil)
161
+ def to_json(lang = nil)
182
162
  {
183
163
  :doc_url => doc_url,
184
164
  :name => @method,
185
165
  :apis => method_apis_to_json(lang),
186
166
  :formats => formats,
187
- :full_description => Apipie.app.translate(@full_description, lang),
188
- :errors => errors.map(&:to_json),
167
+ :full_description => Apipie.markup_to_html(Apipie.app.translate(@full_description, lang)),
168
+ :errors => errors.map{ |error| error.to_json(lang) }.flatten,
189
169
  :params => params_ordered.map{ |param| param.to_json(lang) }.flatten,
190
170
  :returns => @returns.map{ |return_item| return_item.to_json(lang) }.flatten,
191
171
  :examples => @examples,
@@ -201,25 +181,12 @@ module Apipie
201
181
  @from_concern
202
182
  end
203
183
 
204
- private
205
-
206
- def api_data(dsl_data)
207
- ret = dsl_data[:api_args].dup
208
- if dsl_data[:api_from_routes]
209
- desc = dsl_data[:api_from_routes][:desc]
210
- options = dsl_data[:api_from_routes][:options]
211
-
212
- api_from_routes = Apipie.routes_for_action(resource.controller, method, {:desc => desc, :options => options}).map do |route_info|
213
- [route_info[:verb],
214
- route_info[:path],
215
- route_info[:desc],
216
- (route_info[:options] || {}).merge(:from_routes => true)]
217
- end
218
- ret.concat(api_from_routes)
219
- end
220
- ret
184
+ def method_name
185
+ @method
221
186
  end
222
187
 
188
+ private
189
+
223
190
  def merge_params(params, new_params)
224
191
  new_param_names = Set.new(new_params.map(&:name))
225
192
  params.delete_if { |p| new_param_names.include?(p.name) }
@@ -259,16 +226,5 @@ module Apipie
259
226
  example << "\n" << format_example_data(ex[:response_data]).to_s if ex[:response_data]
260
227
  example
261
228
  end
262
-
263
- def concern_subst(string)
264
- return if string.nil?
265
- if from_concern?
266
- resource.controller._apipie_perform_concern_subst(string)
267
- else
268
- string
269
- end
270
- end
271
-
272
229
  end
273
-
274
230
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Apipie
4
+ class ParamDescription
5
+ # Data transfer object, used when param description is deprecated
6
+ class Deprecation
7
+ attr_reader :info, :deprecated_in, :sunset_at
8
+
9
+ def initialize(info: nil, deprecated_in: nil, sunset_at: nil)
10
+ @info = info
11
+ @deprecated_in = deprecated_in
12
+ @sunset_at = sunset_at
13
+ end
14
+
15
+ def to_json(*_args)
16
+ {
17
+ info: @info,
18
+ deprecated_in: @deprecated_in,
19
+ sunset_at: @sunset_at
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end