Class: OpenApi::DSL::MediaTypeObj

Inherits:
Hash
  • Object
show all
Defined in:
lib/oas_objs/media_type_obj.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_type, example: nil, examples: nil, exp_params: nil, **media_hash) ⇒ MediaTypeObj

Returns a new instance of MediaTypeObj.



12
13
14
15
16
17
18
19
# File 'lib/oas_objs/media_type_obj.rb', line 12

def initialize(media_type, example: nil, examples: nil, exp_params: nil, **media_hash)
  schema_type     = media_hash.values_at(:type, :data).compact.first
  exp_params      = schema_type.keys if exp_params == :all
  self.examples   = ExampleObj.new(examples, exp_params, multiple: true) if examples.present?
  self.example    = ExampleObj.new(example) if example.present?
  self.media_type = media_type_mapping(media_type)
  self.schema     = SchemaObj.new(schema_type, media_hash.except(:type, :data))
end

Instance Attribute Details

#exampleObject

Returns the value of attribute example.



10
11
12
# File 'lib/oas_objs/media_type_obj.rb', line 10

def example
  @example
end

#examplesObject

Returns the value of attribute examples.



10
11
12
# File 'lib/oas_objs/media_type_obj.rb', line 10

def examples
  @examples
end

#media_typeObject

Returns the value of attribute media_type.



10
11
12
# File 'lib/oas_objs/media_type_obj.rb', line 10

def media_type
  @media_type
end

#schemaObject

Returns the value of attribute schema.



10
11
12
# File 'lib/oas_objs/media_type_obj.rb', line 10

def schema
  @schema
end

Instance Method Details

#media_type_mapping(media_type) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oas_objs/media_type_obj.rb', line 35

def media_type_mapping(media_type)
  return media_type if media_type.is_a? String
  case media_type
  when :app then   "application/*"
  when :json then  "application/json"
  when :xml then   "application/xml"
  when :xwww then  "application/x-www-form-urlencoded"
  when :pdf then   "application/pdf"
  when :zip then   "application/zip"
  when :gzip then  "application/gzip"
  when :doc then   "application/msword"
  when :docx then  "application/application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  when :xls then   "application/vnd.ms-excel"
  when :xlsx then  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  when :ppt then   "application/vnd.ms-powerpoint"
  when :pptx then  "application/vnd.openxmlformats-officedocument.presentationml.presentation"
  when :form then  "multipart/form-data"; when :form_data then "multipart/form-data"
  when :text then  "text/*"
  when :plain then "text/plain then charset=utf-8"
  when :html then  "text/html"
  when :csv then   "text/csv"
  when :image then "image/*"
  when :png then   "image/png"
  when :jpeg then  "image/jpeg"
  when :gif then   "image/gif"
  when :audio then "audio/*"
  when :video then "video/*"
  else             nil
  end
end

#processObject



21
22
23
24
25
26
27
28
# File 'lib/oas_objs/media_type_obj.rb', line 21

def process
  return { } if media_type.nil?
  schema_processed = schema.process
  result = schema_processed.values.join.blank? ? { } : { schema: schema_processed }
  result[:example] = example.process unless example.nil?
  result[:examples] = examples.process unless examples.nil?
  { media_type => result }
end