Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rachael and Christabel Slack-CLI #3

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
3e73a21
Added verification.rb
RachaelGomez Oct 6, 2020
3c79295
added workspace class, reconfigured test-helper, added workspace clas…
RachaelGomez Oct 6, 2020
aeed83c
added workspace_test.rb, added requires to all files. Unable to run t…
RachaelGomez Oct 6, 2020
7989599
Added positive nominal #load_channels test
cescarez Oct 6, 2020
1871099
Added `require httparty` and commented out VCR config... for now.
cescarez Oct 6, 2020
48afa3f
Added additional tests for #load_channels
cescarez Oct 6, 2020
9d4a62c
added load_user method and tests
RachaelGomez Oct 6, 2020
cb74164
Fixed indentation
cescarez Oct 6, 2020
eef19de
added driver code for CLI program in lib/slack.rb and input validation
cescarez Oct 6, 2020
37a7b14
created user and channel classes
RachaelGomez Oct 7, 2020
76923af
Refactored `require` headers and moved "load channels" test to channe…
cescarez Oct 7, 2020
8d853c0
Refactored `require` headers
cescarez Oct 7, 2020
0ba025b
Refactored `require` headers
cescarez Oct 7, 2020
8611f02
Changed #error_message to .error_message
cescarez Oct 7, 2020
1a73d91
Refactored `require` headers
cescarez Oct 7, 2020
73e2c7d
Wrote tests for constructor and test for .list_all
cescarez Oct 7, 2020
6a1f624
Wrote exception test for .list_all
cescarez Oct 7, 2020
fcc0b2f
Modified self.get call in .list_all and added `:query` keyword
cescarez Oct 7, 2020
89aacbf
Added (and commented out) VCR config line to allow HTTP requests outs…
cescarez Oct 7, 2020
4286d73
Modified self.get call in .list_all and added `:query` keyword
cescarez Oct 7, 2020
909558b
Added sleep(1) to .get to prevent token lockout in cases of infinite …
cescarez Oct 7, 2020
413bc43
added user tests and added extra details test to channel_test.rb
RachaelGomez Oct 7, 2020
e0292e5
added select_channel, select_user, and show_details and wrote tests f…
RachaelGomez Oct 8, 2020
44b98c9
added cassette files
RachaelGomez Oct 8, 2020
f197b06
added final tests to show_details checking positive and negative nomi…
RachaelGomez Oct 8, 2020
5bcbeb7
added selections for select user, select channel and details for thes…
RachaelGomez Oct 8, 2020
7e22e63
uncommented vcr config that allows http request outside of VCR contro…
RachaelGomez Oct 8, 2020
66a0947
changed nil output sentence to be more clear to the user what went wr…
RachaelGomez Oct 8, 2020
52c5fa9
Removed test_helper require
cescarez Oct 8, 2020
8d47a90
created send_message method and tests for recipient_test.rb send mess…
RachaelGomez Oct 8, 2020
9a92c48
changed cassette name for expected Errors in .get test
cescarez Oct 8, 2020
cb11811
Add cassettes
cescarez Oct 8, 2020
3ae4924
Added addtional tests for send_message (in progress)
cescarez Oct 8, 2020
edbced7
Added cassettes
cescarez Oct 8, 2020
3e58c1b
Added all tests for send_message
cescarez Oct 8, 2020
f28a63b
added require httparty in recipient.rb
RachaelGomez Oct 8, 2020
ccda1c6
added selections for send message to slack.rb.
RachaelGomez Oct 8, 2020
f65cf01
added cassettes
RachaelGomez Oct 8, 2020
dab22bb
removed sleeps
RachaelGomez Oct 8, 2020
b049650
reformmated how list of users and channels each print using table pri…
RachaelGomez Oct 8, 2020
aa96783
Add cassette
cescarez Oct 8, 2020
dcd3e2f
Combined selections 3/4 in `main` case statement
cescarez Oct 8, 2020
96a1faf
Changed .error_message condition to only evaluate response["ok"] and …
cescarez Oct 8, 2020
00d37c4
added .to_i to member_count constructor
cescarez Oct 8, 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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

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

CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list"

class Channel < Recipient
attr_reader :topic, :member_count

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

def details
return "Channel id: #{@slack_id}\nChannel name: #{@name}\nTopic: #{@topic}\nNumber of members: #{@member_count}"
end

def self.list_all
query = {token: ENV["SLACK_TOKEN"]}

request = self.get(CONVERSATIONS_LIST_URL, query: query)

return request["channels"].map do |channel|
self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"].to_i)
end
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'

POST_URL = "https://slack.com/api/chat.postMessage"

class Recipient
attr_reader :slack_id, :name

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

def send_message(message)
params = {token: ENV['SLACK_TOKEN'], channel: @slack_id, text: message}
return self.class.error_message(HTTParty.post(POST_URL, body: params))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot in one line. Consider making the post request, storing the response in a variable likeresponse and then calling the helper method error_message to increase readability.

end

def self.get(url, params)
return error_message(HTTParty.get(url,params))
end

def details
raise NotImplementedError, 'Implement me in a child class!'
end
def self.list_all
raise NotImplementedError, 'Implement me in a child class!'
end

private

def self.error_message(response)
if response["ok"] != true
raise ArgumentError, "API request failed with error: #{response["error"]}."
else
return response
end
end
Comment on lines +31 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of a helper method


end
88 changes: 87 additions & 1 deletion lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,98 @@
#!/usr/bin/env ruby
require 'dotenv'
require "table_print"
require_relative 'workspace'


def main
Dotenv.load

puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
workspace.load_users
workspace.load_channels
puts "Amount of users in workspace: #{workspace.users.length}"
puts "Amount of channels in workspace: #{workspace.channels.length}"


puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit"
selection = number_validation(gets.chomp, 5)

# TODO project
while (1..4).include? selection
case selection
when 1
tp workspace.users, "slack_id", "name", "real_name", "status_text", "status_emoji"
when 2
tp workspace.channels, "slack_id", "name", "topic", "member_count"
when 3, 4
puts "Please input a name or slack id"
if selection == 3
selected_recipient = workspace.select_user(gets.chomp)
else
selected_recipient = workspace.select_channel(gets.chomp)
end

if selected_recipient
puts "Would you like details? (yes/no)"
details_selection = input_validation(gets.chomp.downcase)
if details_selection == "yes"
puts workspace.show_details(selected_recipient)
end
puts "Would you like to #{selected_recipient.class == User ? "send a message" : "post"} to #{selected_recipient.name}? (yes/no)"
message_selection = input_validation(gets.chomp.downcase)
if message_selection == "yes"
puts "Please write your message."
message = gets.chomp
selected_recipient.send_message(message)
end
else
puts "No #{(selection == 3) ? "user" : "channel"} found."
end
Comment on lines +35 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice choice to only prompt the user for details of sending message if a user has been selected. Consider breaking the functionality to show details and send message into helper methods.


end

puts "Make another selection:"
puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit"
selection = number_validation(gets.chomp, 5)
end

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

def number_validation(input, max_num)
input = translate_input(input)

until (1..max_num).include? input
puts "Invalid input. Please re-enter a selection."
input = translate_input(gets.chomp)
end

return input
end

def input_validation(input)
until ["yes", "no"].include? input
puts "Invalid input. Please re-enter either a yes or no."
input = gets.chomp.downcase
end
return input
end

def translate_input(string_input)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of a helper method.

case string_input.downcase
when "list users", "1"
return 1
when "list channels", "2"
return 2
when "select user", "3"
return 3
when "select channel", "4"
return 4
when "quit", "5"
return 5
else
return string_input
end
end

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

USERS_LIST_URL = "https://slack.com/api/users.list"

class User < Recipient
attr_reader :real_name, :status_text, :status_emoji
def initialize(slack_id, name, real_name, status_text, status_emoji)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def details
return "User id: #{@slack_id}\nUsername: #{@name}\nReal name: #{@real_name}\nStatus text: #{@status_text}\nStatus emoji: #{@status_emoji}"
end

def self.list_all
query = {token: ENV['SLACK_TOKEN']}

request = self.get(USERS_LIST_URL, query: query)

@users = request["members"].map do |user|
self.new(user["id"],user["name"],user["real_name"],user["profile"]["status_text"],user["profile"]["status_emoji"])

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

Dotenv.load # allows access to ENV['VARIABLE_NAME']

CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list"

def verification
query = {
token: ENV["SLACK_TOKEN"]
}

request = HTTParty.get(CONVERSATIONS_LIST_URL, query: query)

if request.code != 200 || request["ok"] != true
return "API request failed with error code #{request.code} and #{request["error"]}."
else
channel_names = request["channels"].map { |channel| channel["name"] }
return channel_names
end
end

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

class Workspace
attr_reader :users, :channels

def initialize
@users = []
@channels = []
end

def load_channels
@channels = Channel.list_all
end

def load_users
@users = User.list_all
end

def select_channel(search_term)
return @channels.find{|channel| channel.name == search_term.downcase || channel.slack_id == search_term.upcase}
end

def select_user(search_term)
return @users.find{|user| user.name == search_term.downcase || user.slack_id == search_term.upcase}
end

def show_details(recipient)
if recipient
return recipient.details
else
return "Invalid recipient. Unable to display details"
end
end

end


125 changes: 125 additions & 0 deletions test/cassettes/get_conversations_list.yml

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

Loading