Class: PagerTree::Integrations::OutgoingWebhookDelivery::HookRelay

Inherits:
PagerTree::Integrations::OutgoingWebhookDelivery show all
Extended by:
Env
Defined in:
app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb

Constant Summary

Constants inherited from PagerTree::Integrations::OutgoingWebhookDelivery

HTTP_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PagerTree::Integrations::OutgoingWebhookDelivery

factory

Class Method Details

.hook_relay_account_idObject



7
8
9
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 7

def self.
  find_value_by_name(:hook_relay, :account_id)
end

.hook_relay_api_keyObject



15
16
17
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 15

def self.hook_relay_api_key
  find_value_by_name(:hook_relay, :api_key)
end

.hook_relay_hook_idObject



11
12
13
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 11

def self.hook_relay_hook_id
  find_value_by_name(:hook_relay, :hook_id)
end

Instance Method Details

#deliverObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 31

def deliver
  run_callbacks :deliver do
    begin
      hook_relay_options = {
        headers: {
          HR_TARGET_URL: url
        }
      }

      pagertree_options = {
        headers: {
          Accept: "*/*",
          "User-Agent": "pagertree outgoing webhook service; ref: #{resource&.id}; report: [email protected]",
          "Content-Type": "application/json"
        }
      }

      auth_options = {}
      if auth.is_a?(Hash) && (username = auth.dig(:username)).present? && (password = auth.dig(:password)).present?
        auth_options = {
          headers: {
            Authorization: Base64.strict_encode64("#{username}:#{password}")
          }
        }
      end

      options = OutgoingWebhookDelivery::HTTP_OPTIONS
        .deep_merge(hook_relay_options)
        .deep_merge(pagertree_options)
        .deep_merge(auth_options)

      response = HTTParty.post(hook_relay_hook_url, body: body.to_json, **options)

      self.thirdparty_id = response["id"]
      self.status = :sent
    rescue => exception
      Rails.logger.error(exception)
      self.status = :failure
    end
    save!
  end # run_callbacks
end

#deliver_laterObject



27
28
29
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 27

def deliver_later
  OutgoingWebhookJob.perform_later(id, :deliver)
end

#deliveryObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 74

def delivery
  return @delivery if @delivery
  return {} unless thirdparty_id

  options = {
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer #{OutgoingWebhookDelivery::HookRelay.hook_relay_api_key}"
    },
    timeout: 15
  }

  response = ::HTTParty.get(hook_relay_delivery_url, **options)
  @delivery = response.parsed_response if response.success?

  @delivery || {}
end

#hook_relay_delivery_urlObject



23
24
25
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 23

def hook_relay_delivery_url
  "https://app.hookrelay.dev/api/v1/accounts/#{OutgoingWebhookDelivery::HookRelay.}/hooks/#{OutgoingWebhookDelivery::HookRelay.hook_relay_hook_id}/deliveries/#{thirdparty_id}"
end

#hook_relay_hook_urlObject



19
20
21
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 19

def hook_relay_hook_url
  "https://api.hookrelay.dev/hooks/#{OutgoingWebhookDelivery::HookRelay.}/#{OutgoingWebhookDelivery::HookRelay.hook_relay_hook_id}"
end

#requestObject



92
93
94
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 92

def request
  delivery&.dig("request") || {}
end

#responsesObject



96
97
98
# File 'app/models/pager_tree/integrations/outgoing_webhook_delivery/hook_relay.rb', line 96

def responses
  delivery&.dig("responses") || []
end