Class: Parse::Response
- Inherits:
-
Object
- Object
- Parse::Response
- Includes:
- Enumerable
- Defined in:
- lib/parse/client/response.rb
Overview
Represents a response from Parse server. A response can also be a set of responses (from a Batch response).
Constant Summary collapse
- ERROR_INTERNAL =
Code for an unknown error.
1- ERROR_SERVICE_UNAVAILABLE =
Code when the server returns a 500 or is non-responsive.
2- ERROR_TIMEOUT =
Code when the request times out.
124- ERROR_EXCEEDED_BURST_LIMIT =
Code when the requests per second limit as been exceeded.
155- ERROR_DUPLICATE_REQUEST =
Code when a request carrying a previously-seen
X-Parse-Request-Idis rejected by Parse Server's idempotency layer (the request was already applied). Surfaced as Error::DuplicateRequestError. 159- ERROR_OBJECT_NOT_FOUND =
Code when a requested record is not found.
101- ERROR_USERNAME_MISSING =
Code when the username is missing in request.
200- ERROR_PASSWORD_MISSING =
Code when the password is missing in request.
201- ERROR_USERNAME_TAKEN =
Code when the username is already in the system.
202- ERROR_EMAIL_TAKEN =
Code when the email is already in the system.
203- ERROR_EMAIL_NOT_FOUND =
Code when the email is not found
205- ERROR_EMAIL_INVALID =
Code when the email is invalid
125- ERROR_INVALID_SESSION_TOKEN =
Code returned for invalid or expired session tokens (Parse Server).
209- ERROR_OPERATION_FORBIDDEN =
Code returned for operations that are not permitted under the caller's ACL / CLP / authentication scope. Parse Server uses this code for both "you are not authenticated for this" and "you are authenticated, but not authorized" — see #permission_denied? for the SDK-friendly predicate.
119- ERROR =
The field name for the error.
"error".freeze
- SUCCESS =
The field name for the success.
"success".freeze
- CODE =
The field name for the error code.
"code".freeze
- RESULTS =
The field name for the results of the request.
"results".freeze
- COUNT =
The field name for the count result in a count response.
"count".freeze
- RETRY_AFTER =
The Retry-After header name.
"Retry-After".freeze
Instance Attribute Summary collapse
-
#code ⇒ Integer
The error code.
-
#count ⇒ Integer
readonly
You can query Parse for counting objects, which may not actually have results.
-
#error ⇒ Integer
The error message.
-
#headers ⇒ Hash
The HTTP response headers.
-
#http_status ⇒ Integer
The HTTP status code from the response.
-
#parse_class ⇒ String
The Parse class for this request.
-
#request ⇒ Integer
The Parse::Request that generated this response.
-
#result ⇒ Hash
The body of the response result.
Instance Method Summary collapse
-
#batch? ⇒ Boolean
true if this was a batch response.
-
#batch_responses ⇒ Array
If it is a batch respnose, we'll create an array of Response objects for each of the ones in the batch.
-
#each {|a| ... } ⇒ Object
Iterate through each result item.
-
#error? ⇒ Boolean
true if the response has an error code.
-
#first ⇒ Object
The first thing in the result array.
-
#initialize(res = {}) ⇒ Response
constructor
Create an instance with a Parse response JSON hash.
-
#object_not_found? ⇒ Boolean
true if the response has an error code of 'object not found'.
-
#parse_result!(h) ⇒ Object
(also: #parse_results!)
This method takes the result hash and determines if it is a regular parse query result, object result or a count result.
-
#permission_denied? ⇒ Boolean
true if the response indicates the caller is not authorized to perform the requested operation.
-
#results ⇒ Array
The result data from the response.
-
#retry_after ⇒ Integer?
Get the Retry-After header value as seconds.
-
#success? ⇒ Boolean
true if the response is successful.
-
#to_s ⇒ String
JSON encoded object, or an error string.
Constructor Details
#initialize(res = {}) ⇒ Response
Create an instance with a Parse response JSON hash.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/parse/client/response.rb', line 110 def initialize(res = {}) @http_status = 0 @count = 0 @batch_response = false # by default, not a batch response @result = nil # If a string is used for initializing, treat it as JSON # check for string to not be 'OK' since that is the health check API response res = JSON.parse(res) if res.is_a?(String) && res != "OK".freeze # If it is a hash (or parsed JSON), then parse the result. parse_result!(res) if res.is_a?(Hash) # if the result is an Array, then most likely it is a set of responses # from using a Batch API. if res.is_a?(Array) @batch_response = true @result = res || [] @count = @result.count end #if none match, set pure result @result = res if @result.nil? end |
Instance Attribute Details
#code ⇒ Integer
Returns the error code.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#count ⇒ Integer (readonly)
You can query Parse for counting objects, which may not actually have results.
83 84 85 |
# File 'lib/parse/client/response.rb', line 83 def count @count end |
#error ⇒ Integer
Returns the error message.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#headers ⇒ Hash
Returns the HTTP response headers.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#http_status ⇒ Integer
Returns the HTTP status code from the response.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#parse_class ⇒ String
Returns the Parse class for this request.
78 79 80 |
# File 'lib/parse/client/response.rb', line 78 def parse_class @parse_class end |
#request ⇒ Integer
Returns the Parse::Request that generated this response.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#result ⇒ Hash
Returns the body of the response result.
78 79 |
# File 'lib/parse/client/response.rb', line 78 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
Instance Method Details
#batch? ⇒ Boolean
true if this was a batch response.
132 133 134 |
# File 'lib/parse/client/response.rb', line 132 def batch? @batch_response end |
#batch_responses ⇒ Array
If it is a batch respnose, we'll create an array of Response objects for each of the ones in the batch.
139 140 141 142 143 144 145 146 147 |
# File 'lib/parse/client/response.rb', line 139 def batch_responses return [@result] unless @batch_response # if batch response, generate array based on the response hash. @result.map do |r| next r unless r.is_a?(Hash) hash = r[SUCCESS] || r[ERROR] Parse::Response.new hash end end |
#each {|a| ... } ⇒ Object
Iterate through each result item.
218 219 220 221 222 |
# File 'lib/parse/client/response.rb', line 218 def each(&block) return enum_for(:each) unless block_given? results.each(&block) self end |
#error? ⇒ Boolean
true if the response has an error code.
176 177 178 |
# File 'lib/parse/client/response.rb', line 176 def error? !success? end |
#first ⇒ Object
Returns the first thing in the result array.
212 213 214 |
# File 'lib/parse/client/response.rb', line 212 def first @result.is_a?(Array) ? @result.first : @result end |
#object_not_found? ⇒ Boolean
true if the response has an error code of 'object not found'
182 183 184 |
# File 'lib/parse/client/response.rb', line 182 def object_not_found? @code == ERROR_OBJECT_NOT_FOUND end |
#parse_result!(h) ⇒ Object Also known as: parse_results!
This method takes the result hash and determines if it is a regular parse query result, object result or a count result. The response should be a hash either containing the result data or the error.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/parse/client/response.rb', line 152 def parse_result!(h) @result = {} return unless h.is_a?(Hash) @code = h[CODE] @error = h[ERROR] if h[RESULTS].is_a?(Array) @result = h[RESULTS] @count = h[COUNT] || @result.count else @result = h @count = 1 end end |
#permission_denied? ⇒ Boolean
true if the response indicates the caller is not authorized to perform the requested operation. Parse Server signals authorization failure in two shapes that no-master-key clients commonly hit:
- HTTP 403 with no body (sometimes 401) — recorded as
http_statusonly, with no error code in the JSON. - HTTP 400 + error code 119 (
OPERATION_FORBIDDEN) — typical for CLP andprotectedFieldsdenials. - HTTP 400 + error code 209 (
INVALID_SESSION_TOKEN) — session token missing, revoked, or expired.
This predicate normalizes those into a single check so client code doesn't have to remember both the HTTP-status and code-only paths.
200 201 202 203 |
# File 'lib/parse/client/response.rb', line 200 def return true if @http_status == 401 || @http_status == 403 @code == ERROR_OPERATION_FORBIDDEN || @code == ERROR_INVALID_SESSION_TOKEN end |
#results ⇒ Array
Returns the result data from the response.
206 207 208 209 |
# File 'lib/parse/client/response.rb', line 206 def results return [] if @result.nil? @result.is_a?(Array) ? @result : [@result] end |
#retry_after ⇒ Integer?
Get the Retry-After header value as seconds. The Retry-After header can be either a number of seconds or an HTTP-date.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/parse/client/response.rb', line 88 def retry_after return nil unless @headers.is_a?(Hash) value = @headers[RETRY_AFTER] || @headers["retry-after"] return nil if value.nil? # Try parsing as integer (seconds) if value.to_s =~ /\A\d+\z/ value.to_i else # Try parsing as HTTP-date begin date = Time.httpdate(value.to_s) delay = (date - Time.now).ceil delay > 0 ? delay : 1 rescue ArgumentError nil end end end |
#success? ⇒ Boolean
true if the response is successful.
170 171 172 |
# File 'lib/parse/client/response.rb', line 170 def success? @code.nil? && @error.nil? end |
#to_s ⇒ String
Returns JSON encoded object, or an error string.
234 235 236 237 |
# File 'lib/parse/client/response.rb', line 234 def to_s return "[E-#{@code}] #{@request} : #{@error} (#{@http_status})" if error? @result.to_json end |