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

Water - Iris and Jessica #7

Open
wants to merge 94 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 93 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
69bf55b
dotenv test
iris-lux Oct 6, 2020
2a08ee1
updated doteenv_test
iris-lux Oct 6, 2020
f5851a9
add VCR filter to test helper
iris-lux Oct 6, 2020
e722628
modified filter sensitive data config
jwinchan Oct 6, 2020
fdaeddf
created workspace class
jwinchan Oct 6, 2020
f9e88b0
added workspace class
jwinchan Oct 6, 2020
59aa085
created Recipient class and constructor method
iris-lux Oct 7, 2020
46c2351
created recpient test and test for constructor
iris-lux Oct 7, 2020
b67fcad
add require for dotenv and require_relative for recpient, deleted red…
iris-lux Oct 7, 2020
5d43a9a
added get, details, and list_all methods
jwinchan Oct 7, 2020
ec67365
added test to verify parameters for get method
jwinchan Oct 7, 2020
c77db13
created channel class
jwinchan Oct 7, 2020
9dc48b0
created user class and constructor method
jwinchan Oct 7, 2020
02cb338
added initilize tests
iris-lux Oct 7, 2020
295be86
addedNotIMplementedError tests
iris-lux Oct 7, 2020
695f407
took out arguemnts from constructor
iris-lux Oct 7, 2020
3b78f84
created constructor method for channel
jwinchan Oct 7, 2020
7ffa394
added constants to recipient method
jwinchan Oct 7, 2020
cfcd60c
added constants to user method
jwinchan Oct 7, 2020
8683608
edited before do
jwinchan Oct 7, 2020
7431b06
added parameters in before do
jwinchan Oct 7, 2020
5857759
added require_relative to user and channel files
jwinchan Oct 7, 2020
e48e23f
created tests for method and constructor
jwinchan Oct 7, 2020
548c1d1
corrected validate_id, fixed super method calls in channel and user, …
iris-lux Oct 7, 2020
b9c60c4
moved .env path
jwinchan Oct 7, 2020
0d6d76c
created user menu
jwinchan Oct 7, 2020
70efd75
called to slack API to read conversations.list
jwinchan Oct 7, 2020
3429b39
added list_channels call and list_users call for main
iris-lux Oct 8, 2020
3563aca
created Gemfile
jwinchan Oct 8, 2020
b13cf8c
workspace and respective tests
iris-lux Oct 8, 2020
a28b2e5
merging changes
jwinchan Oct 8, 2020
c05bec0
changed .env path
jwinchan Oct 8, 2020
e1e2ece
added dotenv.load
jwinchan Oct 8, 2020
7ee4d31
deleted dotenv.load
jwinchan Oct 8, 2020
4e2572f
now has working cassette
jwinchan Oct 8, 2020
4c74bde
created self.list_all method
jwinchan Oct 8, 2020
43a286f
changed nothing
jwinchan Oct 8, 2020
e5cea3e
created list_all method
jwinchan Oct 8, 2020
9b88f1e
changed output statements and now calls from user and channel class
jwinchan Oct 8, 2020
986dafa
changed user test
jwinchan Oct 8, 2020
8674ed7
added menu options for select
iris-lux Oct 8, 2020
38ba753
added select method
iris-lux Oct 8, 2020
d9df74c
cassette changed
iris-lux Oct 8, 2020
88d201f
created details method
jwinchan Oct 8, 2020
bf71c29
updated details option
jwinchan Oct 8, 2020
1f0bfb2
created details method
jwinchan Oct 8, 2020
561f269
created show details method
jwinchan Oct 8, 2020
046011c
tested list all and details methods
jwinchan Oct 8, 2020
ff92f77
tested list all and details methods
jwinchan Oct 8, 2020
b156298
tested show details method
jwinchan Oct 8, 2020
cbe5d66
updated cassettes
jwinchan Oct 8, 2020
ef8e895
new cassettes
jwinchan Oct 8, 2020
96ff74a
added send message to interface
iris-lux Oct 8, 2020
d92b0e4
added send_message
iris-lux Oct 8, 2020
d469636
added tests for send_message
iris-lux Oct 8, 2020
2f3a1ec
new cassetts for send_message
iris-lux Oct 8, 2020
2cea429
updated send message syntax
jwinchan Oct 9, 2020
f2df810
raise error for incorrect input
jwinchan Oct 9, 2020
deb623b
recorded new cassettes for testing
jwinchan Oct 9, 2020
1372365
required relative to new error file
jwinchan Oct 9, 2020
9d1e3b5
created tests for edge cases
jwinchan Oct 9, 2020
93f7ee0
created custom error
jwinchan Oct 9, 2020
bef3c33
created new cassettes
jwinchan Oct 9, 2020
4fd4fc9
added select method
iris-lux Oct 9, 2020
c136561
added details and send_message msg
iris-lux Oct 9, 2020
ad290fb
removed unnecesary code
iris-lux Oct 9, 2020
abe81c7
refactored select method
iris-lux Oct 9, 2020
301fcfd
fixed merge conflict
iris-lux Oct 9, 2020
f88c771
added channel history option
jwinchan Oct 9, 2020
a99623a
created channel history method
jwinchan Oct 9, 2020
1a75b2c
tested channel history method
jwinchan Oct 9, 2020
b1f8312
recorded new cassettes
jwinchan Oct 9, 2020
7dad6d9
fixed spacing
iris-lux Oct 9, 2020
6030580
added error for bad api calls
iris-lux Oct 9, 2020
c0ce8d0
refactored select
iris-lux Oct 9, 2020
a2604eb
refactored select - duck typing instead of if
iris-lux Oct 9, 2020
6efee41
revised test for refactored select method
iris-lux Oct 9, 2020
a8b0ecb
refactored select method
iris-lux Oct 9, 2020
d56a922
revised gemfiles and added nu cassettes
iris-lux Oct 9, 2020
e334b4a
modified send message and channel history method
jwinchan Oct 9, 2020
c590b61
added send message method
jwinchan Oct 9, 2020
52468a6
modified send message and history
jwinchan Oct 9, 2020
280fc43
did not make changes
jwinchan Oct 9, 2020
cbaa629
modified send message and history methods
jwinchan Oct 9, 2020
8078983
modified test to accomodate the table print changes
jwinchan Oct 9, 2020
a29cdb7
new cassettes and modified gemfile
jwinchan Oct 9, 2020
a25ba36
fixed indentation
iris-lux Oct 9, 2020
37190c4
added argument error to send_message
iris-lux Oct 9, 2020
e9a82ef
refactored recipient test
iris-lux Oct 9, 2020
43bb11e
refactored test for changes in send_message and conversation_history
iris-lux Oct 9, 2020
d9bb93e
new cassettes
iris-lux Oct 9, 2020
a16f7c0
added line to give workspace info
jwinchan Oct 9, 2020
4c981ef
modified workspace info statement
jwinchan Oct 9, 2020
dbce3f4
test
iris-lux Nov 4, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
.DS_Store

# Ignore environemnt variables
.env
.env
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"


gem 'rake'
gem 'minitest'
gem 'minitest-spec'
gem 'minitest-reporters'
gem "pry"
gem 'minitest-skip'
gem 'httparty'
gem 'dotenv'
gem 'table_print'
gem 'vcr'
gem 'simplecov'
56 changes: 56 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
GEM
remote: https://rubygems.org/
specs:
ansi (1.5.0)
builder (3.2.4)
coderay (1.1.3)
docile (1.3.2)
dotenv (2.7.6)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
minitest (5.14.2)
minitest-reporters (1.4.2)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
minitest-skip (0.0.3)
minitest (~> 5.0)
minitest-spec (0.0.2.1)
minitest (>= 3.0)
multi_xml (0.6.0)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.1)
ruby-progressbar (1.10.1)
simplecov (0.19.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov-html (0.12.3)
table_print (1.5.7)
vcr (6.0.0)

PLATFORMS
ruby

DEPENDENCIES
dotenv
httparty
minitest
minitest-reporters
minitest-skip
minitest-spec
pry
rake
simplecov
table_print
vcr

BUNDLED WITH
2.1.4
29 changes: 29 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'recipient'

module SlackCLI
class Channel < Recipient

attr_reader :slack_id, :name, :topic, :member_count

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

def self.list_all
return self.get("conversations.list", {token: ENV["SLACK_API_TOKEN"]})["channels"].map do |channel|
self.new(slack_id: channel["id"], name: channel["name"], topic: channel["topic"]["value"], member_count: channel["num_members"])
end
end

def details
return "ID: #{@slack_id} \nName: #{@name} \nTopic: #{@topic} \nMember Count: #{@member_count}"
end

def channel_history
response = Channel.get("conversations.history", {token: ENV["SLACK_API_TOKEN"], channel: @slack_id})
return response["messages"].map{ |message| message["text"] }
end
end
end
64 changes: 64 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'dotenv'
require 'httparty'
require_relative 'slackapierror'

Dotenv.load

module SlackCLI
class Recipient

attr_reader :name, :slack_id
def initialize(slack_id:, name:)
self.class.validate_id(slack_id)
@slack_id = slack_id
@name = name
end

def self.validate_id(id)
if id.nil?
raise ArgumentError, 'ID cannot be blank.'
end
end

def self.get(end_point, params)
raise ArgumentError.new("invalid arguments") unless (end_point.is_a?(String) && params.is_a?(Hash))
response = HTTParty.get("https://slack.com/api/#{end_point}", query: params)
unless response.code == 200 && response.parsed_response["ok"]
raise SlackAPIError.new("Error: #{response.parsed_response["error"]}")
end
return response
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

def self.select(identifier)
return self.list_all.find{|recipient| recipient.slack_id == identifier || recipient.name == identifier}
end

def send_message(message)

response = HTTParty.post(
"https://slack.com/api/chat.postMessage",
body: {
token: ENV["SLACK_API_TOKEN"],
text: message,
channel: @slack_id,
as_user: "true"
},
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
)

unless response.code == 200 && response.parsed_response["ok"]
raise SlackAPIError.new("Error: #{response.parsed_response["error"]}")
end
Comment on lines +57 to +59

Choose a reason for hiding this comment

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

You use this code a couple places. Consider breaking out this functionality into a helper method.


return true
end
end
end
79 changes: 76 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
#!/usr/bin/env ruby
require 'dotenv'
require 'httparty'
require 'table_print'
require_relative 'workspace'

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

# TODO project
puts "Welcome to the Ada Slack CLI!\n\n"

Choose a reason for hiding this comment

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

The user experience is fantastic. Great work breaking this CLI down into helper methods and gracefully handling bad user input.

workspace = SlackCLI::Workspace.new
puts "This workspace has #{workspace.channels.length} channels and #{workspace.users.length} users."

option = nil
current_selection = nil
until option == "quit" || option == "8"
if option == "list users" || option == "1"
tp workspace.list_users
elsif option == "list channels" || option == "2"
tp workspace.list_channels
elsif option == "select user" || option == "3"
puts "Enter the name or id"
current_selection = workspace.select(SlackCLI::User,gets.chomp)
puts "Selection not found" if(current_selection.nil?)
elsif option == "select channel" || option == "4"
puts "Enter the name or id"
current_selection = workspace.select(SlackCLI::Channel,gets.chomp)
puts "Selection not found" if(current_selection.nil?)
elsif option == "details" || option == "5"
details(current_selection, workspace)
elsif option == "send message" || option == "6"
send_message(current_selection, workspace)
elsif option == "channel history" || option == "7"
message_history(current_selection, workspace)
elsif option.nil?
puts ""
else
puts "Please input a valid option"
end
menu
option = gets.chomp.downcase
end
puts "Thank you for using the Ada Slack CLI"
end

def details(recipient, workspace)
if recipient.nil?
puts "No recipient selected"
else
puts workspace.show_details(recipient)
end
end

def send_message(recipient, workspace)
if recipient.nil?
puts "No recipient selected"
else
puts "Please enter message"
workspace.send_message(gets.chomp, recipient)
end
end

def message_history(recipient, workspace)
if recipient.nil? || recipient.is_a?(SlackCLI::User)
puts "Please select a channel"
else
puts workspace.conversation_history(recipient)
end
end

def menu
puts "**" * 20
puts "Please select one of the three options: "
puts "1. list users"
puts "2. list channels"
puts "3. select user"
puts "4. select channel"
puts "5. details"
puts "6. send message"
puts "7. channel history"
puts "8. quit"
puts "**" * 20
end

main if __FILE__ == $PROGRAM_NAME
4 changes: 4 additions & 0 deletions lib/slackapierror.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SlackCLI
class SlackAPIError < StandardError
end
end
25 changes: 25 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative 'recipient'

module SlackCLI
class User < Recipient

attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji

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

def self.list_all
return self.get("users.list", {token: ENV["SLACK_API_TOKEN"]})["members"].map do |user|
self.new(slack_id: user["id"], name: user["name"], real_name: user["real_name"], status_text: user["profile"]["status_text"], status_emoji: user["profile"]["status_emoji"])
end
end

def details
return "ID: #{@slack_id} \nName: #{@name} \nReal Name: #{@real_name} \nStatus: #{@status_text} \nEmoji: #{@status_emoji}"
end
end
end
41 changes: 41 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'httparty'
require_relative 'user'
require_relative 'channel'

module SlackCLI
class Workspace
attr_reader :users, :channels

def initialize
@users = User.list_all
@channels = Channel.list_all

Choose a reason for hiding this comment

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

Consider include an instance variable @selected such that show_details and send_message would not need to take recipient as a parameter but could use the object stored in @selected

end

def list_users
return @users.map{|user| {id: user.slack_id, name: user.name, real_name: user.real_name} }
end

def list_channels
return @channels.map{|channel| {id: channel.slack_id, name: channel.name, topic: channel.topic, member_count: channel.member_count}}
end

def select(recipient_class, identifier)
raise ArgumentError.new("Argument cannot be empty") if(identifier == nil)
raise ArgumentError.new("Recipient class must be user or channel") unless recipient_class == SlackCLI::User || recipient_class == SlackCLI::Channel
recipient_class.select(identifier)
end

def show_details(recipient)
return recipient.details
end

def send_message(message, recipient)
raise ArgumentError.new("recipient must be of Recipient class") unless recipient.is_a? Recipient
recipient.send_message(message)
end

def conversation_history(channel)
return channel.channel_history
end
end
end
Loading