Class: Restspec::Endpoints::Response
- Inherits:
-
Object
- Object
- Restspec::Endpoints::Response
- Defined in:
- lib/restspec/endpoints/response.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#raw_body ⇒ Object
Returns the value of attribute raw_body.
Instance Method Summary collapse
- #body ⇒ Object
-
#initialize(code, headers, raw_body) ⇒ Response
constructor
A new instance of Response.
- #parsed_body ⇒ Object
- #read_body(value = parsed_body) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(code, headers, raw_body) ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 |
# File 'lib/restspec/endpoints/response.rb', line 8 def initialize(code, headers, raw_body) self.headers = headers self.code = code self.raw_body = raw_body end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
6 7 8 |
# File 'lib/restspec/endpoints/response.rb', line 6 def code @code end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
6 7 8 |
# File 'lib/restspec/endpoints/response.rb', line 6 def endpoint @endpoint end |
#headers ⇒ Object
Returns the value of attribute headers.
6 7 8 |
# File 'lib/restspec/endpoints/response.rb', line 6 def headers @headers end |
#raw_body ⇒ Object
Returns the value of attribute raw_body.
6 7 8 |
# File 'lib/restspec/endpoints/response.rb', line 6 def raw_body @raw_body end |
Instance Method Details
#body ⇒ Object
48 49 50 |
# File 'lib/restspec/endpoints/response.rb', line 48 def body @body ||= read_body end |
#parsed_body ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/restspec/endpoints/response.rb', line 40 def parsed_body @parsed_body ||= begin JSON.parse(raw_body) rescue JSON::ParserError => e raw_body end end |
#read_body(value = parsed_body) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/restspec/endpoints/response.rb', line 23 def read_body(value = parsed_body) case value when Array value.map { |item| read_body(item) } when Hash Values::SuperHash.new(value).tap do |super_hash| super_hash.find do |key, value| if value.class == Array super_hash[key] = read_body(value) end end end else value end end |
#to_s ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/restspec/endpoints/response.rb', line 14 def to_s if endpoint.present? url = endpoint.executed_url || endpoint.full_path "[#{endpoint.method.upcase} to #{url}]" else raw_body end end |