Class: Swagalicious::ExampleHelpers::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/swagalicious/example_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, request:) ⇒ Parser

Returns a new instance of Parser.



79
80
81
82
83
84
# File 'lib/swagalicious/example_helpers.rb', line 79

def initialize(response:, request:)
  @content_type = response.headers["Content-Type"]
  @body         = response.body
  @response     = response
  @request      = request
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



76
77
78
# File 'lib/swagalicious/example_helpers.rb', line 76

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



77
78
79
# File 'lib/swagalicious/example_helpers.rb', line 77

def content_type
  @content_type
end

#requestObject (readonly)

Returns the value of attribute request.



77
78
79
# File 'lib/swagalicious/example_helpers.rb', line 77

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



77
78
79
# File 'lib/swagalicious/example_helpers.rb', line 77

def response
  @response
end

Instance Method Details

#parse(raise_on_invalid: true) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/swagalicious/example_helpers.rb', line 86

def parse(raise_on_invalid: true)
  # Redirections shouldnt be parsed
  if response.status >= 300 && response.status <= 399
    return
  end

  case content_type
  when /json/
    self.body = "{}" if self.body.empty?

    Oj.load(self.body, symbol_keys: true)
  when /ya?ml/
    body = "---" if self.body.empty?

    (YAML.load(self.body) || {}).with_indifferent_access
  when /xml/
    (Ox.load(self.body, mode: :hash_no_attrs) || {}).with_indifferent_access
  when "", nil, /html/
    self.body
  else
    return unless raise_on_invalid

    Swagalicious::ExampleHelpers.raise_invalid_response(response: response, request: request, message: "Invalid Content-Type header #{response.headers["Content-Type"]}")
  end
end