Class: ActionSpec::Doc::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/action_spec/doc/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, description:, media_type:, schema: nil, example: nil, examples: nil, options:) ⇒ Response

Returns a new instance of Response.



185
186
187
188
189
190
191
# File 'lib/action_spec/doc/endpoint.rb', line 185

def initialize(code:, description:, media_type:, schema: nil, example: nil, examples: nil, options:)
  @code = code.to_s
  @description = description
  @media_types = ActiveSupport::OrderedHash.new
  @options = options.deep_dup
  merge_media_type!(media_type || :json, schema:, example:, examples:, initialize: true)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



183
184
185
# File 'lib/action_spec/doc/endpoint.rb', line 183

def code
  @code
end

#descriptionObject (readonly)

Returns the value of attribute description.



183
184
185
# File 'lib/action_spec/doc/endpoint.rb', line 183

def description
  @description
end

#media_typesObject (readonly)

Returns the value of attribute media_types.



183
184
185
# File 'lib/action_spec/doc/endpoint.rb', line 183

def media_types
  @media_types
end

#optionsObject (readonly)

Returns the value of attribute options.



183
184
185
# File 'lib/action_spec/doc/endpoint.rb', line 183

def options
  @options
end

Instance Method Details

#copyObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/action_spec/doc/endpoint.rb', line 203

def copy
  self.class.new(
    code:,
    description:,
    media_type: nil,
    schema: nil,
    example: nil,
    examples: nil,
    options: options.deep_dup
  ).tap do |response|
    response.instance_variable_set(
      :@media_types,
      media_types.each_with_object(ActiveSupport::OrderedHash.new) do |(media_type, content), hash|
        hash[media_type] = copy_content(content)
      end
    )
  end
end

#merge(other) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/action_spec/doc/endpoint.rb', line 193

def merge(other)
  copy.tap do |merged|
    merged.instance_variable_set(:@description, other.description.presence || description)
    merged.instance_variable_set(:@options, options.deep_dup.merge(other.options.deep_dup))
    other.media_types.each do |media_type, content|
      merged.send(:merge_media_type!, media_type, **merged.send(:copy_content, content))
    end
  end
end