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.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/parse/webhooks/registration.rb', line 43

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
  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)

    Allow wildcard registrations


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/parse/webhooks/registration.rb', line 71

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
  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:


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
# File 'lib/parse/webhooks/registration.rb', line 117

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)
  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.


19
20
21
22
23
24
25
26
# File 'lib/parse/webhooks/registration.rb', line 19

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.


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parse/webhooks/registration.rb', line 29

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