Class: Parse::Webhooks::Payload

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON
Defined in:
lib/parse/webhooks/payload.rb

Overview

Represents the data structure that Parse server sends to a registered webhook. Parse Parse allows you to receive Cloud Code webhooks on your own hosted server. The `Parse::Webhooks` class is a lightweight Rack application that routes incoming Cloud Code webhook requests and payloads to locally registered handlers. The payloads are Payload type of objects that represent that data that Parse sends webhook handlers.

Constant Summary collapse

ATTRIBUTES =

The set of keys that can be contained in a Parse hash payload for a webhook.

{ master: nil, user: nil,
installationId: nil, params: nil,
functionName: nil, object: nil,
original: nil, update: nil,
query: nil, log: nil,
objects: nil,
triggerName: nil }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Payload

You would normally never create a Parse::Webhooks::Payload object since it is automatically provided to you when using Parse::Webhooks.

See Also:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/parse/webhooks/payload.rb', line 76

def initialize(hash = {})
  hash = JSON.parse(hash) if hash.is_a?(String)
  hash = Hash[hash.map { |k, v| [k.to_s.underscore.to_sym, v] }]
  @raw = hash
  @master = hash[:master]
  @user = Parse::User.new hash[:user] if hash[:user].present?
  @installation_id = hash[:installation_id]
  @params = hash[:params]
  @params = @params.with_indifferent_access if @params.is_a?(Hash)
  @function_name = hash[:function_name]
  @object = hash[:object]
  @trigger_name = hash[:trigger_name]
  @original = hash[:original]
  @update = hash[:update] || {} #it comes as an update hash
  # Added for beforeFind and afterFind triggers
  @query = hash[:query]
  @objects = hash[:objects] || []
  @log = hash[:log]
end

Instance Attribute Details

#function_nameString Also known as: functionName

Returns the name of the function.

Returns:

  • (String)

    the name of the function.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#installation_idString Also known as: installationId

Returns The identifier of the device that submitted the request.

Returns:

  • (String)

    The identifier of the device that submitted the request.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#logObject

The query request in a beforeFind trigger. Available in Parse Server 2.3.1 or later.

@return [Parse::Query]

The set of matching objects in an afterFind trigger. Available in Parse Server 2.3.1 or later.

@return [<Parse::Object>]

Logging information if available. Available in Parse Server 2.3.1 or later.

@return [Hash] the set of matching objects in an afterFind trigger.


64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#masterBoolean

Returns whether the master key was used for this request.

Returns:

  • (Boolean)

    whether the master key was used for this request.



64
65
66
# File 'lib/parse/webhooks/payload.rb', line 64

def master
  @master
end

#objectHash

In a beforeSave, this attribute is the final object that will be persisted.

Returns:

  • (Hash)

    the object hash related to a webhook trigger request.

See Also:



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#objectsObject

The query request in a beforeFind trigger. Available in Parse Server 2.3.1 or later.

@return [Parse::Query]

The set of matching objects in an afterFind trigger. Available in Parse Server 2.3.1 or later.

@return [<Parse::Object>]

Logging information if available. Available in Parse Server 2.3.1 or later.

@return [Hash] the set of matching objects in an afterFind trigger.


64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#originalHash

In a beforeSave, for previously saved objects, this attribute is the Parse::Object that was previously in the persistent store.

Returns:

  • (Hash)

    the object hash related to a webhook trigger request.

See Also:



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#paramsHash

Returns The list of function arguments submitted for a function request.

Returns:

  • (Hash)

    The list of function arguments submitted for a function request.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#queryObject

The query request in a beforeFind trigger. Available in Parse Server 2.3.1 or later.

@return [Parse::Query]

The set of matching objects in an afterFind trigger. Available in Parse Server 2.3.1 or later.

@return [<Parse::Object>]

Logging information if available. Available in Parse Server 2.3.1 or later.

@return [Hash] the set of matching objects in an afterFind trigger.


64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#rawHash

Returns the raw payload from Parse server.

Returns:

  • (Hash)

    the raw payload from Parse server.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#trigger_nameString Also known as: triggerName

Returns the name of the trigger (ex. beforeSave, afterSave, etc.).

Returns:

  • (String)

    the name of the trigger (ex. beforeSave, afterSave, etc.)



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#updateHash

Returns the update payload in the request.

Returns:

  • (Hash)

    the update payload in the request.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

#userParse::User

Returns the user who performed this request or action.

Returns:

  • (Parse::User)

    the user who performed this request or action.



64
# File 'lib/parse/webhooks/payload.rb', line 64

attr_accessor :master, :user, :installation_id, :params, :function_name, :object, :trigger_name

Instance Method Details

#after_delete?Boolean

true if this is a afterDelete webhook trigger request.

Returns:

  • (Boolean)


162
163
164
# File 'lib/parse/webhooks/payload.rb', line 162

def after_delete?
  trigger? && @trigger_name.to_sym == :afterDelete
end

#after_find?Boolean

true if this is a afterFind webhook trigger request.

Returns:

  • (Boolean)


172
173
174
# File 'lib/parse/webhooks/payload.rb', line 172

def after_find?
  trigger? && @trigger_name.to_sym == :afterFind
end

#after_save?Boolean

true if this is a afterSave webhook trigger request.

Returns:

  • (Boolean)


152
153
154
# File 'lib/parse/webhooks/payload.rb', line 152

def after_save?
  trigger? && @trigger_name.to_sym == :afterSave
end

#after_trigger?Boolean

true if this is a afterSave or afterDelete webhook trigger request.

Returns:

  • (Boolean)


142
143
144
# File 'lib/parse/webhooks/payload.rb', line 142

def after_trigger?
  after_save? || after_delete? || after_find?
end

#attributesATTRIBUTES

Returns:



97
98
99
# File 'lib/parse/webhooks/payload.rb', line 97

def attributes
  ATTRIBUTES
end

#before_delete?Boolean

true if this is a beforeDelete webhook trigger request.

Returns:

  • (Boolean)


157
158
159
# File 'lib/parse/webhooks/payload.rb', line 157

def before_delete?
  trigger? && @trigger_name.to_sym == :beforeDelete
end

#before_find?Boolean

true if this is a beforeFind webhook trigger request.

Returns:

  • (Boolean)


167
168
169
# File 'lib/parse/webhooks/payload.rb', line 167

def before_find?
  trigger? && @trigger_name.to_sym == :beforeFind
end

#before_save?Boolean

true if this is a beforeSave webhook trigger request.

Returns:

  • (Boolean)


147
148
149
# File 'lib/parse/webhooks/payload.rb', line 147

def before_save?
  trigger? && @trigger_name.to_sym == :beforeSave
end

#before_trigger?Boolean

true if this is a beforeSave or beforeDelete webhook trigger request.

Returns:

  • (Boolean)


137
138
139
# File 'lib/parse/webhooks/payload.rb', line 137

def before_trigger?
  before_save? || before_delete? || before_find?
end

#error!(msg = "") ⇒ Parse::Webhooks::ResponseError

This method will intentionally raise a ResponseError with a specific message. When used inside of a registered cloud code webhook function or trigger, will halt processing and return the proper error response code back to the Parse server.

Parameters:

  • msg (String) (defaults to: "")

    the error message to send back.

Returns:

Raises:

  • Parse::Webhooks::ResponseError



233
234
235
# File 'lib/parse/webhooks/payload.rb', line 233

def error!(msg = "")
  raise Parse::Webhooks::ResponseError, msg
end

#function?Boolean

true if this is a webhook function request.

Returns:

  • (Boolean)


113
114
115
# File 'lib/parse/webhooks/payload.rb', line 113

def function?
  @function_name.present?
end

#object?Boolean

true if this request is a trigger that contains an object.

Returns:

  • (Boolean)


177
178
179
# File 'lib/parse/webhooks/payload.rb', line 177

def object?
  trigger? && @object.present?
end

#original_parse_objectParse::Object

Returns a Parse::Object from the original object.

Returns:



182
183
184
185
# File 'lib/parse/webhooks/payload.rb', line 182

def original_parse_object
  return nil unless @original.is_a?(Hash)
  Parse::Object.build(@original)
end

#parse_classString

Returns the name of the Parse class for this request.

Returns:

  • (String)

    the name of the Parse class for this request.



118
119
120
121
122
# File 'lib/parse/webhooks/payload.rb', line 118

def parse_class
  return @webhook_class if @webhook_class.present?
  return nil unless @object.present?
  @object[Parse::Model::KEY_CLASS_NAME] || @object[:className]
end

#parse_idString Also known as: objectId

Returns the objectId in this request.

Returns:

  • (String)

    the objectId in this request.



125
126
127
128
# File 'lib/parse/webhooks/payload.rb', line 125

def parse_id
  return nil unless @object.present?
  @object[Parse::Model::OBJECT_ID] || @object[:objectId]
end

#parse_object(pristine = false) ⇒ Parse::Object

This method returns a Parse::Object by combining the original object, if was provided, with the final object. This will return a dirty tracked Parse::Object subclass, that will have information on which fields have changed between the previous state in the persistent store and the one about to be saved.

Parameters:

  • pristine (Boolean) (defaults to: false)

    whether the object should be returned without dirty tracking.

Returns:

  • (Parse::Object)

    a dirty tracked Parse::Object subclass instance



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/parse/webhooks/payload.rb', line 193

def parse_object(pristine = false)
  return nil unless object?
  return Parse::Object.build(@object) if pristine
  # if its a before trigger, then we build the original object and apply the updates
  # in order to create a Parse::Object that has the dirty tracking information
  # if no original is nil, then it means this is a brand new object, so we create
  # one from the className
  if before_trigger?
    # if original is present, then this is a modified object
    if @original.present? && @original.is_a?(Hash)
      o = Parse::Object.build @original
      o.apply_attributes! @object, dirty_track: true

      if o.is_a?(Parse::User) && @update.present? && @update["authData"].present?
        o.auth_data = @update["authData"]
      end
      return o
    else #else the object must be new
      klass = Parse::Object.find_class parse_class
      # if we have a class, return that with updated changes, otherwise
      # default to regular object
      if klass.present?
        o = klass.new(@object || {})
        if o.is_a?(Parse::User) && @update.present? && @update["authData"].present?
          o.auth_data = @update["authData"]
        end
        return o
      end # if klass.present?
    end # if we have original
  end # if before_trigger?
  Parse::Object.build(@object)
end

#parse_queryParse::Query

Returns the Parse query for a beforeFind trigger.

Returns:

  • (Parse::Query)

    the Parse query for a beforeFind trigger.



238
239
240
241
# File 'lib/parse/webhooks/payload.rb', line 238

def parse_query
  return nil unless parse_class.present? && @query.is_a?(Hash)
  Parse::Query.new parse_class, @query
end

#trigger?Boolean

true if this is a webhook trigger request.

Returns:

  • (Boolean)


132
133
134
# File 'lib/parse/webhooks/payload.rb', line 132

def trigger?
  @trigger_name.present?
end

#wlog(s) ⇒ Object

Method to print to standard that utilizes the an internal id to make it easier to trace incoming requests.



103
104
105
106
107
108
109
110
# File 'lib/parse/webhooks/payload.rb', line 103

def wlog(s)
  # generates a unique random number in order to be used in logging. This
  # is useful when debugging issues in production where one server instance
  # may be running multiple threads and you want to trace the incoming call.
  @rid ||= rand(999).to_s.rjust(3)
  puts "[> #{@rid}] #{s}"
  @rid
end