Class: Parse::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(res = {}) ⇒ Response

Create an instance with a Parse response JSON hash.

Parameters:

  • res (Hash) (defaults to: {})

    the 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

#codeInteger

Returns the error code.

Returns:

  • (Integer)

    the error code



61
62
# File 'lib/parse/client/response.rb', line 61

attr_accessor :parse_class, :code, :error, :result, :http_status,
:request

#countInteger (readonly)

You can query Parse for counting objects, which may not actually have results.

Returns:

  • (Integer)

    the count result from a count query request.



66
67
68
# File 'lib/parse/client/response.rb', line 66

def count
  @count
end

#errorInteger

Returns the error message.

Returns:

  • (Integer)

    the error message



61
62
# File 'lib/parse/client/response.rb', line 61

attr_accessor :parse_class, :code, :error, :result, :http_status,
:request

#http_statusInteger

Returns the HTTP status code from the response.

Returns:

  • (Integer)

    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_classString

Returns the Parse class for this request.

Returns:

  • (String)

    the Parse class for this request



61
62
63
# File 'lib/parse/client/response.rb', line 61

def parse_class
  @parse_class
end

#requestInteger

Returns the Parse::Request that generated this response.

Returns:

  • (Integer)

    the Parse::Request that generated this response.

See Also:



61
62
# File 'lib/parse/client/response.rb', line 61

attr_accessor :parse_class, :code, :error, :result, :http_status,
:request

#resultHash

Returns the body of the response result.

Returns:

  • (Hash)

    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.

Returns:

  • (Boolean)


92
93
94
# File 'lib/parse/client/response.rb', line 92

def batch?
  @batch_response
end

#batch_responsesArray

If it is a batch respnose, we'll create an array of Response objects for each of the ones in the batch.

Returns:

  • (Array)

    an array of Response objects.



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.

Yield Parameters:

  • a (Object)

    result entry.



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.

Returns:

  • (Boolean)

See Also:



136
137
138
# File 'lib/parse/client/response.rb', line 136

def error?
  !success?
end

#firstObject

Returns the first thing in the result array.

Returns:

  • (Object)

    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'

Returns:

  • (Boolean)

See Also:



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

#resultsArray

Returns the result data from the response.

Returns:

  • (Array)

    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.

Returns:

  • (Boolean)

See Also:



130
131
132
# File 'lib/parse/client/response.rb', line 130

def success?
  @code.nil? && @error.nil?
end

#to_sString

Returns JSON encoded object, or an error string.

Returns:

  • (String)

    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