Class: Swagger::V2::Example

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/swagger/v2/example.rb

Overview

A class to represent example objects in the Swagger schema. Usually used to represent example request or responses. Provides access to both the raw example or a parsed representation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample) ⇒ Example

Returns a new instance of Example.



14
15
16
# File 'lib/swagger/v2/example.rb', line 14

def initialize(sample)
  @raw = sample
end

Instance Attribute Details

#rawObject (readonly)

The example as it appears in the Swagger document.

Returns:

  • Object the example



12
13
14
# File 'lib/swagger/v2/example.rb', line 12

def raw
  @raw
end

Instance Method Details

#inspectObject



27
28
29
# File 'lib/swagger/v2/example.rb', line 27

def inspect
  @raw.inspect
end

#parse(media_type = 'application/json') ⇒ Object

The example after it has been parsed to match the media_type.

Parameters:

  • media_type (String) (defaults to: 'application/json')

    the target media_type

Returns:

  • (Object)

    an object according to the media_type



21
22
23
24
25
# File 'lib/swagger/v2/example.rb', line 21

def parse(media_type = 'application/json')
  return @raw unless @raw.is_a? String
  parser = Swagger::MIMEType.parser_for(media_type)
  parser.parse(@raw)
end