Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

.DS_Store

# Ignore environemnt variables
.env
# Ignore environment variables
.env
38 changes: 38 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative 'recipient'

module Slack
class Channel < Recipient

attr_reader :name, :topic, :num_members, :id

def initialize(id, name, topic, num_members)

super(id, name)

@topic = topic ||= "No topic"
@num_members = num_members

end

def details
tp self, "id", "name", "topic", "num_members"
end

def self.load_all
query_results = HTTParty.get(BASE_URL + "channels.list", query: {token: KEY})
channel_list = query_results["channels"].to_a.map {
|channel| Channel.new(channel["id"], channel["name"], channel["topic"["value"]], channel["members"].length)
}
return channel_list
end

def send_message(message)
if message.class != String
raise ArgumentError.new("Message: #{message} must be a string!")
else
HTTParty.post(BASE_URL + "chat.postMessage", query: {token: KEY, channel: @id, text: message})
end
end

end
end
26 changes: 26 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'httparty'

module Slack
class Recipient

attr_reader :id, :name

def initialize(id, name)
@id = id
@name = name
end

def load_all
raise NotImplementedError.new("Implement me in a child class!")
end

def details
raise NotImplementedError.new("Implement me in a child class!")
end

def send_message
raise NotImplementedError.new("Implement me in a child class!")
end

end
end
65 changes: 61 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require 'table_print'
require 'tty-prompt'

require_relative 'workspace'

Dotenv.load

BASE_URL = "https://slack.com/api/"
KEY = ENV["SLACK_TOKEN"]

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
workspace = Slack::Workspace.new
prompt = TTY::Prompt.new
continue = true
# active_selection = nil

selection = nil

puts "Welcome to the Ada Slack CLI!\n"

# TODO project
until continue == nil
prompt.select("Please make a selection from the following options:") do |menu|
menu.choice 'list users', -> {
tp workspace.users, "id", "name", "real_name"
}
menu.choice'list channels', -> {
tp workspace.channels, "id", "name", "topic", "num_members"
}
menu.choice 'select user', -> {
tp workspace.users, "id", "name", "real_name"
puts "Please enter the name or id of the user you would like to select:"
user_selection = gets.chomp.to_s
selection = workspace.select_user(user_selection)
}
menu.choice 'select channel', -> {
tp workspace.channels, "id", "name", "topic", "num_members"
puts "Please enter the name or id of the channel you would like to select:"
user_selection = gets.chomp.to_s
selection = workspace.select_channel(user_selection)
}
menu.choice 'send message', -> {
if selection == nil
puts "There is no recipient selected. Please select a channel or user first."
else
puts "Please enter the message you would like to send"
message = gets.chomp.to_s
selection.send_message(message)
end
}
menu.choice 'details', -> {
if selection == nil
puts "There is no recipient selected. Please select a channel or user first."
else
selection.details
end
}
menu.choice 'quit', -> {
continue = nil
}
end
end

puts "Thank you for using the Ada Slack CLI"
puts "\nThank you for using the Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME
37 changes: 37 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative 'recipient'

module Slack
class User < Recipient

attr_reader :real_name

def initialize(id, name, real_name)

super(id, name)

@real_name = real_name ||= "Not specified"

end

def details
tp self, "id", "name", "real_name"
end

def self.load_all
list_results = HTTParty.get(BASE_URL + "users.list", query: {token: KEY})
users_list = list_results["members"].to_a.map {
|user| User.new(user["id"], user["name"], user["real_name"])
}
return users_list
end

def send_message(message)
if message.class != String
raise ArgumentError.new("Message: #{message} must be a string!")
else
HTTParty.post(BASE_URL + "chat.postMessage", query: {token: KEY, channel: @id, text: message})
end
end

end
end
39 changes: 39 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative 'user'
require_relative 'channel'

module Slack
class Workspace

attr_reader :users, :channels

def initialize
@users = Slack::User.load_all
@channels = Slack::Channel.load_all
end

def select_user(user_selection)
selected_user = @users.select {
|user| user.name.downcase == user_selection.downcase || user.id == user_selection || user.real_name.downcase == user_selection.downcase
}

if selected_user[0] == nil
raise ArgumentError.new("There is no user matching #{user_selection}.")
end

return selected_user[0]
end

def select_channel(given_channel)
selected_channel = @channels.select {
|channel| channel.name.downcase == given_channel.downcase || channel.id == given_channel
}

if selected_channel[0] == nil
raise ArgumentError.new("There is no channel matching #{given_channel}.")
end

return selected_channel[0]
end

end
end
160 changes: 160 additions & 0 deletions test/cassettes/channel.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading