Class: WebhookService::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/slackhook/webhook_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Webhook

Returns a new instance of Webhook.



8
9
10
11
12
13
14
# File 'lib/slackhook/webhook_service.rb', line 8

def initialize options = {}
  @text        = options.fetch(:text, nil)
  @icon_type   = options.fetch(:icon_type, nil)
  @channel     = options.fetch(:channel, nil)
  @username    = options.fetch(:username, nil)
  @webhook_url = options.fetch(:webhook_url, nil)
end

Instance Method Details

#sendObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slackhook/webhook_service.rb', line 16

def send
  uri           = URI::encode(@webhook_url)
  @toSend       = { channel: @channel, text: @text, username: @username, icon_emoji: @icon_type}
  uri           = URI.parse(uri)
  https         = Net::HTTP.new(uri.host,uri.port)
  https.use_ssl = true
  req           = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
  req.body      = JSON.dump @toSend
  res           = https.request(req)
  res.code
end