Class: OasParser::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_parser/response_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ ResponseParser

Returns a new instance of ResponseParser.



5
6
7
# File 'lib/oas_parser/response_parser.rb', line 5

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject

Returns the value of attribute raw.



3
4
5
# File 'lib/oas_parser/response_parser.rb', line 3

def raw
  @raw
end

Instance Method Details

#jsonObject



14
15
16
# File 'lib/oas_parser/response_parser.rb', line 14

def json
  parse('json').to_json
end

#parse(mode = nil) ⇒ Object



9
10
11
12
# File 'lib/oas_parser/response_parser.rb', line 9

def parse(mode = nil)
  @mode = mode
  route(@raw)
end

#xml(xml_options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oas_parser/response_parser.rb', line 18

def xml(xml_options = {})
  xml_options ||= {}
  xml_options = default_xml_options.merge(xml_options)

  raw_xml = parse('xml').to_xml(xml_options)

  xml_document = Nokogiri::XML(raw_xml)

  xml_document.xpath('//__attributes').each do |attributes|
    attributes.children.each do |attribute|
      next unless attribute.class == Nokogiri::XML::Element
      attribute.parent.parent.css("> #{attribute.name}").remove
      attribute.parent.parent[attribute.name] = attribute.content
    end
  end

  xml_document.xpath('//__array_attributes').each do |attributes|
    attributes.children.each do |attribute|
      next unless attribute.class == Nokogiri::XML::Element

      parameter = {}
      parameter['example'] = attribute.css('example').text if attribute.css('example')
      parameter['type'] = attribute.css('type').text if attribute.css('type')

      attribute.parent.parent.parent[attribute.name] = parameter_value(parameter)
    end
    attributes.parent.remove
  end

  xml_document.xpath('//__text').each do |attribute|
    value = attribute.children.last.content
    attribute.parent.content = value
  end

  xml_document.xpath('//__attributes').each(&:remove)

  xml_document.to_xml.each_line.reject { |x| x.strip == '' }.join
end