Class: Slappy::Messenger

Inherits:
Object
  • Object
show all
Defined in:
lib/slappy/messenger.rb

Defined Under Namespace

Classes: MissingChannelException

Constant Summary collapse

CHANNEL_APIS =
[SlackAPI::Channel, SlackAPI::Group, SlackAPI::Direct]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Messenger

Returns a new instance of Messenger.



7
8
9
10
11
12
13
# File 'lib/slappy/messenger.rb', line 7

def initialize(options = {})
  opt = options.dup
  @destination = {}
  @destination = opt[:channel]
  opt.delete :channel
  @options = opt
end

Instance Method Details

#messageObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/slappy/messenger.rb', line 15

def message
  options = merge_params(@options)

  if @destination.is_a? SlackAPI::Base
    id = @destination.id
  else
    instance = nil
    CHANNEL_APIS.each do |klass|
      instance = klass.find(name: @destination) || klass.find(id: @destination)
      break unless instance.nil?
    end
    fail MissingChannelException.new, "channel / #{@destination} is not found" if instance.nil?
    id = instance.id
  end

  options[:channel] = id
  response = Slack.chat_postMessage options
  fail SlackAPI::SlackError.new, response['error'] unless response['ok']
end