Class: Elevate::HTTP::Response
- Inherits:
-
Object
- Object
- Elevate::HTTP::Response
- Defined in:
- lib/elevate/http/response.rb
Overview
Encapsulates a response received from a HTTP server.
Instance Attribute Summary collapse
- #error ⇒ Object
-
#headers ⇒ Hash
Returns the HTTP headers.
-
#raw_body ⇒ NSData
readonly
Returns the raw body.
-
#status_code ⇒ Integer
Returns the HTTP status code.
-
#url ⇒ String
Returns the URL.
Instance Method Summary collapse
-
#append_data(data) ⇒ Object
private
Appends a chunk of data to the body.
-
#body ⇒ NSData, ...
Returns the body of the response.
-
#freeze ⇒ Object
private
Freezes this instance, making it immutable.
-
#initialize ⇒ Response
constructor
A new instance of Response.
-
#method_missing(m, *args, &block) ⇒ Object
Forwards unknown methods to
body
, enabling this object to behave likebody
. -
#respond_to_missing?(m, include_private = false) ⇒ Boolean
Handles missing method queries, allowing
body
masquerading.
Constructor Details
#initialize ⇒ Response
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
#error ⇒ Object
86 87 88 |
# File 'lib/elevate/http/response.rb', line 86 def error @error end |
#headers ⇒ Hash
Returns the HTTP headers
76 77 78 |
# File 'lib/elevate/http/response.rb', line 76 def headers @headers end |
#raw_body ⇒ NSData (readonly)
Returns the raw body
94 95 96 |
# File 'lib/elevate/http/response.rb', line 94 def raw_body @raw_body end |
#status_code ⇒ Integer
Returns the HTTP status code
84 85 86 |
# File 'lib/elevate/http/response.rb', line 84 def status_code @status_code end |
#url ⇒ String
Returns the URL
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 |
#body ⇒ NSData, ...
Returns the body of the response.
If the body is JSON-encoded, it will be decoded and returned.
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 |
#freeze ⇒ 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.
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.
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 |