Sha256: 99fd84caa38b5a36748776ae984236c507281f995ac9c0b5a625cb485712f663

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Slappy
  class Messenger
    class MissingChannelException < StandardError; end

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

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

    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

    private

    def config
      Slappy.configuration
    end

    def merge_params(options)
      default = config.send_params
      default.merge options
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slappy-0.6.3 lib/slappy/messenger.rb
slappy-0.6.2 lib/slappy/messenger.rb
slappy-0.6.1 lib/slappy/messenger.rb