Class: Parse::Webhooks

Inherits:
Object
  • Object
show all
Extended by:
Registration
Includes:
Client::Connectable
Defined in:
lib/parse/webhooks.rb,
lib/parse/webhooks/payload.rb,
lib/parse/webhooks/registration.rb

Overview

Interface to the CloudCode webhooks API.

Defined Under Namespace

Modules: Registration Classes: Payload, ResponseError

Constant Summary collapse

HTTP_PARSE_WEBHOOK =

The name of the incoming env containing the webhook key.

"HTTP_X_PARSE_WEBHOOK_KEY"
HTTP_PARSE_APPLICATION_ID =

The name of the incoming env containing the application id key.

"HTTP_X_PARSE_APPLICATION_ID"
CONTENT_TYPE =

The content type that needs to be sent back to Parse server.

"application/json"

Constants included from Registration

Registration::ALLOWED_HOOKS

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Registration

register_functions!, register_triggers!, register_webhook!, remove_all_functions!, remove_all_triggers!

Methods included from Client::Connectable

#client

Class Attribute Details

.keyString

Returns the configured webhook key if available. By default it will use the value of ENV if not configured.

Returns:

 216 217 218
# File 'lib/parse/webhooks.rb', line 216 def key @key end

.loggingBoolean

Returns whether to print additional logging information. You may also set this to `:debug` for additional verbosity.

Returns:

  • (Boolean)

    whether to print additional logging information. You may also set this to `:debug` for additional verbosity.

 107 108 109
# File 'lib/parse/webhooks.rb', line 107 def logging @logging end

Class Method Details

.call(env) ⇒ Array

Standard Rack call method. This method processes an incoming cloud code webhook request from Parse Server, validates it and executes any registered handlers for it. The result of the handler for the matching webhook request is sent back to Parse Server. If the handler raises a ResponseError, it will return the proper error response.

Parameters:

  • env (Hash)

    the environment hash in a Rack request.

Returns:

Raises:

  • Parse::Webhooks::ResponseError whenever Object, ActiveModel::ValidationError

 230 231 232 233
# File 'lib/parse/webhooks.rb', line 230 def call(env) # Thraed safety  dup.call!(env) end

.call_route(type, className, payload = nil) ⇒ Object

Calls the set of registered webhook trigger blocks or the specific function block. This method is usually called when an incoming request from Parse Server is received.

Parameters:

  • payload (Parse::Webhooks::Payload) (defaults to: nil)

    the payload object received from the server.

  • type (Symbol)

    The type of cloud code webhook to register. This can be any of the supported routes. These are `:before_save`, `:after_save`,

  • className (String)

    if `type` is not `:function`, then this registers a trigger for the given className. Otherwise, className is treated to be the function name to register with Parse server.

Returns:

  • (Object)

    the result of the trigger or function.

 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
# File 'lib/parse/webhooks.rb', line 165 def call_route(type, className, payload = nil) type = type.to_s.underscore.to_sym #support camelcase  className = className.parse_class if className.respond_to?(:parse_class) className = className.to_s return unless routes[type].present? && routes[type][className].present? registry = routes[type][className] if registry.is_a?(Array) result = registry.map { |hook| payload.instance_exec(payload, &hook) }.last else result = payload.instance_exec(payload, &registry) end if result.is_a?(Parse::Object) # if it is a Parse::Object, we will call the registered ActiveModel callbacks  # and then send the proper changes payload  if type == :before_save # returning false from the callback block only runs the before_* callback  result.prepare_save! result = result.changes_payload elsif type == :before_delete result.run_callbacks(:destroy) { false } result = true end elsif type == :before_save && (result == true || result.nil?) # Open Source Parse server does not accept true results on before_save hooks.  result = {} end result end

.error(data = false) ⇒ Hash

Generates an error response for Parse Server.

Parameters:

  • data (Object) (defaults to: false)

    the data to send back with the error.

Returns:

  • (Hash)

    a error data payload

 208 209 210
# File 'lib/parse/webhooks.rb', line 208 def error(data = false) { error: data }.to_json end

.route(type, className, block = nil) { ... } ⇒ OpenStruct

Internally registers a route for a specific webhook trigger or function. `:before_delete`, `:after_delete` or `:function`.

Parameters:

  • type (Symbol)

    The type of cloud code webhook to register. This can be any of the supported routes. These are `:before_save`, `:after_save`,

  • className (String)

    if `type` is not `:function`, then this registers a trigger for the given className. Otherwise, className is treated to be the function name to register with Parse server.

Yields:

  • the block that will handle of the webhook trigger or function.

Returns:

  • (OpenStruct)
 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
# File 'lib/parse/webhooks.rb', line 128 def route(type, className, block = nil) type = type.to_s.underscore.to_sym #support camelcase  if type != :function && className.respond_to?(:parse_class) className = className.parse_class end className = className.to_s block = Proc.new if block_given? if routes[type].nil? || block.respond_to?(:call) == false raise ArgumentError, "Invalid Webhook registration trigger #{type} #{className}" end # AfterSave/AfterDelete hooks support more than one  if type == :after_save || type == :after_delete routes[type][className] ||= [] routes[type][className].push block else routes[type][className] = block end @routes end

.routesOpenStruct

A hash-like structure composing of all the registered webhook triggers and functions. These are `:before_save`, `:after_save`, `:before_delete`, `:after_delete` or `:function`.

Returns:

  • (OpenStruct)
 113 114 115 116 117
# File 'lib/parse/webhooks.rb', line 113 def routes return @routes unless @routes.nil? r = Parse::API::Hooks::TRIGGER_NAMES_LOCAL + [:function] @routes = OpenStruct.new(r.reduce({}) { |h, t| h[t] = {}; h }) end

.run_function(name, params) ⇒ Object

Run a locally registered webhook function. This bypasses calling a function through Parse-Server if the method handler is registered locally.

Returns:

  • (Object)

    the result of the function.

 152 153 154 155 156 157
# File 'lib/parse/webhooks.rb', line 152 def run_function(name, params) payload = Payload.new payload.function_name = name payload.params = params call_route(:function, name, payload) end

.success(data = true) ⇒ Hash

Generates a success response for Parse Server.

Parameters:

  • data (Object) (defaults to: true)

    the data to send back with the success.

Returns:

  • (Hash)

    a success data payload

 201 202 203
# File 'lib/parse/webhooks.rb', line 201 def success(data = true) { success: data }.to_json end

Instance Method Details

#keyString

The Parse Webhook Key to be used for authenticating webhook requests. See key on setting this value.

Returns:

 94 95 96
# File 'lib/parse/webhooks.rb', line 94 def key self.class.key end