Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

# Ignore environemnt variables
.env

30 changes: 30 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative 'recipient.rb'
require 'table_print'

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

def initialize(channel_name, slack_id, member_count, topic)
super(slack_id)

@channel_name = channel_name
@member_count = member_count
@topic = topic
end

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

# Will retrieve the data from the API using the URL
def self.list_all
response = super('conversations.list')
channels = []
response["channels"].each do |channel|
channels << Channel.new(channel["name"], channel["id"], channel["num_members"].to_i, channel["topic"]["value"])
end
return channels
end
end
end
42 changes: 42 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'httparty'
require 'dotenv'

Dotenv.load

TOKEN = ENV['SLACK_TOKEN']
URL = "https://slack.com/api/"

module SlackCLI
class Recipient

attr_reader :slack_id

def initialize(slack_id)
@slack_id = slack_id
end

def self.list_all(list)
url = "#{URL}"<<"#{list}"
Copy link

Choose a reason for hiding this comment

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

This syntax works, as we can shovel strings into other strings.

Just for fun, I want to provide these other syntaxes you could use, in case you wanted something a little more straightforward:

url = "#{URL}#{list}"
url = URL + list

query_params = {token: TOKEN}
response = HTTParty.get(url, query: query_params)
return response
end

def send_message(message)
url = "#{URL}chat.postMessage"

response = HTTParty.post(url,
headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: {
token: TOKEN,
channel: @slack_id,
text: message
})
unless response.code == 200
raise ArgumentError.new("Request Error")
end
return true
end

end
end
72 changes: 70 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
#!/usr/bin/env ruby
require 'table_print'
require_relative 'workspace'

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

# TODO project
options_list
choice = get_user_choice
execute_choice(workspace, choice)

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

def options_list
print "Choose from the following:\n"
puts "list users\nlist channels\nselect user\nselect channel\ndetails\nsend message\nquit"
end

def get_user_choice
user_choice = gets.chomp
options = ["list users", "list channels", "quit", "select user", "select channel", "details", "send message"]

until options.include?(user_choice)
puts "Invalid option. Please type: list users, list channels, select user, select channel, details, send message or quit"
user_choice = gets.chomp
end

return user_choice
end

def execute_choice(workspace, choice)
given_data = nil
until choice == "quit"
if choice == "list users"
tp workspace.users.list, "username", "slack_id", "real_name"
Copy link

Choose a reason for hiding this comment

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

Your code breaks here, because workspace.users returns an array of Users, and an array of Users doesn't have a method named list! Is it possible that you all meant workspace.users_list?

puts "\n"
elsif choice == "list channels"
tp workspace.channels_list, "channel_name", "slack_id", "topic", "member_count"
elsif choice == "select user"
print "Please enter username or Slack ID: "
user_info = gets.chomp
given_data = workspace.select_user(user_info)
if given_data.nil?
puts "User not found"
end
elsif choice == "select channel"
print "Please select channel name or Slack ID: "
channel_info = gets.chomp
given_data = workspace.select_channel(channel_info)
if given_data.nil?
puts "Channel not found."
end
elsif choice == "details"
if given_data != nil
puts given_data.details
elsif given_data != nil
puts given_data.details
else
puts "There is no user or channel that matches your search"
end
elsif choice == "send message"
if given_data.nil?
puts "Cannot send message. Please choose a recipient first."
else
puts "Please type your message:"
user_msg = gets.chomp
given_data.send_message(user_msg)
puts "Success!"
end
end

puts "*****\n\n"
options_list
choice = get_user_choice
end
end

main if __FILE__ == $PROGRAM_NAME
35 changes: 35 additions & 0 deletions lib/user.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 SlackCLI
class User < Recipient
attr_reader :username, :slack_id, :real_name

def initialize(username, slack_id, real_name)
super (slack_id)
Copy link

Choose a reason for hiding this comment

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

Just a very small nitpick: It'll be clearer that this is a super method call without the space, so super(slack_id)


@username = username
@real_name = real_name
end

def details
return "username: #{@username}\nSlack ID: #{@slack_id}\nreal name: #{@real_name}\n"
end

#will retrieve users data from the API using URL
def self.list_all
response = super('users.list')
users = []
response["members"].each do |user|
users << User.new(user["name"], user["id"], user["real_name"])
end
return users
end

end
end


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

module SlackCLI
class Workspace

attr_reader :users, :channels
Copy link

Choose a reason for hiding this comment

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

I don't think .channels is ever used in this project (instead your project uses channels_list as a method to get a list of channels), so you could technically remove channels from this attr_reader

It might be worth conforming to either attr_reader OR the channels_list method


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

def users_list
return @users
end
Comment on lines +14 to +16
Copy link

Choose a reason for hiding this comment

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

The methods users_list is only used once in this project: in the tests! If this method isn't used in this project, then feel free to delete it!


def channels_list
return @channels
end

def select_user(user_info)
name_search = @users.find{|user| user.username == user_info}
id_search = @users.find{|user| user.slack_id == user_info}

if name_search.nil?
@selected_user = id_search
elsif id_search.nil?
@selected_user = name_search
else
puts "Invalid User name or ID"
Copy link

Choose a reason for hiding this comment

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

  1. Tiny nitpick: Watch your indentation!

  2. Instead of putting the puts "Invalid User name or ID" line right here in the Workspace class, we could remove this line, and possibly return nil. Then, in any code that uses this method (aka slack.rb, maybe we could check if the return value is nil and THEN print puts "Invalid User name or ID" there (in slack.rb)

Let me know if you have questions on this!

end
return @selected_user
end

def select_channel(channel_info)
name_search = @channels.find{|channel| channel.channel_name == channel_info}
id_search = @channels.find{|channel| channel.slack_id == channel_info}

if name_search.nil?
@selected_channel = id_search
elsif id_search.nil?
@selected_channel = name_search
else
puts "Invalid Channel name or ID"
Copy link

Choose a reason for hiding this comment

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

Same comment as above forselect_user

end
return @selected_channel
end

end
end
22 changes: 22 additions & 0 deletions test/channel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative 'test_helper.rb'

describe "class Channel" do
before do
@channel = SlackCLI::Channel.new("seattle-stuff", "U786YWDVC3D", 71, "coffee shops")
end

it "creates an instance of channel" do
expect(@channel).must_be_kind_of SlackCLI::Channel
end

it "returns channel details correctly" do
expect(@channel.details).must_be_kind_of String
end

it "returns a list of channels" do
VCR.use_cassette "channels_list" do
response = SlackCLI::Channel.list_all
expect(response).must_be_kind_of Array
end
end
Comment on lines +16 to +21
Copy link

Choose a reason for hiding this comment

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

It's true that a request-response cycle happens during the SlackCLI::Channel.list_all method call-- you're right! When we call SlackCLI::Channel.list_all, at some point, we make the API call with HTTParty in it.

However, this line in this test reads a little weirdly. What gets returned from the SlackCLI::Channel.list_all? A list of channels gets returned from that method call, not a response!

It might read better overall to see this test as...

Suggested change
it "returns a list of channels" do
VCR.use_cassette "channels_list" do
response = SlackCLI::Channel.list_all
expect(response).must_be_kind_of Array
end
end
it "returns a list of channels" do
VCR.use_cassette "channels_list" do
channels = SlackCLI::Channel.list_all
expect(channels).must_be_kind_of Array
end
end

Especially when you read it with your well-written it "returns a list of channels" line!

end
20 changes: 20 additions & 0 deletions test/recipient_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative 'test_helper.rb'

describe "Recipient class" do

it "stores id correctly" do
recipient_id = "GTB29AAZ"
recipient = SlackCLI::Recipient.new(recipient_id)
expect(recipient.slack_id).must_equal recipient_id
end

it "retrieves data from chosen list" do
VCR.use_cassette "list_all" do
response = SlackCLI::Recipient.list_all("conversations.list")
expect(response.code).must_equal 200
end
end



end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'vcr'
require 'dotenv'

Dotenv.load

require_relative '../lib/workspace'
require_relative '../lib/user'
require_relative '../lib/channel'
require_relative '../lib/recipient'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

Expand All @@ -25,5 +33,8 @@
}

# Don't leave our token lying around in a cassette file.
config.filter_sensitive_data("SLACK_TOKEN") do
ENV["SLACK_TOKEN"]
end

end
24 changes: 24 additions & 0 deletions test/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative 'test_helper'

describe "User Class" do
before do
@users = SlackCLI::User.new("ruby", "PM15QINBXFZ", "ruby mine")
end

it "Creates an instance of Users" do
expect(@users).must_be_kind_of SlackCLI::User
end

it "Return user details correctly" do
expect(@users.details).must_be_kind_of String
expect(@users.username).must_equal "ruby"
expect(@users.slack_id).must_equal "PM15QINBXFZ"
end

it "Returns a list of users" do
VCR.use_cassette "user_list" do
result = SlackCLI::User.list_all
expect(result).must_be_kind_of Array
end
end
end
Loading