Module: WolfCore::Integrations::WebhooksOperations

Defined in:
lib/wolf_core/application/integrations/webhooks_operations.rb

Instance Method Summary collapse

Instance Method Details

#build_body_from_params(params:, id_key:) ⇒ Object



48
49
50
51
52
53
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 48

def build_body_from_params(params:, id_key:)
  {
    event_name: get_event_name(params: params),
    id_key => get_object_id(params: params)
  }
end

#get_event_name(params:) ⇒ Object



14
15
16
17
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 14

def get_event_name(params:)
  message = JSON.parse(params['Message'] || {})
  message['event_name']
end

#get_event_type(params:) ⇒ Object



10
11
12
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 10

def get_event_type(params:)
  params['Type']
end

#get_object_id(params:) ⇒ Object



55
56
57
58
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 55

def get_object_id(params:)
  params = JSON.parse(params['Message'] || '{}')
  params['object_id']
end

#get_payload(event_type:, params:) ⇒ Object



4
5
6
7
8
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 4

def get_payload(event_type:, params:)
  return if event_type == 'SubscriptionConfirmation'
  params = JSON.parse(params['Message'] || '{}')
  JSON.parse(params['payload'] || '{}')
end

#process_webhook(params:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 37

def process_webhook(params:)
  event_type = get_event_type(params: params)
  event_name = get_event_name(params: params)
  # payload = get_payload(event_type: event_type, params: params)
  if event_type == 'SubscriptionConfirmation'
    url = params['SubscribeURL']
  elsif event_type == 'Notification'
    yield(event_name)
  end
end

#subscription_confirmation_request(url:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 29

def subscription_confirmation_request(url:)
  response = safe_http_get(
    url: url,
    error_message: "Can not confirm subscription",
  )
  response
end

#validate_user(params:) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/wolf_core/application/integrations/webhooks_operations.rb', line 19

def validate_user(params:)
  event_type = get_event_type(params: params)
  return if event_type == 'SubscriptionConfirmation'

  user_id = params.dig('invoker', 'user_id').to_s
  return unless user_id == ENV['WOLF_ADMIN_ID'].to_s

  raise_service_error("Ignoring event from user id #{user_id}")
end