parse-stack 1.5.3 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/parse-ruby-sdk.png +0 -0
  3. data/Changes.md +25 -1
  4. data/Gemfile.lock +4 -4
  5. data/README.md +37 -31
  6. data/bin/console +3 -0
  7. data/lib/parse/api/all.rb +2 -1
  8. data/lib/parse/api/apps.rb +12 -0
  9. data/lib/parse/api/config.rb +5 -1
  10. data/lib/parse/api/files.rb +1 -0
  11. data/lib/parse/api/hooks.rb +1 -0
  12. data/lib/parse/api/objects.rb +4 -1
  13. data/lib/parse/api/push.rb +1 -0
  14. data/lib/parse/api/{schemas.rb → schema.rb} +7 -0
  15. data/lib/parse/api/server.rb +44 -0
  16. data/lib/parse/api/sessions.rb +1 -0
  17. data/lib/parse/api/users.rb +4 -1
  18. data/lib/parse/client.rb +109 -73
  19. data/lib/parse/client/authentication.rb +2 -1
  20. data/lib/parse/client/batch.rb +9 -1
  21. data/lib/parse/client/body_builder.rb +16 -1
  22. data/lib/parse/client/caching.rb +15 -13
  23. data/lib/parse/client/protocol.rb +27 -15
  24. data/lib/parse/client/response.rb +26 -8
  25. data/lib/parse/model/acl.rb +1 -1
  26. data/lib/parse/model/associations/belongs_to.rb +18 -19
  27. data/lib/parse/model/associations/collection_proxy.rb +6 -0
  28. data/lib/parse/model/associations/has_many.rb +5 -6
  29. data/lib/parse/model/bytes.rb +4 -1
  30. data/lib/parse/model/classes/user.rb +46 -44
  31. data/lib/parse/model/core/actions.rb +508 -460
  32. data/lib/parse/model/core/builder.rb +75 -0
  33. data/lib/parse/model/core/errors.rb +9 -0
  34. data/lib/parse/model/core/fetching.rb +42 -38
  35. data/lib/parse/model/core/properties.rb +46 -27
  36. data/lib/parse/model/core/querying.rb +231 -228
  37. data/lib/parse/model/core/schema.rb +76 -74
  38. data/lib/parse/model/date.rb +10 -2
  39. data/lib/parse/model/file.rb +16 -2
  40. data/lib/parse/model/geopoint.rb +9 -2
  41. data/lib/parse/model/model.rb +38 -7
  42. data/lib/parse/model/object.rb +60 -19
  43. data/lib/parse/model/pointer.rb +22 -1
  44. data/lib/parse/model/push.rb +6 -2
  45. data/lib/parse/query.rb +57 -11
  46. data/lib/parse/query/constraint.rb +5 -2
  47. data/lib/parse/query/constraints.rb +588 -589
  48. data/lib/parse/query/ordering.rb +2 -2
  49. data/lib/parse/stack.rb +1 -0
  50. data/lib/parse/stack/version.rb +1 -1
  51. data/lib/parse/webhooks.rb +30 -29
  52. data/lib/parse/webhooks/payload.rb +181 -168
  53. data/lib/parse/webhooks/registration.rb +1 -1
  54. data/parse-stack.gemspec +9 -9
  55. metadata +9 -12
@@ -24,7 +24,7 @@ module Parse
24
24
  # @!attribute [rw] field
25
25
  # @return [Symbol] the name of the field
26
26
  attr_accessor :field
27
-
27
+
28
28
  # @!attribute [rw] direction
29
29
  # The direction of the sorting. This is either `:asc` or `:desc`.
30
30
  # @return [Symbol]
@@ -59,7 +59,7 @@ module Parse
59
59
 
60
60
  end
61
61
 
62
- # Add all the operator instance methods to the symbol classes
62
+ # Extension to add all the operator instance methods to the Symbol classe
63
63
  class Symbol
64
64
  Parse::Order::ORDERING.keys.each do |sym|
65
65
  define_method(sym) do
data/lib/parse/stack.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'webhooks'
9
9
 
10
10
 
11
11
  module Parse
12
+ class Error < StandardError; end;
12
13
  module Stack
13
14
 
14
15
  # Your code goes here...
@@ -6,6 +6,6 @@ module Parse
6
6
  # The Parse Server SDK for Ruby
7
7
  module Stack
8
8
  # The current version.
9
- VERSION = "1.5.3"
9
+ VERSION = "1.6.0"
10
10
  end
11
11
  end
@@ -51,7 +51,7 @@ module Parse
51
51
  #
52
52
  # end
53
53
  # @param type (see Parse::Webhooks.route)
54
- # @yield the body of the function to be evaluated in the scope of a {Parse::Payload} instance.
54
+ # @yield the body of the function to be evaluated in the scope of a {Parse::Webhooks::Payload} instance.
55
55
  # @param block [Symbol] the name of the method to call, if no block is passed.
56
56
  # @return (see Parse::Webhooks.route)
57
57
  def self.webhook(type, block = nil)
@@ -75,28 +75,20 @@ module Parse
75
75
 
76
76
  end
77
77
 
78
- class Payload
79
- # This method will intentionally raise a {WebhookErrorResponse}, which when used inside
80
- # of a registered cloud code webhook function or trigger, will halt processing
81
- # and return the proper error response code back to the Parse server.
82
- # @param msg [String] the error message
83
- # @raise WebhookErrorResponse
84
- # @return [WebhookErrorResponse]
85
- def error!(msg = "")
86
- raise WebhookErrorResponse, msg
87
- end
88
- end
89
-
90
- # The error to be raised in registered trigger or function webhook blocks that
91
- # will trigger the Parse::Webhooks application to return the proper error response.
92
- class WebhookErrorResponse < StandardError; end;
93
78
  # A Rack-based application middlware to handle incoming Parse cloud code webhook
94
79
  # requests.
95
80
  class Webhooks
81
+ # The error to be raised in registered trigger or function webhook blocks that
82
+ # will trigger the Parse::Webhooks application to return the proper error response.
83
+ class ResponseError < StandardError; end;
84
+
96
85
  include Client::Connectable
97
- extend Webhook::Registration
86
+ extend Parse::Webhooks::Registration
87
+ # The name of the incoming env containing the webhook key.
98
88
  HTTP_PARSE_WEBHOOK = "HTTP_X_PARSE_WEBHOOK_KEY"
89
+ # The name of the incoming env containing the application id key.
99
90
  HTTP_PARSE_APPLICATION_ID = "HTTP_X_PARSE_APPLICATION_ID"
91
+ # The content type that needs to be sent back to Parse server.
100
92
  CONTENT_TYPE = "application/json"
101
93
 
102
94
  # @!attribute key
@@ -173,7 +165,7 @@ module Parse
173
165
  # This method is usually called when an incoming request from Parse Server is received.
174
166
  # @param type (see route)
175
167
  # @param className (see route)
176
- # @param payload [Parse::Payload] the payload object received from the server.
168
+ # @param payload [Parse::Webhooks::Payload] the payload object received from the server.
177
169
  # @return [Object] the result of the trigger or function.
178
170
  def call_route(type, className, payload = nil)
179
171
  type = type.to_s.underscore.to_sym #support camelcase
@@ -219,6 +211,9 @@ module Parse
219
211
  { error: data }.to_json
220
212
  end
221
213
 
214
+ # Returns the configured webhook key if available. By default it will use
215
+ # the value of ENV['PARSE_WEBHOOK_KEY'] if not configured.
216
+ # @return [String]
222
217
  def key
223
218
  @key ||= ENV['PARSE_WEBHOOK_KEY']
224
219
  end
@@ -226,12 +221,18 @@ module Parse
226
221
  # Standard Rack call method. This method processes an incoming cloud code
227
222
  # webhook request from Parse Server, validates it and executes any registered handlers for it.
228
223
  # The result of the handler for the matching webhook request is sent back to
229
- # Parse Server. If the handler raises a {Parse::WebhookErrorResponse},
224
+ # Parse Server. If the handler raises a {Parse::Webhooks::ResponseError},
230
225
  # it will return the proper error response.
226
+ # @raise Parse::Webhooks::ResponseError whenever {Parse::Object}, ActiveModel::ValidationError
231
227
  # @param env [Hash] the environment hash in a Rack request.
232
- # @return [Object] the value of calling `finish` on the Rack::Response object.
233
- # @todo Create a call! method that takes a dup of this env.
228
+ # @return [Array] the value of calling `finish` on the {http://www.rubydoc.info/github/rack/rack/Rack/Response Rack::Response} object.
234
229
  def call(env)
230
+ # Thraed safety
231
+ dup.call!(env)
232
+ end
233
+
234
+ # @!visibility private
235
+ def call!(env)
235
236
 
236
237
  request = Rack::Request.new env
237
238
  response = Rack::Response.new
@@ -248,7 +249,7 @@ module Parse
248
249
 
249
250
  request.body.rewind
250
251
  begin
251
- payload = Parse::Payload.new request.body.read
252
+ payload = Parse::Webhooks::Payload.new request.body.read
252
253
  rescue => e
253
254
  warn "Invalid webhook payload format: #{e}"
254
255
  response.write error("Invalid payload format. Should be valid JSON.")
@@ -257,12 +258,12 @@ module Parse
257
258
 
258
259
  if self.logging.present?
259
260
  if payload.trigger?
260
- puts "[ParseWebhooks Request] --> #{payload.trigger_name} #{payload.parse_class}:#{payload.parse_id}"
261
+ puts "[Webhooks::Request] --> #{payload.trigger_name} #{payload.parse_class}:#{payload.parse_id}"
261
262
  elsif payload.function?
262
263
  puts "[ParseWebhooks Request] --> Function #{payload.function_name}"
263
264
  end
264
265
  if self.logging == :debug
265
- puts "[ParseWebhooks Payload] ----------------------------"
266
+ puts "[Webhooks::Payload] ----------------------------"
266
267
  puts payload.as_json
267
268
  puts "----------------------------------------------------\n"
268
269
  end
@@ -280,22 +281,22 @@ module Parse
280
281
  generic_result = Parse::Webhooks.call_route(payload.trigger_name, "*", payload)
281
282
  result = generic_result if generic_result.present? && result.nil?
282
283
  else
283
- puts "[ParseWebhooks] --> Could not find mapping route for #{payload.to_json}"
284
+ puts "[Webhooks] --> Could not find mapping route for #{payload.to_json}"
284
285
  end
285
286
 
286
287
  result = true if result.nil?
287
288
  if self.logging.present?
288
- puts "[ParseWebhooks Response] ----------------------------"
289
+ puts "[Webhooks::Response] ----------------------------"
289
290
  puts success(result)
290
291
  puts "----------------------------------------------------\n"
291
292
  end
292
293
  response.write success(result)
293
294
  return response.finish
294
- rescue Parse::WebhookErrorResponse, ActiveModel::ValidationError => e
295
+ rescue Parse::Webhooks::ResponseError, ActiveModel::ValidationError => e
295
296
  if payload.trigger?
296
- puts "[Webhook ResponseError] >> #{payload.trigger_name} #{payload.parse_class}:#{payload.parse_id}: #{e}"
297
+ puts "[Webhooks::ResponseError] >> #{payload.trigger_name} #{payload.parse_class}:#{payload.parse_id}: #{e}"
297
298
  elsif payload.function?
298
- puts "[Webhook ResponseError] >> #{payload.function_name}: #{e}"
299
+ puts "[Webhooks::ResponseError] >> #{payload.function_name}: #{e}"
299
300
  end
300
301
  response.write error( e.to_s )
301
302
  return response.finish
@@ -10,178 +10,191 @@ require 'active_support/core_ext'
10
10
  require 'active_model_serializers'
11
11
 
12
12
  module Parse
13
- # Represents the data structure that Parse server sends to a registered webhook.
14
- # Parse Parse allows you to receive Cloud Code webhooks on your own hosted
15
- # server. The `Parse::Webhooks` class is a lightweight Rack application that
16
- # routes incoming Cloud Code webhook requests and payloads to locally
17
- # registered handlers. The payloads are `Parse::Payload` type of objects that
18
- # represent that data that Parse sends webhook handlers.
19
- class Payload
20
- ATTRIBUTES = { master: nil, user: nil,
21
- installationId: nil, params: nil,
22
- functionName: nil, object: nil,
23
- original: nil, update: nil,
24
- triggerName: nil }.freeze
25
- include ::ActiveModel::Serializers::JSON
26
- # @!attribute [rw] master
27
- # @return [Boolean] whether the master key was used for this request.
28
- # @!attribute [rw] user
29
- # @return [Parse::User] the user who performed this request or action.
30
- # @!attribute [rw] installation_id
31
- # @return [String] The identifier of the device that submitted the request.
32
- # @!attribute [rw] params
33
- # @return [Hash] The list of function arguments submitted for a function request.
34
- # @!attribute [rw] function_name
35
- # @return [String] the name of the function.
36
- # @!attribute [rw] object
37
- # In a beforeSave, this attribute is the final object that will be persisted.
38
- # @return [Hash] the object hash related to a webhook trigger request.
39
- # @see #parse_object
40
- # @!attribute [rw] trigger_name
41
- # @return [String] the name of the trigger (ex. beforeSave, afterSave, etc.)
42
- # @!attribute [rw] original
43
- # In a beforeSave, for previously saved objects, this attribute is the Parse::Object
44
- # that was previously in the persistent store.
45
- # @return [Hash] the object hash related to a webhook trigger request.
46
- # @see #parse_object
47
- # @!attribute [rw] raw
48
- # @return [Hash] the raw payload from Parse server.
49
- # @!attribute [rw] update
50
- # @return [Hash] the update payload in the request.
51
- attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name
52
-
53
- attr_accessor :original, :update, :raw
54
- alias_method :installationId, :installation_id
55
- alias_method :functionName, :function_name
56
- alias_method :triggerName, :trigger_name
57
-
58
- # You would normally never create a Parse::Payload object since it is automatically
59
- # provided to you when using Parse::Webhooks.
60
- # @see Parse::Webhooks
61
- def initialize(hash = {})
62
- hash = JSON.parse(hash) if hash.is_a?(String)
63
- hash = Hash[hash.map{ |k, v| [k.to_s.underscore.to_sym, v] }]
64
- @raw = hash
65
- @master = hash[:master]
66
- @user = Parse::User.new hash[:user] if hash[:user].present?
67
- @installation_id = hash[:installation_id]
68
- @params = hash[:params]
69
- @params = @params.with_indifferent_access if @params.is_a?(Hash)
70
- @function_name = hash[:function_name]
71
- @object = hash[:object]
72
- @trigger_name = hash[:trigger_name]
73
- @original = hash[:original]
74
- @update = hash[:update] || {} #it comes as an update hash
75
- end
76
-
77
- # @return [Hash]
78
- def attributes
79
- ATTRIBUTES
80
- end
81
-
82
- # true if this is a webhook function request.
83
- def function?
84
- @function_name.present?
85
- end
86
-
87
- # @return [String] the name of the Parse class for this request.
88
- def parse_class
89
- return nil unless @object.present?
90
- @object[Parse::Model::KEY_CLASS_NAME] || @object[:className]
91
- end
92
-
93
- # @return [String] the objectId in this request.
94
- def parse_id
95
- return nil unless @object.present?
96
- @object[Parse::Model::OBJECT_ID] || @object[:objectId]
97
- end; alias_method :objectId, :parse_id
13
+ class Webhooks
14
+ # Represents the data structure that Parse server sends to a registered webhook.
15
+ # Parse Parse allows you to receive Cloud Code webhooks on your own hosted
16
+ # server. The `Parse::Webhooks` class is a lightweight Rack application that
17
+ # routes incoming Cloud Code webhook requests and payloads to locally
18
+ # registered handlers. The payloads are {Parse::Webhooks::Payload} type of objects that
19
+ # represent that data that Parse sends webhook handlers.
20
+ class Payload
21
+ # The set of keys that can be contained in a Parse hash payload for a webhook.
22
+ ATTRIBUTES = { master: nil, user: nil,
23
+ installationId: nil, params: nil,
24
+ functionName: nil, object: nil,
25
+ original: nil, update: nil,
26
+ triggerName: nil }.freeze
27
+ include ::ActiveModel::Serializers::JSON
28
+ # @!attribute [rw] master
29
+ # @return [Boolean] whether the master key was used for this request.
30
+ # @!attribute [rw] user
31
+ # @return [Parse::User] the user who performed this request or action.
32
+ # @!attribute [rw] installation_id
33
+ # @return [String] The identifier of the device that submitted the request.
34
+ # @!attribute [rw] params
35
+ # @return [Hash] The list of function arguments submitted for a function request.
36
+ # @!attribute [rw] function_name
37
+ # @return [String] the name of the function.
38
+ # @!attribute [rw] object
39
+ # In a beforeSave, this attribute is the final object that will be persisted.
40
+ # @return [Hash] the object hash related to a webhook trigger request.
41
+ # @see #parse_object
42
+ # @!attribute [rw] trigger_name
43
+ # @return [String] the name of the trigger (ex. beforeSave, afterSave, etc.)
44
+ # @!attribute [rw] original
45
+ # In a beforeSave, for previously saved objects, this attribute is the Parse::Object
46
+ # that was previously in the persistent store.
47
+ # @return [Hash] the object hash related to a webhook trigger request.
48
+ # @see #parse_object
49
+ # @!attribute [rw] raw
50
+ # @return [Hash] the raw payload from Parse server.
51
+ # @!attribute [rw] update
52
+ # @return [Hash] the update payload in the request.
53
+ attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name
54
+
55
+ attr_accessor :original, :update, :raw
56
+ alias_method :installationId, :installation_id
57
+ alias_method :functionName, :function_name
58
+ alias_method :triggerName, :trigger_name
59
+
60
+ # You would normally never create a {Parse::Webhooks::Payload} object since it is automatically
61
+ # provided to you when using Parse::Webhooks.
62
+ # @see Parse::Webhooks
63
+ def initialize(hash = {})
64
+ hash = JSON.parse(hash) if hash.is_a?(String)
65
+ hash = Hash[hash.map{ |k, v| [k.to_s.underscore.to_sym, v] }]
66
+ @raw = hash
67
+ @master = hash[:master]
68
+ @user = Parse::User.new hash[:user] if hash[:user].present?
69
+ @installation_id = hash[:installation_id]
70
+ @params = hash[:params]
71
+ @params = @params.with_indifferent_access if @params.is_a?(Hash)
72
+ @function_name = hash[:function_name]
73
+ @object = hash[:object]
74
+ @trigger_name = hash[:trigger_name]
75
+ @original = hash[:original]
76
+ @update = hash[:update] || {} #it comes as an update hash
77
+ end
78
+
79
+ # @return [ATTRIBUTES]
80
+ def attributes
81
+ ATTRIBUTES
82
+ end
83
+
84
+ # true if this is a webhook function request.
85
+ def function?
86
+ @function_name.present?
87
+ end
88
+
89
+ # @return [String] the name of the Parse class for this request.
90
+ def parse_class
91
+ return nil unless @object.present?
92
+ @object[Parse::Model::KEY_CLASS_NAME] || @object[:className]
93
+ end
94
+
95
+ # @return [String] the objectId in this request.
96
+ def parse_id
97
+ return nil unless @object.present?
98
+ @object[Parse::Model::OBJECT_ID] || @object[:objectId]
99
+ end; alias_method :objectId, :parse_id
100
+
101
+ # true if this is a webhook trigger request.
102
+ def trigger?
103
+ @trigger_name.present?
104
+ end
105
+
106
+ # true if this is a beforeSave or beforeDelete webhook trigger request.
107
+ def before_trigger?
108
+ before_save? || before_delete?
109
+ end
110
+
111
+ # true if this is a afterSave or afterDelete webhook trigger request.
112
+ def after_trigger?
113
+ after_save? || after_delete?
114
+ end
115
+
116
+ # true if this is a beforeSave webhook trigger request.
117
+ def before_save?
118
+ trigger? && @trigger_name.to_sym == :beforeSave
119
+ end
120
+
121
+ # true if this is a afterSave webhook trigger request.
122
+ def after_save?
123
+ trigger? && @trigger_name.to_sym == :afterSave
124
+ end
125
+
126
+ # true if this is a beforeDelete webhook trigger request.
127
+ def before_delete?
128
+ trigger? && @trigger_name.to_sym == :beforeDelete
129
+ end
130
+
131
+ # true if this is a afterDelete webhook trigger request.
132
+ def after_delete?
133
+ trigger? && @trigger_name.to_sym == :afterDelete
134
+ end
135
+
136
+ # true if this request is a trigger that contains an object.
137
+ def object?
138
+ trigger? && @object.present?
139
+ end
140
+
141
+ # @return [Parse::Object] a Parse::Object from the original object
142
+ def original_parse_object
143
+ return nil unless @original.is_a?(Hash)
144
+ Parse::Object.build(@original)
145
+ end
146
+
147
+ # This method returns a Parse::Object by combining the original object, if was provided,
148
+ # with the final object. This will return a dirty tracked Parse::Object subclass,
149
+ # that will have information on which fields have changed between the previous state
150
+ # in the persistent store and the one about to be saved.
151
+ # @param pristine [Boolean] whether the object should be returned without dirty tracking.
152
+ # @return [Parse::Object] a dirty tracked Parse::Object subclass instance
153
+ def parse_object(pristine = false)
154
+ return nil unless object?
155
+ return Parse::Object.build(@object) if pristine
156
+ # if its a before trigger, then we build the original object and apply the updates
157
+ # in order to create a Parse::Object that has the dirty tracking information
158
+ # if no original is nil, then it means this is a brand new object, so we create
159
+ # one from the className
160
+ if before_trigger?
161
+ # if original is present, then this is a modified object
162
+ if @original.present? && @original.is_a?(Hash)
163
+ o = Parse::Object.build @original
164
+ o.apply_attributes! @object, dirty_track: true
98
165
 
99
- # true if this is a webhook trigger request.
100
- def trigger?
101
- @trigger_name.present?
102
- end
103
-
104
- # true if this is a beforeSave or beforeDelete webhook trigger request.
105
- def before_trigger?
106
- before_save? || before_delete?
107
- end
108
-
109
- # true if this is a afterSave or afterDelete webhook trigger request.
110
- def after_trigger?
111
- after_save? || after_delete?
112
- end
113
-
114
- # true if this is a beforeSave webhook trigger request.
115
- def before_save?
116
- trigger? && @trigger_name.to_sym == :beforeSave
117
- end
118
-
119
- # true if this is a afterSave webhook trigger request.
120
- def after_save?
121
- trigger? && @trigger_name.to_sym == :afterSave
122
- end
123
-
124
- # true if this is a beforeDelete webhook trigger request.
125
- def before_delete?
126
- trigger? && @trigger_name.to_sym == :beforeDelete
127
- end
128
-
129
- # true if this is a afterDelete webhook trigger request.
130
- def after_delete?
131
- trigger? && @trigger_name.to_sym == :afterDelete
132
- end
133
-
134
- # true if this request is a trigger that contains an object.
135
- def object?
136
- trigger? && @object.present?
137
- end
138
-
139
- # @return [Parse::Object] a Parse::Object from the original object
140
- def original_parse_object
141
- return nil unless @original.is_a?(Hash)
142
- Parse::Object.build(@original)
143
- end
144
-
145
- # This method returns a Parse::Object by combining the original object, if was provided,
146
- # with the final object. This will return a dirty tracked Parse::Object subclass,
147
- # that will have information on which fields have changed between the previous state
148
- # in the persistent store and the one about to be saved.
149
- # @param pristine [Boolean] whether the object should be returned without dirty tracking.
150
- # @return [Parse::Object] a dirty tracked Parse::Object subclass instance
151
- def parse_object(pristine = false)
152
- return nil unless object?
153
- return Parse::Object.build(@object) if pristine
154
- # if its a before trigger, then we build the original object and apply the updates
155
- # in order to create a Parse::Object that has the dirty tracking information
156
- # if no original is nil, then it means this is a brand new object, so we create
157
- # one from the className
158
- if before_trigger?
159
- # if original is present, then this is a modified object
160
- if @original.present? && @original.is_a?(Hash)
161
- o = Parse::Object.build @original
162
- o.apply_attributes! @object, dirty_track: true
163
-
164
- if o.is_a?(Parse::User) && @update.present? && @update["authData"].present?
165
- o.auth_data = @update["authData"]
166
- end
167
- return o
168
- else #else the object must be new
169
- klass = Parse::Object.find_class parse_class
170
- # if we have a class, return that with updated changes, otherwise
171
- # default to regular object
172
- if klass.present?
173
- o = klass.new(@object || {})
174
166
  if o.is_a?(Parse::User) && @update.present? && @update["authData"].present?
175
167
  o.auth_data = @update["authData"]
176
168
  end
177
169
  return o
178
- end # if klass.present?
179
- end # if we have original
180
-
181
- end # if before_trigger?
182
- Parse::Object.build(@object)
170
+ else #else the object must be new
171
+ klass = Parse::Object.find_class parse_class
172
+ # if we have a class, return that with updated changes, otherwise
173
+ # default to regular object
174
+ if klass.present?
175
+ o = klass.new(@object || {})
176
+ if o.is_a?(Parse::User) && @update.present? && @update["authData"].present?
177
+ o.auth_data = @update["authData"]
178
+ end
179
+ return o
180
+ end # if klass.present?
181
+ end # if we have original
182
+
183
+ end # if before_trigger?
184
+ Parse::Object.build(@object)
185
+ end
186
+
187
+ # This method will intentionally raise a {Parse::Webhooks::ResponseError} with
188
+ # a specific message. When used inside of a registered cloud code webhook
189
+ # function or trigger, will halt processing and return the proper error response
190
+ # code back to the Parse server.
191
+ # @param msg [String] the error message to send back.
192
+ # @raise Parse::Webhooks::ResponseError
193
+ # @return [Parse::Webhooks::ResponseError] the raised exception
194
+ def error!(msg = "")
195
+ raise Parse::Webhooks::ResponseError, msg
196
+ end
197
+
198
+ end # Payload
183
199
  end
184
-
185
- end # Payload
186
-
187
200
  end