BigW Consortium Gitlab

web_hook.rb 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# == Schema Information
#
# Table name: web_hooks
#
#  id         :integer          not null, primary key
#  url        :string(255)
#  project_id :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  type       :string(255)      default("ProjectHook")
Dmitriy Zaporozhets committed
11
#  service_id :integer
12 13
#

14 15 16
class WebHook < ActiveRecord::Base
  include HTTParty

17 18
  attr_accessible :url

19 20 21
  # HTTParty timeout
  default_timeout 10

22
  validates :url, presence: true,
Andrey Kumanyaev committed
23
                  format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
24 25

  def execute(data)
26 27 28 29
    parsed_url = URI.parse(url)
    if parsed_url.userinfo.blank?
      WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" })
    else
30
      post_url = url.gsub("#{parsed_url.userinfo}@", "")
31 32 33 34
      auth = {
        username: URI.decode(parsed_url.user),
        password: URI.decode(parsed_url.password),
      }
35 36
      WebHook.post(post_url,
                   body: data.to_json,
37
                   headers: {"Content-Type" => "application/json"},
38
                   basic_auth: auth)
39
    end
40
  end
41 42 43 44

  def async_execute(data)
    Sidekiq::Client.enqueue(ProjectWebHookWorker, id, data)
  end
45
end