Class: Waylon::Slack::Channel
- Inherits:
-
Object
- Object
- Waylon::Slack::Channel
- Defined in:
- lib/waylon/slack/channel.rb
Overview
A representation of Slack channels for Waylon
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.from_name(name) ⇒ Channel
Allows finding a channel based on its channel name.
-
.sense ⇒ Class
Provides direct access to the Sense class.
Instance Method Summary collapse
-
#archived? ⇒ Boolean
Is channel archived (meaning no further messages are possible)?.
-
#data ⇒ Hash
Provides lazy, cached access to the Channel’s internal details.
-
#general? ⇒ Boolean
Is this the “main” channel for this team?.
-
#initialize(id = nil, data: {}) ⇒ Channel
constructor
A new instance of Channel.
-
#member? ⇒ Boolean
Is this bot a member of the channel?.
-
#members ⇒ Array<User>
Lists channel members.
-
#name ⇒ String
The proper channel name.
-
#post(text: nil, attachments: nil, blocks: nil, thread: nil) ⇒ void
Posts a message to a channel.
-
#private? ⇒ Boolean
Is this a private channel? (meaning a direct message, NOT private in the Slack sense).
-
#sense ⇒ Class
An instance-level helper to access the class-level method.
-
#topic ⇒ String
Provides access to the Channel’s topic.
Constructor Details
#initialize(id = nil, data: {}) ⇒ Channel
Returns a new instance of Channel.
24 25 26 27 28 29 30 |
# File 'lib/waylon/slack/channel.rb', line 24 def initialize(id = nil, data: {}) raise "Must provide ID or details" unless id || !data.empty? @id = id || data["id"] # @data should never be accessed directly... always use the wrapper instance method @data = data end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/waylon/slack/channel.rb', line 7 def id @id end |
Class Method Details
.from_name(name) ⇒ Channel
Allows finding a channel based on its channel name
12 13 14 15 16 |
# File 'lib/waylon/slack/channel.rb', line 12 def self.from_name(name) name = "##{name}" unless name.start_with?("#") raw = sense.client.conversations_info(channel: name) new(raw["channel"]["id"], data: raw["channel"]) end |
Instance Method Details
#archived? ⇒ Boolean
Is channel archived (meaning no further messages are possible)?
34 35 36 |
# File 'lib/waylon/slack/channel.rb', line 34 def archived? data["is_archived"].dup end |
#data ⇒ Hash
Provides lazy, cached access to the Channel’s internal details
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/waylon/slack/channel.rb', line 40 def data if !@data || @data.empty? # Only cache channel info for 5 min sense.cache("channels.#{id}", expires: 300) do raw_data = sense.client.conversations_info(channel: id) @data = raw_data["channel"] end else @data end end |
#general? ⇒ Boolean
Is this the “main” channel for this team?
54 55 56 |
# File 'lib/waylon/slack/channel.rb', line 54 def general? data["is_general"].dup end |
#member? ⇒ Boolean
Is this bot a member of the channel?
74 75 76 |
# File 'lib/waylon/slack/channel.rb', line 74 def member? data["is_member"] end |
#members ⇒ Array<User>
Lists channel members
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/waylon/slack/channel.rb', line 60 def members # Only cache channel member ids for 5 min ids = sense.cache("channels.#{id}.member_ids", expires: 300) do member_ids = [] sense.client.conversations_members(channel: id) do |raw| member_ids += raw["members"] end member_ids.sort.uniq end ids.map { |m| User.new(m) } end |
#name ⇒ String
The proper channel name
80 81 82 |
# File 'lib/waylon/slack/channel.rb', line 80 def name "##{data["name"]}" end |
#post(text: nil, attachments: nil, blocks: nil, thread: nil) ⇒ void
This method returns an undefined value.
Posts a message to a channel
90 91 92 93 94 95 96 97 |
# File 'lib/waylon/slack/channel.rb', line 90 def post(text: nil, attachments: nil, blocks: nil, thread: nil) = { channel: id } [:text] = text if text [:attachments] = if [:blocks] = blocks if blocks [:thread_ts] = thread if thread sense.client.chat_postMessage() end |
#private? ⇒ Boolean
Is this a private channel? (meaning a direct message, NOT private in the Slack sense)
101 102 103 |
# File 'lib/waylon/slack/channel.rb', line 101 def private? data["is_im"].dup end |
#sense ⇒ Class
An instance-level helper to access the class-level method
107 108 109 |
# File 'lib/waylon/slack/channel.rb', line 107 def sense self.class.sense end |
#topic ⇒ String
Provides access to the Channel’s topic
113 114 115 |
# File 'lib/waylon/slack/channel.rb', line 113 def topic data.dig("topic", "value").dup end |