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.

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.

API:

  • public



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.

API:

  • public



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

API:

  • public



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

def error
  @error
end

#headersHash

Returns the HTTP headers

Returns:

  • returned headers

API:

  • public



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

def headers
  @headers
end

#raw_bodyNSData (readonly)

Returns the raw body

Returns:

  • response body

API:

  • public



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:

  • status code of the response

API:

  • public



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

def status_code
  @status_code
end

#urlString

Returns the URL

Returns:

  • URL of the response

API:

  • public



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.

API:

  • private



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:

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

API:

  • public



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.

API:

  • private



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:

API:

  • public



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