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 =
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
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.
- #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.
- #results ⇒ Array
The result data from the response.
- #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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # File 'lib/parse/client/response.rb', line 70 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.
61 62 | # File 'lib/parse/client/response.rb', line 61 attr_accessor :parse_class, :code, :error, :result, :http_status, :request |
#count ⇒ Integer (readonly)
You can query Parse for counting objects, which may not actually have results.
66 67 68 | # File 'lib/parse/client/response.rb', line 66 def count @count end |
#error ⇒ Integer
Returns the error message.
61 62 | # File 'lib/parse/client/response.rb', line 61 attr_accessor :parse_class, :code, :error, :result, :http_status, :request |
#http_status ⇒ Integer
Returns the HTTP status code from the response.
61 62 | # File 'lib/parse/client/response.rb', line 61 attr_accessor :parse_class, :code, :error, :result, :http_status, :request |
#parse_class ⇒ String
Returns the Parse class for this request.
61 62 63 | # File 'lib/parse/client/response.rb', line 61 def parse_class @parse_class end |
#request ⇒ Integer
Returns the Parse::Request that generated this response.
61 62 | # File 'lib/parse/client/response.rb', line 61 attr_accessor :parse_class, :code, :error, :result, :http_status, :request |
#result ⇒ Hash
Returns the body of the response result.
61 62 | # File 'lib/parse/client/response.rb', line 61 attr_accessor :parse_class, :code, :error, :result, :http_status, :request |
Instance Method Details
#batch? ⇒ Boolean
true if this was a batch response.
92 93 94 | # File 'lib/parse/client/response.rb', line 92 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.
99 100 101 102 103 104 105 106 107 | # File 'lib/parse/client/response.rb', line 99 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.
159 160 161 162 163 | # File 'lib/parse/client/response.rb', line 159 def each return enum_for(:each) unless block_given? results.each(&Proc.new) self end |
#error? ⇒ Boolean
true if the response has an error code.
136 137 138 | # File 'lib/parse/client/response.rb', line 136 def error? !success? end |
#first ⇒ Object
Returns the first thing in the result array.
153 154 155 | # File 'lib/parse/client/response.rb', line 153 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'
142 143 144 | # File 'lib/parse/client/response.rb', line 142 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.
112 113 114 115 116 117 118 119 120 121 122 123 124 | # File 'lib/parse/client/response.rb', line 112 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 |
#results ⇒ Array
Returns the result data from the response.
147 148 149 150 | # File 'lib/parse/client/response.rb', line 147 def results return [] if @result.nil? @result.is_a?(Array) ? @result : [@result] end |
#success? ⇒ Boolean
true if the response is successful.
130 131 132 | # File 'lib/parse/client/response.rb', line 130 def success? @code.nil? && @error.nil? end |
#to_s ⇒ String
Returns JSON encoded object, or an error string.
175 176 177 178 | # File 'lib/parse/client/response.rb', line 175 def to_s return "[E-#{@code}] #{@request} : #{@error} (#{@http_status})" if error? @result.to_json end |