Class: Webhooker::TriggerJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/webhooker/trigger_job.rb

Instance Method Summary collapse

Instance Method Details

#create_signature(body) ⇒ Object



23
24
25
# File 'app/jobs/webhooker/trigger_job.rb', line 23

def create_signature body
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @subscriber.secret, body)
end

#payloadObject



27
28
29
30
31
32
33
34
# File 'app/jobs/webhooker/trigger_job.rb', line 27

def payload
  @payload ||=
    if Webhooker.config.payload_key.present?
      { Webhooker.config.payload_key => @data }
    else
      @data
    end
end

#perform(subscriber, data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/jobs/webhooker/trigger_job.rb', line 5

def perform subscriber, data
  @subscriber = subscriber
  @data = data
  uri = URI.parse subscriber.url
  req = Net::HTTP::Post.new(uri.path)
  req.set_form_data payload
  Net::HTTP.new(uri.host, uri.port).start do |http|
    app = Rails.application.class.parent.to_s
    body = payload.to_json
    header = {
      'Content-Type' => 'application/json',
      'User-Agent' => app,
      "X-#{app}-Signature" => create_signature(body)
    }
    http.post uri.path, body, header
  end
end