Sha256: d314d7325d4ecc291e0edf3a4bfc66e158dd50849e7d99ce9c74835a7ccb566b

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Slappy
  class Messanger
    class MissingChannelException < StandardError; end

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

    def initialize(options)
      @destination = {}
      @destination = options[:channel]
      options.delete :channel
      @options = options
    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
      Slack.chat_postMessage options
    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.5.2 lib/slappy/messanger.rb
slappy-0.5.1 lib/slappy/messanger.rb
slappy-0.5.0 lib/slappy/messanger.rb