Class: Parse::Webhooks
- Inherits:
-
Object
- Object
- Parse::Webhooks
- 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
Class Attribute Summary collapse
-
.key ⇒ String
Returns the configured webhook key if available.
-
.logging ⇒ Boolean
Whether to print additional logging information.
Class Method Summary collapse
-
.call(env) ⇒ Array
Standard Rack call method.
-
.call_route(type, className, payload = nil) ⇒ Object
Calls the set of registered webhook trigger blocks or the specific function block.
-
.error(data = false) ⇒ Hash
Generates an error response for Parse Server.
-
.route(type, className, block = nil) { ... } ⇒ OpenStruct
Internally registers a route for a specific webhook trigger or function.
-
.routes ⇒ OpenStruct
A hash-like structure composing of all the registered webhook triggers and functions.
-
.run_function(name, params) ⇒ Object
Run a locally registered webhook function.
-
.success(data = true) ⇒ Hash
Generates a success response for Parse Server.
Instance Method Summary collapse
-
#key ⇒ String
The Parse Webhook Key to be used for authenticating webhook requests.
Methods included from Registration
register_functions!, register_triggers!, register_webhook!, remove_all_functions!, remove_all_triggers!
Methods included from Client::Connectable
Class Attribute Details
.key ⇒ String
Returns the configured webhook key if available. By default it will use the value of ENV if not configured.
221 222 223 |
# File 'lib/parse/webhooks.rb', line 221 def key @key end |
.logging ⇒ Boolean
Returns whether to print additional logging information. You may also set this to ‘:debug` for additional verbosity.
111 112 113 |
# File 'lib/parse/webhooks.rb', line 111 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.
234 235 236 237 |
# File 'lib/parse/webhooks.rb', line 234 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.
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 197 198 199 200 201 |
# File 'lib/parse/webhooks.rb', line 170 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, ®istry) 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.
213 214 215 |
# File 'lib/parse/webhooks.rb', line 213 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`.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/parse/webhooks.rb', line 132 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 |
.routes ⇒ OpenStruct
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`.
117 118 119 120 121 |
# File 'lib/parse/webhooks.rb', line 117 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.
157 158 159 160 161 162 |
# File 'lib/parse/webhooks.rb', line 157 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.
206 207 208 |
# File 'lib/parse/webhooks.rb', line 206 def success(data = true) { success: data }.to_json end |