Class: Grape::Endpoint
- Inherits:
-
Object
- Object
- Grape::Endpoint
- Defined in:
- lib/grape-swagger/endpoint.rb
Instance Method Summary collapse
- #add_definitions_from(models) ⇒ Object
- #apply_defaults(route, codes) ⇒ Object
- #consumes_object(route, format) ⇒ Object
-
#contact_object(infos) ⇒ Object
contact.
- #content_types_for(target_class) ⇒ Object
- #description_object(route, markdown) ⇒ Object
-
#info_object(infos) ⇒ Object
building info object.
-
#license_object(infos) ⇒ Object
sub-objects of info object license.
- #method_object(route, options, path) ⇒ Object
- #params_object(route) ⇒ Object
-
#path_and_definition_objects(namespace_routes, options) ⇒ Object
building path and definitions objects.
-
#path_item(routes, options) ⇒ Object
path object.
- #produces_object(route, format) ⇒ Object
- #response_object(route, markdown) ⇒ Object
- #security_object(route) ⇒ Object
- #summary_object(route) ⇒ Object
-
#swagger_object(target_class, request, options) ⇒ Object
swagger spec2.0 related parts.
- #tag_object(route) ⇒ Object
Instance Method Details
#add_definitions_from(models) ⇒ Object
80 81 82 83 84 |
# File 'lib/grape-swagger/endpoint.rb', line 80 def add_definitions_from(models) return if models.nil? models.each { |x| expose_params_from_model(x) } end |
#apply_defaults(route, codes) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/grape-swagger/endpoint.rb', line 208 def apply_defaults(route, codes) default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym] default_code[:model] = @entity if @entity default_code[:message] = route.description || default_code[:message].sub('{item}', @item) [default_code] + codes end |
#consumes_object(route, format) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/grape-swagger/endpoint.rb', line 155 def consumes_object(route, format) method = route.request_method.downcase.to_sym format = route.settings[:description][:consumes] if route.settings[:description] && route.settings[:description][:consumes] mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) if [:post, :put].include?(method) mime_types end |
#contact_object(infos) ⇒ Object
contact
59 60 61 62 63 64 65 |
# File 'lib/grape-swagger/endpoint.rb', line 59 def contact_object(infos) { name: infos.delete(:contact_name), email: infos.delete(:contact_email), url: infos.delete(:contact_url) }.delete_if { |_, value| value.blank? } end |
#content_types_for(target_class) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/grape-swagger/endpoint.rb', line 8 def content_types_for(target_class) content_types = (target_class.content_types || {}).values if content_types.empty? formats = [target_class.format, target_class.default_format].compact.uniq formats = Grape::Formatter::Base.formatters({}).keys if formats.empty? content_types = Grape::ContentTypes::CONTENT_TYPES.select { |content_type, _mime_type| formats.include? content_type }.values end content_types.uniq end |
#description_object(route, markdown) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/grape-swagger/endpoint.rb', line 134 def description_object(route, markdown) description = route.[:desc] if route..key?(:desc) description = route.description if route.description.present? description = "# #{description} " if markdown description += "\n #{route.[:detail]}" if route..key?(:detail) description = markdown.markdown(description.to_s).chomp if markdown description end |
#info_object(infos) ⇒ Object
building info object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/grape-swagger/endpoint.rb', line 38 def info_object(infos) { title: infos[:title] || 'API title', description: infos[:description], termsOfServiceUrl: infos[:terms_of_service_url], contact: contact_object(infos), license: license_object(infos), version: infos[:version] }.delete_if { |_, value| value.blank? } end |
#license_object(infos) ⇒ Object
sub-objects of info object license
51 52 53 54 55 56 |
# File 'lib/grape-swagger/endpoint.rb', line 51 def license_object(infos) { name: infos.delete(:license), url: infos.delete(:license_url) }.delete_if { |_, value| value.blank? } end |
#method_object(route, options, path) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/grape-swagger/endpoint.rb', line 106 def method_object(route, , path) method = {} method[:summary] = summary_object(route) method[:description] = description_object(route, [:markdown]) method[:produces] = produces_object(route, [:produces] || [:format]) method[:consumes] = consumes_object(route, [:format]) method[:parameters] = params_object(route) method[:security] = security_object(route) method[:responses] = response_object(route, [:markdown]) method[:tags] = tag_object(route) method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path) method.delete_if { |_, value| value.blank? } [route.request_method.downcase.to_sym, method] end |
#params_object(route) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/grape-swagger/endpoint.rb', line 163 def params_object(route) parameters = partition_params(route).map do |param, value| value = { required: false }.merge(value) if value.is_a?(Hash) _, value = default_type([[param, value]]).first if value == '' GrapeSwagger::DocMethods::ParseParams.call(param, value, route) end if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(parameters, route.request_method) parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(parameters, route, @definitions) end parameters end |
#path_and_definition_objects(namespace_routes, options) ⇒ Object
building path and definitions objects
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/grape-swagger/endpoint.rb', line 68 def path_and_definition_objects(namespace_routes, ) @paths = {} @definitions = {} namespace_routes.keys.each do |key| routes = namespace_routes[key] path_item(routes, ) end add_definitions_from [:models] [@paths, @definitions] end |
#path_item(routes, options) ⇒ Object
path object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/grape-swagger/endpoint.rb', line 87 def path_item(routes, ) routes.each do |route| next if hidden?(route, ) @item, path = GrapeSwagger::DocMethods::PathString.build(route, ) @entity = route.entity || route.[:success] verb, method_object = method_object(route, , path) if @paths.key?(path.to_s) @paths[path.to_s][verb] = method_object else @paths[path.to_s] = { verb => method_object } end GrapeSwagger::DocMethods::Extensions.add(@paths[path.to_s], @definitions, route) end end |
#produces_object(route, format) ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/grape-swagger/endpoint.rb', line 144 def produces_object(route, format) mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) route_mime_types = [:formats, :content_types, :produces].map do |producer| possible = route.[producer] GrapeSwagger::DocMethods::ProducesConsumes.call(possible) if possible.present? end.flatten.compact.uniq route_mime_types.present? ? route_mime_types : mime_types end |
#response_object(route, markdown) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/grape-swagger/endpoint.rb', line 177 def response_object(route, markdown) codes = (route.http_codes || route.[:failure] || []) codes = apply_defaults(route, codes) if route.[:ignore_defaults].nil? codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2] } : x } codes.each_with_object({}) do |value, memo| memo[value[:code]] = { description: value[:message] } response_model = @item response_model = expose_params_from_model(value[:model]) if value[:model] if memo.key?(200) && route.request_method == 'DELETE' && value[:model].nil? memo[204] = memo.delete(200) value[:code] = 204 end next if memo.key?(204) next unless !response_model.start_with?('Swagger_doc') && ((@definitions[response_model] && value[:code].to_s.start_with?('2')) || value[:model]) @definitions[response_model][:description] = description_object(route, markdown) # TODO: proof that the definition exist, if model isn't specified memo[value[:code]][:schema] = if route.[:is_array] { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } } else { '$ref' => "#/definitions/#{response_model}" } end end end |
#security_object(route) ⇒ Object
122 123 124 |
# File 'lib/grape-swagger/endpoint.rb', line 122 def security_object(route) route.[:security] if route..key?(:security) end |
#summary_object(route) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/grape-swagger/endpoint.rb', line 126 def summary_object(route) summary = route.[:desc] if route..key?(:desc) summary = route.description if route.description.present? summary = route.[:summary] if route..key?(:summary) summary end |
#swagger_object(target_class, request, options) ⇒ Object
swagger spec2.0 related parts
required keys for SwaggerObject
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/grape-swagger/endpoint.rb', line 23 def swagger_object(target_class, request, ) { info: info_object([:info].merge(version: [:doc_version])), swagger: '2.0', produces: content_types_for(target_class), authorizations: [:authorizations], securityDefinitions: [:security_definitions], host: GrapeSwagger::DocMethods::OptionalObject.build(:host, , request), basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, , request), tags: GrapeSwagger::DocMethods::TagNameDescription.build(), schemes: [:schemes].is_a?(String) ? [[:schemes]] : [:schemes] }.delete_if { |_, value| value.blank? } end |
#tag_object(route) ⇒ Object
215 216 217 |
# File 'lib/grape-swagger/endpoint.rb', line 215 def tag_object(route) Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first) end |