Class: Webhooks::Subscription

Inherits:
ApplicationRecord show all
Defined in:
app/models/webhooks/subscription.rb

Class Method Summary collapse

Methods inherited from ApplicationRecord

descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?

Class Method Details

.get_notification_urls(api_name:, consumer_id:, event:, api_guid: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/webhooks/subscription.rb', line 6

def self.get_notification_urls(api_name:, consumer_id:, event:, api_guid: nil)
  sql = "
    select json_agg(agg.urls)::jsonb as event_urls
    from (
    select distinct (event_json.sub_event_array -> 'urls') as urls
    from (
      select jsonb_array_elements(subs.api_consumer_subscriptions) as sub_event_array
      from (
        select a.events -> 'subscriptions' as api_consumer_subscriptions
        from webhooks_subscriptions a
        where a.api_name = $1
        and a.consumer_id = $2
        and a.events -> 'subscriptions' is not null
        and ( a.api_guid is null or a.api_guid = $4 )
      ) as subs
    ) as event_json
    where event_json.sub_event_array ->> 'event' = $3
    ) as agg
  "
  retrieve_event_urls(sql, api_name, consumer_id, event, api_guid)
end

.get_observers_by_guid(api_name:, consumer_id:, api_guid:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/webhooks/subscription.rb', line 28

def self.get_observers_by_guid(api_name:, consumer_id:, api_guid:)
  uuid_regex = /^[a-f0-9]{8}-[a-f0-9]{4}-[0-5][a-f0-9]{3}-[089ab][a-f0-9]{3}-[a-f0-9]{12}$/i
  return [] unless uuid_regex.match?(consumer_id) && uuid_regex.match?(api_guid)

  sql = "
    select a.events -> 'subscriptions' as api_consumer_subscriptions
    from webhooks_subscriptions a
    where a.api_name = $1
    and a.consumer_id = $2
    and a.events -> 'subscriptions' is not null
    and a.api_guid = $3
  "
  retrieve_observers_by_guid(sql, api_name, consumer_id, api_guid)
end

.retrieve_event_urls(sql, *args) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/models/webhooks/subscription.rb', line 43

def self.retrieve_event_urls(sql, *args)
  result = ActiveRecord::Base.connection_pool.with_connection do |c|
    c.raw_connection.exec_params(sql, args).to_a
  end

  event_urls = result.first['event_urls'] ||= '[]'
  JSON.parse(event_urls).flatten.uniq
end

.retrieve_observers_by_guid(sql, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/webhooks/subscription.rb', line 52

def self.retrieve_observers_by_guid(sql, *args)
  result = ActiveRecord::Base.connection_pool.with_connection do |c|
    c.raw_connection.exec_params(sql, args).to_a
  end

  if result.any?
    observers = result.first['api_consumer_subscriptions']
    JSON.parse(observers)
  else
    []
  end
end