class Elevate::HTTP::Response

Encapsulates a response received from a HTTP server.

@api public

Attributes

error[RW]
headers[RW]

Returns the HTTP headers

@return [Hash]

returned headers

@api public

raw_body[R]

Returns the raw body

@return [NSData]

response body

@api public

status_code[RW]

Returns the HTTP status code

@return [Integer]

status code of the response

@api public

url[RW]

Returns the URL

@return [String]

URL of the response

@api public

Public Class Methods

new() click to toggle source
# File lib/elevate/http/response.rb, line 7
def initialize
  @body = nil
  @headers = nil
  @status_code = nil
  @error = nil
  @raw_body = NSMutableData.alloc.init
  @url = nil
end

Public Instance Methods

append_data(data) click to toggle source

Appends a chunk of data to the body.

@api private

# File lib/elevate/http/response.rb, line 19
def append_data(data)
  @raw_body.appendData(data)
end
body() click to toggle source

Returns the body of the response.

If the body is JSON-encoded, it will be decoded and returned.

@return [NSData, Hash, Array, nil]

response body, if any. If the response is JSON-encoded, the decoded body.

@api public

# File lib/elevate/http/response.rb, line 31
def body
  @body ||= begin
    if json?
      NSJSONSerialization.JSONObjectWithData(@raw_body, options: 0, error: nil)
    else
      @raw_body
    end
  end
end
freeze() click to toggle source

Freezes this instance, making it immutable.

@api private

Calls superclass method
# File lib/elevate/http/response.rb, line 44
def freeze
  body

  super
end
method_missing(m, *args, &block) click to toggle source

Forwards unknown methods to body, enabling this object to behave like body.

This only occurs if body is a Ruby collection.

@api public

Calls superclass method
# File lib/elevate/http/response.rb, line 55
def method_missing(m, *args, &block)
  return super unless json?

  body.send(m, *args, &block)
end
respond_to_missing?(m, include_private = false) click to toggle source

Handles missing method queries, allowing body masquerading.

@api public

# File lib/elevate/http/response.rb, line 64
def respond_to_missing?(m, include_private = false)
  return false unless json?

  body.respond_to_missing?(m, include_private)
end

Private Instance Methods

json?() click to toggle source
# File lib/elevate/http/response.rb, line 106
def json?
  headers && headers["Content-Type"] =~ %r{application/json}
end