Class: Slackit
- Inherits:
-
Object
- Object
- Slackit
- Defined in:
- lib/slackit.rb,
lib/slackit/version.rb
Overview
To follow
Constant Summary collapse
- VERSION =
'1.1.12'
Instance Method Summary collapse
-
#convert_to_json(json_string) ⇒ Object
Convery the string to json.
-
#initialize(options = {}) ⇒ Slackit
constructor
A new instance of Slackit.
-
#send(text) ⇒ Object
Add an alias for backwards compatibility.
-
#send_attachment(attachment) ⇒ Object
Legacy attachments.
-
#send_block(block) ⇒ Object
New shiney blocks.
-
#send_message(text) ⇒ Object
Raw message.
- #send_payload(payload) ⇒ Object
- #valid_webhook(url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Slackit
Returns a new instance of Slackit.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/slackit.rb', line 13 def initialize( = {}) @webhook_url = [:webhook_url] @username = [:username] @channel = [:channel] @icon_emoji = [:icon_emoji] @validation_test = [:validation_test] @validated_webhook = [:validated_webhook] raise ArgumentError.new('Webhook URL required') if @webhook_url.nil? if @validation_test if valid_webhook(@webhook_url) @channel = 'general' ('This is a validation message') "#{@webhook_url} is a valid url".success else "#{@webhook_url} is NOT a valid url".error end return end raise ArgumentError.new("Invalid webhook URL: #{@webhook_url} - should start with https://hooks.slack.com/services/") unless @webhook_url.start_with?('https://hooks.slack.com/services/') return if valid_webhook(@webhook_url) raise ArgumentError.new("Invalid webhook URL: #{@webhook_url} - please check your configuration") unless valid_webhook(@webhook_url) end |
Instance Method Details
#convert_to_json(json_string) ⇒ Object
Convery the string to json
89 90 91 92 93 94 95 |
# File 'lib/slackit.rb', line 89 def convert_to_json(json_string) begin return JSON.parse(json_string) rescue JSON::ParserError raise ArgumentError.new('Invalid json') end end |
#send(text) ⇒ Object
Add an alias for backwards compatibility
66 67 68 |
# File 'lib/slackit.rb', line 66 def send(text) return (text) end |
#send_attachment(attachment) ⇒ Object
Legacy attachments
73 74 75 76 |
# File 'lib/slackit.rb', line 73 def () payload = { 'attachments' => [ convert_to_json() ] } return send_payload(payload) end |
#send_block(block) ⇒ Object
New shiney blocks
81 82 83 84 |
# File 'lib/slackit.rb', line 81 def send_block(block) payload = convert_to_json(block) return send_payload(payload) end |
#send_message(text) ⇒ Object
Raw message
55 56 57 58 59 60 61 |
# File 'lib/slackit.rb', line 55 def (text) text = text.gsub('\\n', "\n") # ensure newlines are not escaped payload = { 'text' => text } return send_payload(payload) end |
#send_payload(payload) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/slackit.rb', line 97 def send_payload(payload) headers = { 'Content-Type' => 'application/json' } # Add the additional payload items payload['icon_emoji'] ||= @icon_emoji payload['username'] ||= @username payload['channel'] ||= @channel begin uri = URI.parse(@webhook_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri, headers) request.body = payload.to_json response = http.request(request) return true if response.code == '200' raise "Invalid webhook URL: #{@webhook_url} - please check your configuration" if response.code == '301' || response.code == '302' raise "Unknown error for webhook URL: #{@webhook_url}" if response.body.empty? raise response.body rescue Exception => e raise e end end |
#valid_webhook(url) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/slackit.rb', line 42 def valid_webhook(url) uri = URI.parse(url) r = Net::HTTP.get_response(uri) return false if (r.code == '301') || (r.code == '302') return true end |