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
bodymasquerading.
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 = 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
#error ⇒ Object
87 88 89 |
# File 'lib/elevate/http/response.rb', line 87 def error @error end |
#headers ⇒ Hash
Returns the HTTP headers
77 78 79 |
# File 'lib/elevate/http/response.rb', line 77 def headers @headers end |
#raw_body ⇒ NSData (readonly)
Returns the raw body
95 96 97 |
# File 'lib/elevate/http/response.rb', line 95 def raw_body @raw_body end |
#status_code ⇒ Integer
Returns the HTTP status code
85 86 87 |
# File 'lib/elevate/http/response.rb', line 85 def status_code @status_code end |
#url ⇒ String
Returns the URL
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 |
#body ⇒ NSData, ...
Returns the body of the response.
If the body is JSON-encoded, it will be decoded and returned.
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 |
#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.
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.
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 |