Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e1b190a
created lib and test files
llinneaa Sep 10, 2019
c7e97cc
program is communicating with api
llinneaa Sep 10, 2019
e013698
Passed tests for User #initialize method
dhan0406 Sep 10, 2019
68d7c7e
created tests for channel, troubleshooting indexing discrepancy betwe…
llinneaa Sep 11, 2019
657a8aa
updated channel tests, all tests passing
llinneaa Sep 11, 2019
c7cfbf1
added methods to user class and created Workspace class
dhan0406 Sep 11, 2019
0ebb857
Merge branch 'master' of https://github.com/llinneaa/slack-cli
llinneaa Sep 11, 2019
141febf
updated channel tests to match new token id and position
llinneaa Sep 11, 2019
47f7af8
Revised unit test for #self.list method in User class
dhan0406 Sep 11, 2019
0f208c3
troubleshooting
dhan0406 Sep 12, 2019
f414f47
wave 1 complete
llinneaa Sep 12, 2019
7e3b029
created CLI and added methods for @selected
dhan0406 Sep 13, 2019
63f789d
created tests for class Workspace
dhan0406 Sep 13, 2019
d51ccf6
updated command line prompts, cleaned up methods in recipient class
llinneaa Sep 13, 2019
4d2ab6d
merged conflicts, tests passing
llinneaa Sep 13, 2019
9a17cfd
updated channel and user tests
llinneaa Sep 13, 2019
200e9f1
created tests for Recipient class
dhan0406 Sep 13, 2019
700b736
Merge branch 'master' of https://github.com/llinneaa/slack-cli
dhan0406 Sep 13, 2019
0a9f4ee
added send message method and we are receiving messages to our workspace
llinneaa Sep 13, 2019
9e61a4b
created test for send_message method
dhan0406 Sep 13, 2019
c2f0172
minor changes to command line, need to merge
llinneaa Sep 13, 2019
05c241d
merged changes
llinneaa Sep 13, 2019
a934e3f
Added colorize gem to CLI for clarity
llinneaa Sep 13, 2019
d848531
Made changes to original pair project to match instructor design more…
llinneaa Nov 14, 2019
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
Binary file added .DS_Store
Binary file not shown.
35 changes: 35 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'recipient'
require 'httparty'
require 'dotenv'

Dotenv.load

module Slack
class Channel < Recipient
BASE_URL = "https://slack.com/api/channels.list"
TOKEN = ENV["SLACK_TOKEN"]
QUERY = {
token: TOKEN
}
attr_reader :id, :name, :topic, :member_count

def initialize(id, name, topic, member_count)
super(id, name)
@topic = topic
@member_count = member_count
end
end

def self.list
response = self.get(BASE_URL + "channels.list", query: QUERY)

channels = response["channels"].map do |channel|
self.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"])
end
return channels
end

def details
super.push("topic", "member_count")
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 @@
module Slack
class Recipient
URL = "https://slack.com/api/chat.postMessage"

attr_reader :id, :name

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

def get(url, query)
return HTTParty.get(url, query)
end

# ABSTRACT METHODS
def details
raise NotImplementedError, "Implement me in a child class!"
end

def self.list
raise NotImplementedError, "Implement me in a child class!"
end
end
end

68 changes: 64 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,71 @@
#!/usr/bin/env ruby
require_relative 'workspace'
require 'table_print'
require 'colorize'

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Slack::Workspace.new

# TODO project
command_table = [{command: 'list users', description: 'lists all users'}, {command: 'list channels', description: 'lists all channels'}, {command: 'select user', description: 'selects a user'}, {command: 'select channel', description: 'selects a channel'}, {command: 'details', description: 'shows recipient details'}, {command: 'send message', description: 'sends message to recipient'}]

puts "Welcome to the Ada Slack CLI!\n\n".colorize(:light_cyan)

tp command_table
puts "\n\nWhat would you like to do? Enter a command from the list above: ".colorize(:light_cyan)

puts "Thank you for using the Ada Slack CLI"
user_input = ""

while user_input != "quit"
user_input = gets.chomp

case user_input
when "list users"
workspace.display_users

when "list channels"
workspace.display_channels

when "select user"
puts "Enter the name or slack id of the user: ".colorize(:light_cyan)
user_input = gets.chomp
if workspace.select_user(user_input) == []
puts "Invalid entry! Please enter another user: ".colorize(:red)
user_input = gets.chomp
end

when "select channel"
puts "Enter the name or slack id of the channel: ".colorize(:light_cyan)
user_input = gets.chomp
if workspace.select_channel(user_input) == []
puts "Invalid entry! Please enter another channel: ".colorize(:red)
user_input = gets.chomp
end

when "details"
if workspace.selected == nil
puts "No recipient selected!".colorize(:red)
else
workspace.show_details
puts "\n\n"
end

when "send message"
if workspace.selected == nil
puts "No recipient selected!".colorize(:red)
else
puts "What do you want to send to #{workspace.selected[0].name}?"
message = gets.chomp
workspace.send_message(message)
end

when "quit"
puts "Thank you for using the Ada Slack CLI!".colorize(:light_cyan)
exit

else
puts "Invalid command!".colorize(:red)
end
puts "Enter another command or type 'quit' to exit the program.".colorize(:light_cyan)
end
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'
require 'httparty'
require 'dotenv'

Dotenv.load

module Slack
class User < Recipient
BASE_URL = "https://slack.com/api/users.list"
TOKEN = ENV["SLACK_TOKEN"]

QUERY = {
token: TOKEN}

attr_reader :id, :name, :real_name

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

def self.list
response = self.get(BASE_URL, query: QUERY)

users = response["members"].map do |member|
self.new(member["id"], member["name"], member["real_name"])
end
return users
end

def details
super << "real_name"
end

end
end

57 changes: 57 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require_relative 'user'
require_relative 'channel'
require_relative 'recipient'
require 'httparty'
require 'dotenv'

Dotenv.load

module Slack
class Workspace < Recipient
BASE_URL = "https://slack.com/api/"
TOKEN = ENV["SLACK_TOKEN"]
QUERY = {
token: TOKEN
}

attr_reader :users, :channels
attr_accessor :selected

def initialize
@users = User.list
@channels = Channel.list
@selected = nil
end

def display_users
tp @users, :id, :name, :real_name
puts "\n\n"
end

def display_channels
tp @channels, :id, :name, :topic, :member_count
puts "\n\n"
end

def select_user(user_input)
@selected = users.select { |user| user.name == user_input || user.id == user_input }
end

def select_channel(user_input)
@selected = channels.select { |channel| channel.name == user_input || channel.id == user_input }
end

def show_details
tp @selected
end

def send_message(message)
post_query = {
token: TOKEN,
channel: @selected[0].id,
text: message
}
HTTParty.post(BASE_URL + "chat.postMessage", query: post_query)
end
end
end
157 changes: 157 additions & 0 deletions test/cassettes/channel_instantiation.yml

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

Loading