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
Instance Method Summary
collapse
Class Method Details
.build_client ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/webhook_system/job.rb', line 90
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
61
62
63
64
65
66
67
68
|
# File 'lib/webhook_system/job.rb', line 61
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
|
.ensure_success(response, http_method, url) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/webhook_system/job.rb', line 53
def self.ensure_success(response, http_method, url)
status = response.status
unless (200..299).cover? status
text = "#{http_method} request to #{url} failed with code: #{status} and error #{response.body}"
raise RequestFailed.new(text, status, response.body)
end
end
|
70
71
72
|
# File 'lib/webhook_system/job.rb', line 70
def self.format_for_subscription(subscription)
subscription.encrypt ? 'base64+aes256' : 'json'
end
|
.log_response(subscription, event, request, response) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/webhook_system/job.rb', line 74
def self.log_response(subscription, event, request, response)
event_log = EventLog.construct(subscription, event, request, response)
if ActiveRecord::Base.connection.open_transactions == 0
event_log.save!
else
Thread.new { event_log.save! }.join
end
end
|
.post(subscription, event) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/webhook_system/job.rb', line 38
def self.post(subscription, event)
client = build_client
request = build_request(client, subscription, event)
response =
begin
client.builder.build_response(client, request)
rescue RuntimeError => exception
ErrorResponse.new(exception)
end
log_response(subscription, event, request, response)
ensure_success(response, :POST, subscription.url)
end
|
Instance Method Details
34
35
36
|
# File 'lib/webhook_system/job.rb', line 34
def perform(subscription, event)
self.class.post(subscription, event)
end
|