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 = NSMutableData.alloc.init
  @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.



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

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

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

Instance Attribute Details

#errorObject



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

def error
  @error
end

#headersHash

Returns the HTTP headers

Returns:

  • (Hash)

    returned headers



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

def headers
  @headers
end

#raw_bodyNSData (readonly)

Returns the raw body

Returns:

  • (NSData)

    response body



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

def raw_body
  @raw_body
end

#status_codeInteger

Returns the HTTP status code

Returns:

  • (Integer)

    status code of the response



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

def status_code
  @status_code
end

#urlString

Returns the URL

Returns:

  • (String)

    URL of the response



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

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
# File 'lib/elevate/http/response.rb', line 19

def append_data(data)
  @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.



31
32
33
34
35
36
37
38
39
# 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

#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.



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

def freeze
  body

  super
end

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

Handles missing method queries, allowing body masquerading.

Returns:

  • (Boolean)


64
65
66
67
68
# 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