Class: WebhookSystem::Job
- Inherits:
-
ActiveJob::Base
- Object
- ActiveJob::Base
- WebhookSystem::Job
show all
- Defined in:
- lib/webhook_system/job.rb
Overview
This is the ActiveJob in charge of actually sending each event
Defined Under Namespace
Classes: ErrorResponse, RequestFailed
Class Method Summary
collapse
-
.build_client ⇒ Object
-
.build_request(client, subscription, event) ⇒ Object
-
.call_inline(job_name, subscription, event) ⇒ Object
-
.ensure_success(response, http_method, subscription) ⇒ Object
-
.format_for_subscription(subscription) ⇒ Object
-
.log_response(subscription, event, request, response) ⇒ Object
-
.post(subscription, event) ⇒ Object
Instance Method Summary
collapse
Class Method Details
.build_client ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/webhook_system/job.rb', line 115
def self.build_client
Faraday.new do |faraday|
faraday.response :logger if ENV['WEBHOOK_DEBUG']
faraday.response :encoding
faraday.adapter Faraday.default_adapter
end
end
|
.build_request(client, subscription, event) ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/webhook_system/job.rb', line 86
def self.build_request(client, subscription, event)
payload, = Encoder.encode(subscription.secret, event, format: format_for_subscription(subscription))
client.build_request(:post) do |req|
req.url subscription.url
req..merge!()
req.body = payload.to_s
end
end
|
.call_inline(job_name, subscription, event) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/webhook_system/job.rb', line 47
def self.call_inline(job_name, subscription, event)
job_class = const_get("WebhookSystem::Inline#{job_name}Job")
job_class.perform_now(subscription, event)
end
|
.ensure_success(response, http_method, subscription) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/webhook_system/job.rb', line 70
def self.ensure_success(response, http_method, subscription)
url = subscription.url
status = response.status
return if (200..299).cover? status
if subscription.respond_to?(:account_id)
account_info = subscription.account_info
inner = "failed for account #{account_info} with"
else
inner = "failed with"
end
text = "#{http_method} request to #{url} #{inner} code: #{status} and error #{response.body}"
raise RequestFailed.new(text, status, response.body)
end
|
95
96
97
|
# File 'lib/webhook_system/job.rb', line 95
def self.format_for_subscription(subscription)
subscription.encrypted ? 'base64+aes256' : 'json'
end
|
.log_response(subscription, event, request, response) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/webhook_system/job.rb', line 99
def self.log_response(subscription, event, request, response)
event_log = EventLog.construct(subscription, event, request, response)
if ActiveRecord::Base.connection.open_transactions.zero?
event_log.save!
else
Thread.new { event_log.save! }.join
end
end
|
.post(subscription, event) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/webhook_system/job.rb', line 55
def self.post(subscription, event)
client = build_client
request = build_request(client, subscription, event)
response =
begin
client.builder.build_response(client, request)
rescue RuntimeError => e
ErrorResponse.new(e)
end
log_response(subscription, event, request, response)
ensure_success(response, :POST, subscription)
end
|
Instance Method Details
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/webhook_system/job.rb', line 36
def perform(subscription, event)
if subscription.url.match?(/^https?:/)
self.class.post(subscription, event)
elsif (match_data = subscription.url.match(/^inline:(.*)/)).present?
self.class.call_inline(match_data[1], subscription, event)
else
raise RuntimeError, "unknown prefix url for subscription"
ensure_success(ErrorResponse.new(exception), :INVALID, subscription)
end
end
|