Class: WebhookService::Webhook
- Inherits:
-
Object
- Object
- WebhookService::Webhook
- Defined in:
- lib/slackhook/webhook_service.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Webhook
constructor
A new instance of Webhook.
- #send ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Webhook
Returns a new instance of Webhook.
8 9 10 11 12 13 14 15 16 |
# File 'lib/slackhook/webhook_service.rb', line 8 def initialize = {} raise ArgumentError.new('icon_type and icon_url are mutualy exclusive!') if [:icon_type].present? && [:icon_url].present? @text = .fetch(:text, nil) @icon_type = .fetch(:icon_type, nil) @icon_url = .fetch(:icon_url, nil) @channel = .fetch(:channel, nil) @username = .fetch(:username, nil) @webhook_url = .fetch(:webhook_url, nil) end |
Instance Method Details
#send ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/slackhook/webhook_service.rb', line 18 def send uri = URI::encode(@webhook_url) @toSend = { channel: @channel, text: @text, username: @username } if @icon_type.present? @toSend.merge!(icon_emoji: @icon_type) elsif @icon_url.present? @toSend.merge!(icon_url: @icon_url) end 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 |