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_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.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/parse/client/response.rb', line 106 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.
74 75 |
# File 'lib/parse/client/response.rb', line 74 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.
79 80 81 |
# File 'lib/parse/client/response.rb', line 79 def count @count end |
#error ⇒ Integer
Returns the error message.
74 75 |
# File 'lib/parse/client/response.rb', line 74 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#headers ⇒ Hash
Returns the HTTP response headers.
74 75 |
# File 'lib/parse/client/response.rb', line 74 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#http_status ⇒ Integer
Returns the HTTP status code from the response.
74 75 |
# File 'lib/parse/client/response.rb', line 74 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#parse_class ⇒ String
Returns the Parse class for this request.
74 75 76 |
# File 'lib/parse/client/response.rb', line 74 def parse_class @parse_class end |
#request ⇒ Integer
Returns the Parse::Request that generated this response.
74 75 |
# File 'lib/parse/client/response.rb', line 74 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
#result ⇒ Hash
Returns the body of the response result.
74 75 |
# File 'lib/parse/client/response.rb', line 74 attr_accessor :parse_class, :code, :error, :result, :http_status, :request, :headers |
Instance Method Details
#batch? ⇒ Boolean
true if this was a batch response.
128 129 130 |
# File 'lib/parse/client/response.rb', line 128 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.
135 136 137 138 139 140 141 142 143 |
# File 'lib/parse/client/response.rb', line 135 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.
214 215 216 217 218 |
# File 'lib/parse/client/response.rb', line 214 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.
172 173 174 |
# File 'lib/parse/client/response.rb', line 172 def error? !success? end |
#first ⇒ Object
Returns the first thing in the result array.
208 209 210 |
# File 'lib/parse/client/response.rb', line 208 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’
178 179 180 |
# File 'lib/parse/client/response.rb', line 178 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.
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/parse/client/response.rb', line 148 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_status` only, with no error code in the JSON.
-
HTTP 400 + error code 119 (‘OPERATION_FORBIDDEN`) — typical for CLP and `protectedFields` denials.
-
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.
196 197 198 199 |
# File 'lib/parse/client/response.rb', line 196 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.
202 203 204 205 |
# File 'lib/parse/client/response.rb', line 202 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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/parse/client/response.rb', line 84 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.
166 167 168 |
# File 'lib/parse/client/response.rb', line 166 def success? @code.nil? && @error.nil? end |
#to_s ⇒ String
Returns JSON encoded object, or an error string.
230 231 232 233 |
# File 'lib/parse/client/response.rb', line 230 def to_s return "[E-#{@code}] #{@request} : #{@error} (#{@http_status})" if error? @result.to_json end |