Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
03a849a
Added a .gitignore and .env to that file.
NatalieTapias Sep 9, 2019
cc0fefd
Added files for Recipient class and Recipient Test. Also verified our…
NatalieTapias Sep 10, 2019
4fc6986
Added tests for initializing SlackCLI::Recipient
NatalieTapias Sep 10, 2019
9565f89
We have troubleshooted VCR. We have written one test that makes one c…
NatalieTapias Sep 10, 2019
b54a8b1
We have a method within Module User to access a list of users in a Sl…
NatalieTapias Sep 11, 2019
a7a45cd
Found a way to accurately display channel information.
NatalieTapias Sep 11, 2019
646a8e0
Major breakthrough, now instantiating Channel objects, that are retur…
NatalieTapias Sep 12, 2019
8fe71de
Updated list method for Recipient, User, and Channel.
NatalieTapias Sep 12, 2019
d7dc431
Added a SlackApiError class inheriting from StandardError. We raise t…
NatalieTapias Sep 12, 2019
d212b8c
Slack.rb draft
NatalieTapias Sep 12, 2019
f384131
We have been ading to workspace test, testing the select user method
NatalieTapias Sep 13, 2019
f70f803
We wrote a test for method Workspace.select_channel
NatalieTapias Sep 13, 2019
ec53974
Making progress and resettign @selected in workspace to @nil each tim…
NatalieTapias Sep 13, 2019
928e058
Added channel workspace test to select a bogus channel. SlackAPIError…
NatalieTapias Sep 13, 2019
0b67fb5
Troubleshooted functionality on slack.rb
NatalieTapias Sep 13, 2019
6258d09
Added Show Details Menu Option
NatalieTapias Sep 13, 2019
d667167
Workspace now has a functioning message method\!\!
NatalieTapias Sep 13, 2019
ce44cda
Workspace Test is up and running
NatalieTapias Sep 13, 2019
16b95e1
Finished some final touches on the main file. Tests passing and cover…
NatalieTapias Sep 13, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
/test/version_tmp/
/tmp/


# Used by dotenv library to load environment variables.
# .env
.env

## Specific to RubyMotion:
.dat*
Expand Down
36 changes: 36 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'dotenv'
require_relative 'recipient'

Dotenv.load

module SlackCLI
class Channel < Recipient
attr_reader :slack_id, :name, :topic, :member_count

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

def self.list
response = HTTParty.get("https://slack.com/api/channels.list?token=#{ENV['SLACK_TOKEN']}")
unless response["ok"]

Choose a reason for hiding this comment

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

Nice work for raising an exception for an unsuccessful API call.

raise StandardError.new("Data Load Error")
end
array_of_channels = []

response["channels"].each do |channel|
info_hash = {
slack_id: channel["id"],
name: channel["name"],
topic: channel["topic"]["value"],
member_count: channel["members"].length
}
array_of_channels << SlackCLI::Channel.new(info_hash)
end
return array_of_channels
end
end
end
17 changes: 17 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'HTTParty'
require 'dotenv'

class SlackApiError < StandardError
end

module SlackCLI
class Recipient
def initialize

Choose a reason for hiding this comment

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

To fully use your parent class, include @name and @slack_id in Recipient since these are attributes of a User and Channel

end

def self.list
raise SlackApiError.new("Call this method in child class")

Choose a reason for hiding this comment

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

There's actually a special exception for this case called a NotImplementedError.

end
end
end

99 changes: 96 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,104 @@
#!/usr/bin/env ruby
require 'awesome_print'
require_relative 'workspace'
require "table_print"

Dotenv.load
@workspace = SlackCLI::Workspace.new
def main

Choose a reason for hiding this comment

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

Your CLI flow insures that a user/channel will always be selected before a message is attempted to be sent. However, you may consider giving your user all the menu options rather than forcing them to what you see as the logical next choice after they have say, selected a user. They may want to select a different user! :)

puts "Welcome to the Ada Slack CLI!"
menu_method
selection = gets.chomp

while selection == "1" || selection == "2"
print_table(selection)
puts "************************************"
learn_more_method(selection)
message_menu
menu_method
selection = gets.chomp
end

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

# TODO project
def menu_method

Choose a reason for hiding this comment

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

Nice work encapsulating functionality into methods.

# menu_options = ["List Users in the Workspace", "List Channels in the Workspace", "Select User", "Select Channel", "Quit program"]
puts "*********** MAIN MENU **************"
menu_options = ["List Users in the Workspace", "List Channels in the Workspace"]
menu_options.each_with_index do |prompt, i|
puts " #{i + 1}. #{prompt}"
end
puts "[Any Other Key] to Quit"
puts "************************************"
end

puts "Thank you for using the Ada Slack CLI"
def print_table(selection)
# determines whether you are going to print users or channels
if selection == "1"
tp @workspace.users
elsif selection == "2"
tp @workspace.channels
end
end

main if __FILE__ == $PROGRAM_NAME
def learn_more_method(selection)
# selection "1" is users
# selection "2" is channels
puts "Enter \"a\" to search by Channel Name or User Name\nEnter \"b\" to search by Slack ID\n[Any Other Key] to Quit"
find_by = gets.chomp

if find_by == "a"
puts "Enter search term:"
name_or_id = gets.chomp
begin
if selection == "1"
# this means they're looking for username
@workspace.select_user(user_name: name_or_id)
else
# looking for channel by channel name
@workspace.select_channel(name: name_or_id)
end
rescue
puts "NOT FOUND"
end

elsif find_by == "b"
puts "Enter search term:"
name_or_id = gets.chomp
begin
if selection == "1"
@workspace.select_user(slack_id: name_or_id)
# looking for channel by channel name
else
@workspace.select_channel(slack_id: name_or_id)
# this means they're looking for username
end
rescue
puts "NOT FOUND"
end
else
puts "Invalid menu selection"
end
end

def message_menu
puts "Enter \"a\" to Show Details\nEnter \"b\" to Send Message\n[Any Other Key] to Quit"
message_choice = gets.chomp
if message_choice == "a"
tp @workspace.show_details
elsif message_choice == "b"
begin
puts "Enter Message: "
message = gets.chomp
@workspace.send_message(message)
rescue
puts "Message Unable to Send"
end
else
puts "Invalid Menu Choice"
end
end

main if __FILE__ == $PROGRAM_NAME

29 changes: 29 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'recipient'
Dotenv.load

module SlackCLI
class User < Recipient
attr_reader :slack_id, :user_name, :real_name

def initialize(slack_id:, user_name:, real_name:)
@slack_id = slack_id
@user_name = user_name
@real_name = real_name
end

def self.list
response = HTTParty.get("https://slack.com/api/users.list?token=#{ENV['SLACK_TOKEN']}")

Choose a reason for hiding this comment

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

Consider moving the pull request to the class method self.get(url, params) in Recipient to get all your money's worth out of your parent class Recipient

array_of_users = []
response["members"].each do |member|
info_hash = {
slack_id: member["id"],
user_name: member["name"],
real_name: member ["profile"]["real_name"]
}
array_of_users << SlackCLI::User.new(info_hash)
end
return array_of_users
end
end
end

63 changes: 63 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative 'recipient'
# require_relative 'slack'
require_relative 'user'
require_relative 'channel'
Dotenv.load

module SlackCLI

class Workspace
attr_reader :users, :channels, :selected

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

def select_channel(name: nil, slack_id: nil)

Choose a reason for hiding this comment

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

This code is very similar to the code for select_user below. Could you DRY this up somehow?

@selected = nil
@channels.each do |channel|
if channel.name == name || channel.slack_id == slack_id
@selected = channel
end
end
if @selected.nil?
raise SlackApiError.new("Channel not found")
end
end

def select_user(user_name: nil, slack_id: nil)
@selected = nil
@users.each do |user|
if user.user_name == user_name || user.slack_id == slack_id
@selected = user
end
end
if @selected.nil?
raise SlackApiError.new("User not found")
end
end

def show_details
@selected
end


def send_message(message)
uri = 'https://slack.com/api/'
send_message = HTTParty.post("#{uri}/chat.postMessage", body: {
token: ENV['SLACK_TOKEN'],
text: message,
channel: @selected.slack_id },
headers: {'Content-Type' => 'application/x-www-form-urlencoded'})

unless send_message["ok"]
raise SlackApiError.new("Message Unable to Send")
end
return send_message
end
end

end

84 changes: 84 additions & 0 deletions test/cassettes/list_channels.yml

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

Loading