Class: Elevate::HTTP::Response

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

Overview

Encapsulates a response received from a HTTP server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



7
8
9
10
11
12
13
14
# File 'lib/elevate/http/response.rb', line 7

def initialize
  @body = nil
  @headers = nil
  @status_code = nil
  @error = nil
  @raw_body = nil
  @url = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

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

This only occurs if body is a Ruby collection.



56
57
58
59
60
# File 'lib/elevate/http/response.rb', line 56

def method_missing(m, *args, &block)
  return super unless json?

  body.send(m, *args, &block)
end

Instance Attribute Details

#errorObject



87
88
89
# File 'lib/elevate/http/response.rb', line 87

def error
  @error
end

#headersHash

Returns the HTTP headers

Returns:

  • (Hash)

    returned headers



77
78
79
# File 'lib/elevate/http/response.rb', line 77

def headers
  @headers
end

#raw_bodyNSData (readonly)

Returns the raw body

Returns:

  • (NSData)

    response body



95
96
97
# File 'lib/elevate/http/response.rb', line 95

def raw_body
  @raw_body
end

#status_codeInteger

Returns the HTTP status code

Returns:

  • (Integer)

    status code of the response



85
86
87
# File 'lib/elevate/http/response.rb', line 85

def status_code
  @status_code
end

#urlString

Returns the URL

Returns:

  • (String)

    URL of the response



103
104
105
# File 'lib/elevate/http/response.rb', line 103

def url
  @url
end

Instance Method Details

#append_data(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Appends a chunk of data to the body.



19
20
21
22
# File 'lib/elevate/http/response.rb', line 19

def append_data(data)
  @raw_body ||= NSMutableData.alloc.init
  @raw_body.appendData(data)
end

#bodyNSData, ...

Returns the body of the response.

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

Returns:

  • (NSData, Hash, Array, nil)

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



32
33
34
35
36
37
38
39
40
# File 'lib/elevate/http/response.rb', line 32

def body
  @body ||= begin
    if json?
      NSJSONSerialization.JSONObjectWithData(@raw_body, options: 0, error: nil)
    else
      @raw_body
    end
  end
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Freezes this instance, making it immutable.



45
46
47
48
49
# File 'lib/elevate/http/response.rb', line 45

def freeze
  body

  super
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Handles missing method queries, allowing body masquerading.

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/elevate/http/response.rb', line 65

def respond_to_missing?(m, include_private = false)
  return false unless json?

  body.respond_to_missing?(m, include_private)
end