Module: Webhooks::Utilities
Defined Under Namespace
Modules: ClassMethods
Constant Summary
collapse
- SUBSCRIPTION_EX =
JSON.parse(File.read('./modules/vba_documents/spec/fixtures/subscriptions/subscriptions.json'))
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
fetch_events, record_notifications, register_events, register_webhook
Class Attribute Details
.api_name_to_retries ⇒ Object
Returns the value of attribute api_name_to_retries.
15
16
17
|
# File 'lib/webhooks/utilities.rb', line 15
def api_name_to_retries
@api_name_to_retries
end
|
.api_name_to_time_block ⇒ Object
Returns the value of attribute api_name_to_time_block.
15
16
17
|
# File 'lib/webhooks/utilities.rb', line 15
def api_name_to_time_block
@api_name_to_time_block
end
|
.event_to_api_name ⇒ Object
Returns the value of attribute event_to_api_name.
15
16
17
|
# File 'lib/webhooks/utilities.rb', line 15
def event_to_api_name
@event_to_api_name
end
|
.supported_events ⇒ Object
Returns the value of attribute supported_events.
15
16
17
|
# File 'lib/webhooks/utilities.rb', line 15
def supported_events
@supported_events
end
|
Class Method Details
.api_registered?(api_name) ⇒ Boolean
26
27
28
29
30
|
# File 'lib/webhooks/utilities.rb', line 26
def api_registered?(api_name)
@event_to_api_name.values.include?(api_name)
rescue
false
end
|
.included(base) ⇒ Object
17
18
19
|
# File 'lib/webhooks/utilities.rb', line 17
def included(base)
base.extend ClassMethods
end
|
.register_event(event) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/webhooks/utilities.rb', line 42
def register_event(event)
@supported_events ||= []
if @supported_events.include?(event)
raise ArgumentError, "Event: #{event} previously registered! api_name: #{event_to_api_name[event]}"
end
@supported_events << event
@supported_events.uniq!
end
|
.register_name_to_event(name, event) ⇒ Object
32
33
34
35
|
# File 'lib/webhooks/utilities.rb', line 32
def register_name_to_event(name, event)
@event_to_api_name ||= {}
@event_to_api_name[event] = name
end
|
.register_name_to_retries(name, retries) ⇒ Object
21
22
23
24
|
# File 'lib/webhooks/utilities.rb', line 21
def register_name_to_retries(name, retries)
@api_name_to_retries ||= {}
@api_name_to_retries[name] = retries.to_i
end
|
.register_name_to_time_block(name, block) ⇒ Object
37
38
39
40
|
# File 'lib/webhooks/utilities.rb', line 37
def register_name_to_time_block(name, block)
@api_name_to_time_block ||= {}
@api_name_to_time_block[name] = block
end
|
Instance Method Details
#validate_events(subscriptions) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/webhooks/utilities.rb', line 97
def validate_events(subscriptions)
events = subscriptions.select { |s| s.key?('event') }.map { |s| s['event'] }
raise SchemaValidationErrors, ["Duplicate Event(s) submitted! #{events}"] if Set.new(events).size != events.length
unsupported_events = events - Webhooks::Utilities.supported_events
if unsupported_events.length.positive?
raise SchemaValidationErrors, ["Invalid Event(s) submitted! #{unsupported_events}"]
end
true
end
|
#validate_subscription(subscriptions) ⇒ Object
Validates a subscription request for an upload submission. Returns an object representing the subscription
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/webhooks/utilities.rb', line 81
def validate_subscription(subscriptions)
schema_path = Pathname.new('modules/vba_documents/spec/fixtures/subscriptions/webhook_subscriptions_schema.json')
schemer_formats = {
'valid_urls' => ->(urls, _schema_info) { validate_urls(urls) },
'valid_events' => ->(subscription, _schema_info) { validate_events(subscription) }
}
schemer = JSONSchemer.schema(schema_path, formats: schemer_formats)
unless schemer.valid?(subscriptions)
raise SchemaValidationErrors, ["Invalid subscription! Body must match the included example\n#{SUBSCRIPTION_EX}"]
end
subscriptions
end
|
#validate_url(url) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/webhooks/utilities.rb', line 110
def validate_url(url)
begin
uri = URI(url)
rescue URI::InvalidURIError
raise SchemaValidationErrors, ["Invalid subscription! URI does not parse: #{url}"]
end
https = uri.scheme.eql? 'https'
if !https && Settings.webhooks.require_https
raise SchemaValidationErrors, ["Invalid subscription! URL #{url} must be https!"]
end
true
end
|
#validate_urls(urls) ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/webhooks/utilities.rb', line 124
def validate_urls(urls)
valid = true
urls.each do |url|
valid &= validate_url(url)
end
valid
end
|