Module: Parse::Webhooks::Registration

Included in:
Parse::Webhooks
Defined in:
lib/parse/webhooks/registration.rb

Overview

Module to support registering Parse CloudCode webhooks.

Constant Summary collapse

ALLOWED_HOOKS =

The set of allowed trigger types.

Parse::API::Hooks::TRIGGER_NAMES + [:function]

Instance Method Summary collapse

Instance Method Details

#register_functions!(endpoint) ⇒ Object

Registers all webhook functions registered with Parse::Stack with Parse server.

Parameters:

  • endpoint (String)

    a https url that points to the webhook server.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/parse/webhooks/registration.rb', line 89

def register_functions!(endpoint)
  unless endpoint.present? && (endpoint.starts_with?("http://") || endpoint.starts_with?("https://"))
    raise ArgumentError, "The HOOKS_URL must be http/s: '#{endpoint}''"
  end
  assert_webhook_url_safe!(endpoint)
  endpoint += "/" unless endpoint.ends_with?("/")
  functionsMap = {}
  client.functions.results.each do |f|
    next unless f["url"].present?
    functionsMap[f["functionName"]] = f["url"]
  end

  routes.function.keys.sort.each do |functionName|
    url = endpoint + functionName
    if functionsMap[functionName].present? #you may need to update
      next if functionsMap[functionName] == url
      client.update_function(functionName, url)
    else
      client.create_function(functionName, url)
    end
    yield(functionName) if block_given?
  end
end

#register_triggers!(endpoint, include_wildcard: false) ⇒ Object

Registers all webhook triggers registered with Parse::Stack with Parse server.

Parameters:

  • endpoint (String)

    a https url that points to the webhook server.

  • include_wildcard (Boolean) (defaults to: false)

    Allow wildcard registrations



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/parse/webhooks/registration.rb', line 116

def register_triggers!(endpoint, include_wildcard: false)
  unless endpoint.present? && (endpoint.starts_with?("http://") || endpoint.starts_with?("https://"))
    raise ArgumentError, "The HOOKS_URL must be http/s: '#{endpoint}''"
  end
  assert_webhook_url_safe!(endpoint)
  endpoint += "/" unless endpoint.ends_with?("/")
  all_triggers = Parse::API::Hooks::TRIGGER_NAMES_LOCAL

  current_triggers = {}
  all_triggers.each { |t| current_triggers[t] = {} }

  client.triggers.each do |t|
    next unless t["url"].present?
    trigger_name = t["triggerName"].underscore.to_sym
    current_triggers[trigger_name] ||= {}
    current_triggers[trigger_name][t["className"]] = t["url"]
  end

  all_triggers.each do |trigger|
    classNames = routes[trigger].keys.dup
    if include_wildcard && classNames.include?("*") #then create the list for all classes
      classNames.delete "*" #delete the wildcard before we expand it
      classNames = classNames + Parse.registered_classes
      classNames.uniq!
    end

    classNames.sort.each do |className|
      next if className == "*"
      url = endpoint + "#{trigger}/#{className}"
      if current_triggers[trigger][className].present? #then you may need to update
        next if current_triggers[trigger][className] == url
        client.update_trigger(trigger, className, url)
      else
        client.create_trigger(trigger, className, url)
      end
      yield(trigger.columnize, className) if block_given?
    end
  end
end

#register_webhook!(trigger, name, url) ⇒ Object

Registers a webhook trigger with a given endpoint url.

Parameters:

  • trigger (Symbol)

    Trigger type based on Parse::API::Hooks::TRIGGER_NAMES or :function.

  • name (String)

    the name of the webhook.

  • url (String)

    the https url endpoint that will handle the request.

Raises:

  • (ArgumentError)

See Also:



161
162
163
164
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/registration.rb', line 161

def register_webhook!(trigger, name, url)
  trigger = trigger.to_s.camelize(:lower).to_sym
  raise ArgumentError, "Invalid hook trigger #{trigger}" unless ALLOWED_HOOKS.include?(trigger)
  assert_webhook_url_safe!(url)
  if trigger == :function
    response = client.fetch_function(name)
    # if it is either an error (which has no results) or there is a result but
    # no registered item with a URL (which implies either none registered or only cloud code registered)
    # then create it.
    if response.results.none? { |d| d.has_key?("url") }
      response = client.create_function(name, url)
    else
      # update it
      response = client.update_function(name, url)
    end
    warn "Webhook Registration warning: #{response.result["warning"]}" if response.result.has_key?("warning")
    warn "Failed to register Cloud function #{name} with #{url}" if response.error?
    return response
  else # must be trigger
    response = client.fetch_trigger(trigger, name)
    # if it is either an error (which has no results) or there is a result but
    # no registered item with a URL (which implies either none registered or only cloud code registered)
    # then create it.
    if response.results.none? { |d| d.has_key?("url") }
      # create it
      response = client.create_trigger(trigger, name, url)
    else
      # update it
      response = client.update_trigger(trigger, name, url)
    end

    warn "Webhook Registration warning: #{response.result["warning"]}" if response.result.has_key?("warning")
    warn "Webhook Registration error: #{response.error}" if response.error?
    return response
  end
end

#remove_all_functions!Object

removes all registered webhook functions with Parse Server.



68
69
70
71
72
73
74
# File 'lib/parse/webhooks/registration.rb', line 68

def remove_all_functions!
  client.functions.results.sort_by { |f| f["functionName"] }.each do |f|
    next unless f["url"].present?
    client.delete_function f["functionName"]
    yield(f["functionName"]) if block_given?
  end
end

#remove_all_triggers!Object

removes all registered webhook triggers with Parse Server.



77
78
79
80
81
82
83
84
85
# File 'lib/parse/webhooks/registration.rb', line 77

def remove_all_triggers!
  client.triggers.results.sort_by { |f| [f["triggerName"], f["className"]] }.each do |f|
    next unless f["url"].present?
    triggerName = f["triggerName"]
    className = f[Parse::Model::KEY_CLASS_NAME]
    client.delete_trigger triggerName, className
    yield(f["triggerName"], f[Parse::Model::KEY_CLASS_NAME]) if block_given?
  end
end