Class: Slackly

Inherits:
Object
  • Object
show all
Defined in:
lib/slackly.rb,
lib/slackly/version.rb

Defined Under Namespace

Classes: InvalidMessageTextError

Constant Summary collapse

DEFAULT_OPTIONS =
{
  username: 'slackly',
  icon_emoji: ':sunglasses:',
  unfurl_links: true
}.freeze
VERSION =
'0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url, client_options = {}) ⇒ Slackly

Returns a new instance of Slackly.



16
17
18
19
# File 'lib/slackly.rb', line 16

def initialize(webhook_url, client_options = {})
  @webhook_uri = URI.parse(webhook_url)
  @client_options = DEFAULT_OPTIONS.merge(client_options)
end

Instance Attribute Details

#client_optionsObject

Returns the value of attribute client_options.



8
9
10
# File 'lib/slackly.rb', line 8

def client_options
  @client_options
end

#webhook_uriObject

Returns the value of attribute webhook_uri.



7
8
9
# File 'lib/slackly.rb', line 7

def webhook_uri
  @webhook_uri
end

Instance Method Details

#message(message_options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/slackly.rb', line 31

def message(message_options = {})
  message!(message_options)
rescue => exception
  warn "Error while sending message to Slack: #{exception.message}"
end

#message!(message_options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/slackly.rb', line 21

def message!(message_options = {})
  fail Slackly::InvalidMessageTextError, 'Please supply a valid message text' if message_options[:text].nil?

  request = Net::HTTP::Post.new(webhook_uri.request_uri)
  request.body = "payload=#{client_options.merge(message_options).to_json}"
  Net::HTTP.start(webhook_uri.host, webhook_uri.port, use_ssl: webhook_uses_ssl?) do |http|
    http.request(request)
  end
end