web_hook.rb 1.02 KB
Newer Older
1 2 3
class WebHook < ActiveRecord::Base
  include HTTParty

4 5
  attr_accessible :url

6 7 8
  # HTTParty timeout
  default_timeout 10

Nihad Abbasov's avatar
Nihad Abbasov committed
9
  validates :url, presence: true,
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
10
                  format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }
11 12

  def execute(data)
13 14 15 16
    parsed_url = URI.parse(url)
    if parsed_url.userinfo.blank?
      WebHook.post(url, body: data.to_json, headers: { "Content-Type" => "application/json" })
    else
Nihad Abbasov's avatar
Nihad Abbasov committed
17
      post_url = url.gsub("#{parsed_url.userinfo}@", "")
18 19
      WebHook.post(post_url,
                   body: data.to_json,
Nihad Abbasov's avatar
Nihad Abbasov committed
20
                   headers: {"Content-Type" => "application/json"},
21 22
                   basic_auth: {username: parsed_url.user, password: parsed_url.password})
    end
23 24
  end
end
Nihad Abbasov's avatar
Nihad Abbasov committed
25

Valery Sizov's avatar
Valery Sizov committed
26 27 28 29
# == Schema Information
#
# Table name: web_hooks
#
Nihad Abbasov's avatar
Nihad Abbasov committed
30
#  id         :integer         not null, primary key
Valery Sizov's avatar
Valery Sizov committed
31
#  url        :string(255)
Nihad Abbasov's avatar
Nihad Abbasov committed
32
#  project_id :integer
randx's avatar
randx committed
33 34
#  created_at :datetime        not null
#  updated_at :datetime        not null
Nihad Abbasov's avatar
Nihad Abbasov committed
35
#  type       :string(255)     default("ProjectHook")
Valery Sizov's avatar
Valery Sizov committed
36
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37