Module: Parse::API::Hooks

Included in:
Client
Defined in:
lib/parse/api/hooks.rb

Overview

Defines the Parse webhooks interface for the Parse REST API

Constant Summary collapse

TRIGGER_NAMES =

The allowed set of Parse webhook triggers. Mirrors Parse Server's triggers.Types so registration of the auth / LiveQuery / password- reset hooks is no longer pre-rejected by the SDK.

NOTE: this allowlist gates registration only. The webhook router in Webhooks currently shapes payloads for the object triggers (before/after save/delete/find); the login / connect / subscribe / password-reset payloads carry a different shape (no object) and their first-class routing is a follow-up. beforeConnect is a connection-global trigger whose Parse-canonical className is the @Connect sentinel; file triggers use @File. Both are accepted by the trigger-className validator (PathSegment.trigger_class_name!).

[
  :afterDelete, :afterFind, :afterSave,
  :beforeDelete, :beforeFind, :beforeSave,
  :beforeLogin, :afterLogin, :afterLogout, :beforePasswordResetRequest,
  :beforeConnect, :beforeSubscribe, :afterEvent,
].freeze

Instance Method Summary collapse

Instance Method Details

#create_function(functionName, url) ⇒ Parse::Response

Register a cloud code webhook function pointing to a endpoint url.

Parameters:

  • functionName (String)

    the name of the cloud code function.

  • url (String)

    the url endpoint for this cloud code function.

Returns:



77
78
79
# File 'lib/parse/api/hooks.rb', line 77

def create_function(functionName, url)
  request :post, "#{HOOKS_PREFIX}functions", body: { functionName: functionName, url: url }
end

#create_trigger(triggerName, className, url) ⇒ Parse::Response

Register a new cloud code webhook trigger with an endpoint url.

Parameters:

  • triggerName (String)

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

  • className (String)

    the name of the Parse collection for the trigger.

  • url (String)

    the url endpoint for this webhook trigger.

Returns:

See Also:



124
125
126
127
128
# File 'lib/parse/api/hooks.rb', line 124

def create_trigger(triggerName, className, url)
  triggerName = _verify_trigger(triggerName)
  body = { className: className, triggerName: triggerName, url: url }
  request :post, "#{HOOKS_PREFIX}triggers", body: body
end

#delete_function(functionName) ⇒ Parse::Response

Remove a registered cloud code webhook function.

Parameters:

  • functionName (String)

    the name of the cloud code function.

Returns:



95
96
97
98
# File 'lib/parse/api/hooks.rb', line 95

def delete_function(functionName)
  safe = Parse::API::PathSegment.identifier!(functionName, kind: "function name")
  request :put, "#{HOOKS_PREFIX}functions/#{safe}", body: { __op: "Delete" }
end

#delete_trigger(triggerName, className) ⇒ Parse::Response

Remove a registered cloud code webhook trigger.

Parameters:

  • triggerName (String)

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

  • className (String)

    the name of the Parse collection for the trigger.

Returns:

See Also:



147
148
149
150
151
# File 'lib/parse/api/hooks.rb', line 147

def delete_trigger(triggerName, className)
  triggerName = _verify_trigger(triggerName)
  safe_class = Parse::API::PathSegment.trigger_class_name!(className, kind: "class name")
  request :put, "#{HOOKS_PREFIX}triggers/#{safe_class}/#{triggerName}", body: { __op: "Delete" }
end

#fetch_function(functionName) ⇒ Parse::Response

Fetch information about a specific registered cloud function.

Parameters:

  • functionName (String)

    the name of the cloud code function.

Returns:



68
69
70
71
# File 'lib/parse/api/hooks.rb', line 68

def fetch_function(functionName)
  safe = Parse::API::PathSegment.identifier!(functionName, kind: "function name")
  request :get, "#{HOOKS_PREFIX}functions/#{safe}"
end

#fetch_trigger(triggerName, className) ⇒ Parse::Response

Fetch information about a registered webhook trigger.

Parameters:

  • triggerName (String)

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

  • className (String)

    the name of the Parse collection for the trigger.

Returns:

See Also:



112
113
114
115
116
# File 'lib/parse/api/hooks.rb', line 112

def fetch_trigger(triggerName, className)
  triggerName = _verify_trigger(triggerName)
  safe_class = Parse::API::PathSegment.trigger_class_name!(className, kind: "class name")
  request :get, "#{HOOKS_PREFIX}triggers/#{safe_class}/#{triggerName}"
end

#functionsParse::Response

Fetch all defined cloud code functions.

Returns:



60
61
62
63
# File 'lib/parse/api/hooks.rb', line 60

def functions
  opts = { cache: false }
  request :get, "#{HOOKS_PREFIX}functions", opts: opts
end

#triggersParse::Response

Get the set of registered triggers.

Returns:



102
103
104
105
# File 'lib/parse/api/hooks.rb', line 102

def triggers
  opts = { cache: false }
  request :get, "#{HOOKS_PREFIX}triggers", opts: opts
end

#update_function(functionName, url) ⇒ Parse::Response

Updated the endpoint url for a registered cloud code webhook function.

Parameters:

  • functionName (String)

    the name of the cloud code function.

  • url (String)

    the new url endpoint for this cloud code function.

Returns:



85
86
87
88
89
90
# File 'lib/parse/api/hooks.rb', line 85

def update_function(functionName, url)
  # If you add _method => "PUT" to the JSON body,
  # and send it as a POST request and parse will accept it as a PUT.
  safe = Parse::API::PathSegment.identifier!(functionName, kind: "function name")
  request :put, "#{HOOKS_PREFIX}functions/#{safe}", body: { url: url }
end

#update_trigger(triggerName, className, url) ⇒ Parse::Response

Updated the registered endpoint for this cloud code webhook trigger.

Parameters:

  • triggerName (String)

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

  • className (String)

    the name of the Parse collection for the trigger.

  • url (String)

    the new url endpoint for this webhook trigger.

Returns:

See Also:



136
137
138
139
140
# File 'lib/parse/api/hooks.rb', line 136

def update_trigger(triggerName, className, url)
  triggerName = _verify_trigger(triggerName)
  safe_class = Parse::API::PathSegment.trigger_class_name!(className, kind: "class name")
  request :put, "#{HOOKS_PREFIX}triggers/#{safe_class}/#{triggerName}", body: { url: url }
end