Class: Envoy::Webhook

Inherits:
Transport show all
Defined in:
lib/envoy/transport.rb

Instance Attribute Summary collapse

Attributes inherited from Transport

#errors

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Webhook

Returns a new instance of Webhook.



62
63
64
65
# File 'lib/envoy/transport.rb', line 62

def initialize(options = {})
  self.url = options[:url]
  self.headers = options[:headers] || {}
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



60
61
62
# File 'lib/envoy/transport.rb', line 60

def headers
  @headers
end

#urlObject

Returns the value of attribute url.



60
61
62
# File 'lib/envoy/transport.rb', line 60

def url
  @url
end

Instance Method Details

#send_message(message) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/envoy/transport.rb', line 67

def send_message(message)
  url = URI.parse(@url)
  request = Net::HTTP::Post.new(url.path)
  request.body = message.options

  @headers.each do |k,v|
    request.add_field k,v
  end

  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}

  case response
    when Net::HTTPSuccess, Net::HTTPFound
      return true
    else
      return false
  end

  rescue URI::InvalidURIError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse,
    Net::HTTPHeaderSyntaxError, Net::ProtocolError => error
    self.errors << SendError.new(error, Time.now)
    return false
end