Class: Spectre::Http::SpectreHttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_res) ⇒ SpectreHttpResponse

Returns a new instance of SpectreHttpResponse.



144
145
146
147
148
149
150
# File 'lib/spectre/http.rb', line 144

def initialize net_res
  @code = net_res.code.to_i
  @message = net_res.message
  @body = net_res.body
  @headers = SpectreHttpHeader.new(net_res.to_hash)
  @json_data = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



142
143
144
# File 'lib/spectre/http.rb', line 142

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



142
143
144
# File 'lib/spectre/http.rb', line 142

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



142
143
144
# File 'lib/spectre/http.rb', line 142

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



142
143
144
# File 'lib/spectre/http.rb', line 142

def message
  @message
end

Instance Method Details

#jsonObject



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/spectre/http.rb', line 152

def json
  if !@body.nil? and @json_data.nil?
    begin
      @json_data = JSON.parse(@body, object_class: OpenStruct)
    rescue JSON::ParserError
      raise HttpError.new("Body content is not a valid JSON:\n#{@body}")
    end
  end

  @json_data
end

#success?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/spectre/http.rb', line 164

def success?
  @code < 400
end