Class: Slacks::Channel
- Inherits:
-
Object
- Object
- Slacks::Channel
- Defined in:
- lib/slacks/channel.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #direct_message? ⇒ Boolean (also: #dm?, #im?)
- #guest? ⇒ Boolean
-
#initialize(slack, attributes = {}) ⇒ Channel
constructor
A new instance of Channel.
- #inspect ⇒ Object
- #private_group? ⇒ Boolean (also: #group?, #private?)
- #random_reply(replies) ⇒ Object
- #reply(*messages) ⇒ Object (also: #say)
- #to_s ⇒ Object
- #typing ⇒ Object
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/slacks/channel.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/slacks/channel.rb', line 3 def name @name end |
#type ⇒ Object (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?
54 55 56 |
# File 'lib/slacks/channel.rb', line 54 def type == :direct_message end |
#guest? ⇒ Boolean
66 67 68 |
# File 'lib/slacks/channel.rb', line 66 def guest? false end |
#inspect ⇒ Object
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?
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(*) .flatten! return unless .any? = .shift = {} = .shift if .length == 1 && [0].is_a?(Hash) slack.(, .merge(channel: id)) .each do || sleep .length / slack.typing_speed slack.(, channel: id) end end |
#to_s ⇒ Object
74 75 76 77 78 |
# File 'lib/slacks/channel.rb', line 74 def to_s return name if private? return "@#{name}" if "##{name}" end |
#typing ⇒ Object
50 51 52 |
# File 'lib/slacks/channel.rb', line 50 def typing slack.typing_on(self) end |