Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
6499807
set up the test_helper with all the required files and hide authentic…
mheshmati-tech Mar 11, 2020
b7de36a
created User and Channel class- they will use Recipient to inherint t…
mheshmati-tech Mar 11, 2020
93d205e
created workspace class that is composed of user and channel class an…
mheshmati-tech Mar 11, 2020
cdae88e
finished wave 1 of project- user can choose from three options <list …
mheshmati-tech Mar 11, 2020
ebe5005
set up test files for Recipient, User, Channel, and Workspace class
mheshmati-tech Mar 11, 2020
1feee43
wrote test for Channel to ensure .get and list_all work as expected
mheshmati-tech Mar 12, 2020
aa0b539
implemented method to see details on user/channel
mheshmati-tech Mar 12, 2020
f1deedf
implement method to find user and/or channel given their id or name- …
mheshmati-tech Mar 12, 2020
8b73dcb
implemented method using HTTParty to post message on slack
mheshmati-tech Mar 12, 2020
b5cb7ed
added select user/channel/details and send message to the options of …
mheshmati-tech Mar 12, 2020
b95d7c6
added cassettes from tests written for channel- only test written so …
mheshmati-tech Mar 12, 2020
2cf0de8
done with wave 3- refactored, no tests written yet
mheshmati-tech Mar 13, 2020
116a41d
wrote tests for User and made minor modification to Channel tests
mheshmati-tech Mar 13, 2020
0f24f90
wrote tests for all methods in workspace except for text_me which fun…
mheshmati-tech Mar 14, 2020
5d9f8ad
finishes writing tests for Workspace
mheshmati-tech Mar 16, 2020
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/coverage/
/coverage/
#ask: are we adding a coverage or is this already there just hidden?

.DS_Store

Expand Down
31 changes: 31 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative "recipient"


class Channel < Recipient
attr_reader :topic, :member_count

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

def self.list_all
response = Channel.get("https://slack.com/api/conversations.list")
channel_list = response["channels"].map do |channel|
Channel.new(
name = channel["name"],
slack_id = channel["id"],
topic = channel["topic"]["value"],
member_count = channel["num_members"]
)
end

return channel_list
end

def details
return "Channel name: #{name}\nSlack ID: #{slack_id}\nChannel topic: #{topic}\nMember count: #{member_count}"
end

end
39 changes: 39 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "httparty"
require "dotenv"
Dotenv.load
AUTH_TOKEN = ENV["SLACK_TOKEN"]

class Recipient
attr_reader :name, :slack_id

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

def send_message(channel, message)
url = "https://slack.com/api/chat.postMessage"
query = {
token: AUTH_TOKEN,
channel: channel,
text: message,
}

request = HTTParty.post(url, query: query)
raise(Exception, "Encountered an error: #{request["error"]}") if request["ok"] == false || request.code != 200
return request
end

def self.get(base_url)
query = {
token: AUTH_TOKEN,
}
response = HTTParty.get(base_url, query: query)
raise(Exception, "Encountered an error: #{response["error"]}") if response["ok"] == false || response.code != 200
return response
end

def self.list_all
raise(NotImplementedError, "Implement me in a child class!")
end
end
87 changes: 82 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,89 @@
#!/usr/bin/env ruby
require "httparty"
require "dotenv"
require "awesome_print"
require_relative "workspace"

def main
puts "Welcome to the Ada Slack CLI!"
puts "Welcome to the Mair Bear's Slack CLI!"
workspace = Workspace.new
puts "There are #{workspace.channels.length} channels and #{workspace.users.length} users in this Slack."

# TODO project
def pretty_user_list(workspace)
workspace.users.each do |user|
puts "🔸#{user.details}"
puts ""
end
end

puts "Thank you for using the Ada Slack CLI"
def pretty_channel_list(workspace)
workspace.channels.each do |channel|
puts "🔸#{channel.details}"
puts ""
end
end

while true
puts "\nWhat would you like to do?"
puts "\n🔵 list users\n🔵 list channels\n🔵 select user\n🔵 select channel\n🔵 details\n🔵 send message\n🔵 quit"
options = ["list users", "list channels", "select user", "select channel", "details", "quit", "send message"]
users_option = gets.chomp.downcase
until options.include? users_option
puts "Please input a valid command."
users_option = gets.chomp.downcase
end

case users_option
when "list users"
pretty_user_list(workspace)
when "list channels"
pretty_channel_list(workspace)
when "select user"
puts "enter username or slack ID of a user"
select_user = gets.chomp.downcase
workspace.select_user(select_user)
if workspace.selected.nil?
puts "no such user exists 😢"
elsif workspace.selected != nil
puts "you've selected a user yay! 🤗"
end
when "select channel"
puts "enter username or slack ID of a channel"
select_channel = gets.chomp.downcase
workspace.select_channel(select_channel)
if workspace.selected.nil?
puts "no such channel exists 🤔"
elsif workspace.selected != nil
puts "you've selected a channel yay! 🥳"
end


when "details"
if workspace.selected.nil?
puts "please select a user or channel to get details on"
else
puts workspace.details_of
end
when "send message"
puts "what is your message?"
message = gets.chomp
until message != nil && message != ""
puts "enter your message"
message = gets.chomp
end

if workspace.selected.nil?
puts "you must choose a channel or a user before you send a message"
elsif workspace.text_me(message) == false
puts "unable to send your message 🥺"
else
puts "your message has been succefully sent 📨"
end
when "quit"
break
end
end

puts "Thank you for using the Mair Bear's Slack CLI"
end

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

class User < Recipient
attr_reader :real_name

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

def self.list_all
response = User.get("https://slack.com/api/users.list")
user_list = response["members"].map do |member|
User.new(
name = member["name"],
slack_id = member["id"],
real_name = member["real_name"]
)
end

return user_list
end

def details
return "Slack name: #{name}\nSlack ID: #{slack_id}\nReal name: #{real_name}"
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 "httparty"
require_relative "channel"
require_relative "user"

class Workspace
attr_reader :users, :channels, :selected

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

def select_user(name_or_slack_id)
user = @users.find do |user|
user.name.downcase == name_or_slack_id.downcase || user.slack_id.downcase == name_or_slack_id.downcase
end

@selected = user unless user == nil
end

def select_channel(name_or_slack_id)
channel = @channels.find do |channel|
channel.name.downcase == name_or_slack_id.downcase || channel.slack_id.downcase == name_or_slack_id.downcase
end

@selected = channel unless channel == nil
end

def details_of
return @selected.details
end

def text_me(message)
channel = @selected.slack_id
request = @selected.send_message(channel, message)
return false if request["ok"] == false
end
end
84 changes: 84 additions & 0 deletions test/cassettes/get_user.yml

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

Loading