Class: Slacks::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/slacks/channel.rb

Direct Known Subclasses

GuestChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slack, attributes = {}) ⇒ Channel

Returns a new instance of Channel.



5
6
7
8
9
10
11
12
# File 'lib/slacks/channel.rb', line 5

def initialize(slack, attributes={})
  @slack = slack
  @id = attributes["id"]
  @name = attributes["name"]
  @type = :channel
  @type = :group if attributes["is_group"]
  @type = :direct_message if attributes["is_im"]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/slacks/channel.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/slacks/channel.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/slacks/channel.rb', line 3

def type
  @type
end

Instance Method Details

#direct_message?Boolean Also known as: dm?, im?

Returns:

  • (Boolean)


54
55
56
# File 'lib/slacks/channel.rb', line 54

def direct_message?
  type == :direct_message
end

#guest?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/slacks/channel.rb', line 66

def guest?
  false
end

#inspectObject



70
71
72
# File 'lib/slacks/channel.rb', line 70

def inspect
  "<Slacks::Channel id=\"#{id}\" name=\"#{name}\">"
end

#private_group?Boolean Also known as: group?, private?

Returns:

  • (Boolean)


60
61
62
# File 'lib/slacks/channel.rb', line 60

def private_group?
  type == :group
end

#random_reply(replies) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/slacks/channel.rb', line 30

def random_reply(replies)
  if replies.is_a?(Hash)
    weights = replies.values
    unless weights.reduce(&:+) == 1.0
      raise ArgumentError, "Reply weights don't add up to 1.0"
    end

    draw = rand
    sum = 0
    pick = nil
    replies.each do |reply, weight|
      pick = reply unless sum > draw
      sum += weight
    end
    reply pick
  else
    reply replies.sample
  end
end

#reply(*messages) ⇒ Object Also known as: say



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slacks/channel.rb', line 14

def reply(*messages)
  messages.flatten!
  return unless messages.any?

  first_message = messages.shift
  message_options = {}
  message_options = messages.shift if messages.length == 1 && messages[0].is_a?(Hash)
  slack.send_message(first_message, message_options.merge(channel: id))

  messages.each do |message|
    sleep message.length / slack.typing_speed
    slack.send_message(message, channel: id)
  end
end

#to_sObject



74
75
76
77
78
# File 'lib/slacks/channel.rb', line 74

def to_s
  return name if private?
  return "@#{name}" if direct_message?
  "##{name}"
end

#typingObject



50
51
52
# File 'lib/slacks/channel.rb', line 50

def typing
  slack.typing_on(self)
end