Sha256: ec0358b07113e06324ba2553cec4c11acc169307cf5b284ef1c7050266c44999

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module Slacky
  class Message
    attr_reader :raw, :text, :user, :channel

    @@decorator = @@bot = nil

    def self.decorator=(decorator)
      @@decorator = decorator
    end

    def self.bot=(bot)
      @@bot = bot
    end

    def initialize(user, channel, raw)
      @raw = raw
      @user = user
      @channel = channel
      @text = raw.text.strip
      @pieces = @text.split ' '
      self.extend @@decorator if @@decorator
    end

    def reply(msg)
      @@bot.client.message channel: @channel.slack_id, reply_to: @raw.id, text: msg
    end

    def command?
      first = word 0
      return false unless first
      first == @@bot.name || first == "<@#{@@bot.slack_id}>"
    end

    def command
      if @channel.type == :im
        command? ? downword(1) : downword(0)
      else
        command? ? downword(1) : nil
      end
    end

    def yes?
      [ 'y', 'yes' ].include? @text
    end

    def no?
      [ 'n', 'no' ].include? @text
    end

    private

    def word(n)
      return nil unless @pieces.length > n
      @pieces[n]
    end

    def downword(n)
      word(n) && word(n).downcase
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
slacky-0.1.3 lib/slacky/message.rb
slacky-0.1.2 lib/slacky/message.rb
slacky-0.1.1 lib/slacky/message.rb
slacky-0.1 lib/slacky/message.rb