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
- #register_functions!(endpoint) ⇒ Object
Registers all webhook functions registered with Parse::Stack with Parse server.
- #register_triggers!(endpoint, include_wildcard: false) ⇒ Object
Registers all webhook triggers registered with Parse::Stack with Parse server.
- #register_webhook!(trigger, name, url) ⇒ Object
Registers a webhook trigger with a given endpoint url.
- #remove_all_functions! ⇒ Object
removes all registered webhook functions with Parse Server.
- #remove_all_triggers! ⇒ Object
removes all registered webhook triggers with Parse Server.
Instance Method Details
#register_functions!(endpoint) ⇒ Object
Registers all webhook functions registered with Parse::Stack with Parse server.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # File 'lib/parse/webhooks/registration.rb', line 40 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.
66 67 68 69 70 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 | # File 'lib/parse/webhooks/registration.rb', line 66 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.
110 111 112 113 114 115 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 | # File 'lib/parse/webhooks/registration.rb', line 110 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 | # 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.
28 29 30 31 32 33 34 35 36 | # File 'lib/parse/webhooks/registration.rb', line 28 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 |