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

Ports - Angela and Shamira #3

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ebfcdff
Class setup
MiraMarshall Mar 19, 2019
c8a83ff
set up Channel class
AngelaOh Mar 19, 2019
7fbfc4e
added structure for recipient and workspace classes
MiraMarshall Mar 19, 2019
4a0c8c4
Merge branch 'master' of https://github.com/MiraMarshall/slack-cli
MiraMarshall Mar 19, 2019
62d357d
finializing class structure
MiraMarshall Mar 19, 2019
f319692
created self.list method in User class and corresponding passed tests.
AngelaOh Mar 19, 2019
fdc8b53
added self.list
AngelaOh Mar 19, 2019
b269ec9
added list method for channel class and tests
MiraMarshall Mar 19, 2019
c9c7242
created main method in Slack.rb file
AngelaOh Mar 19, 2019
f1851b1
added workspace class and tests
MiraMarshall Mar 19, 2019
821a745
added test for show_details_user method
AngelaOh Mar 19, 2019
ba5104d
added driver code to slack.rb
AngelaOh Mar 19, 2019
c732249
added slack_api_wrapper class and spec
MiraMarshall Mar 20, 2019
c555943
deleted extra files and added additional option for main class
AngelaOh Mar 20, 2019
6947a93
added until loop in slack.rb
AngelaOh Mar 20, 2019
112a72f
added useful comment
AngelaOh Mar 20, 2019
b8bb13d
adding details functionality
MiraMarshall Mar 20, 2019
3365cec
fixed show_details
AngelaOh Mar 20, 2019
9bc79b5
notes on how to add select methods to send user method
MiraMarshall Mar 21, 2019
c2551d7
fixed sending message to a channel
AngelaOh Mar 21, 2019
e3c6412
fixed our ability to send messages to user and aligned with project r…
AngelaOh Mar 21, 2019
49c24cd
fixed sending message to user
AngelaOh Mar 21, 2019
4a81dcb
Added tests for workspace
MiraMarshall Mar 21, 2019
955e8b1
cleaned up comments
AngelaOh Mar 21, 2019
fd84b3b
added better formatting for options in slack.rb
AngelaOh Mar 22, 2019
777140e
we DRY'd our code and changed Argument Error to custom error
AngelaOh Mar 22, 2019
dbbfdac
erase unneccesary constructor and changed ArgumentError to our custom…
AngelaOh Mar 22, 2019
43a8e26
added tests for recipient-channel and workspace-channel
AngelaOh Mar 22, 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
Binary file added lib/.DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "httparty"
require "dotenv"
require "pry"
Dotenv.load

class Channel

Choose a reason for hiding this comment

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

This class is just one class method. Instead I suggest having a Channel class with instance variables and methods. Your list method could then return an array of Channel instances.

# attr_reader :name, :topic, :member_count, :id
BASE_URL = "https://slack.com/api/channels.list"
SLACK_TOKEN = ENV["SLACK_TOKEN"]

def self.list
query_param = {
token: SLACK_TOKEN,
}
response = HTTParty.get(BASE_URL, query: query_param)

list = []
response["channels"].each do |channel|
channel_info = []
channel_info << channel["name"]
channel_info << channel["topic"]["value"]
channel_info << channel["num_members"]
channel_info << channel["id"]

list << channel_info
end

return list
end
end
62 changes: 62 additions & 0 deletions lib/dont_use_recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "httparty"

Choose a reason for hiding this comment

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

Looks like you got started on this but didn't get finished. I suggest removing it from git.

require "dotenv"
Dotenv.load

class Recipient
attr_reader :slack_id, :name
BASE_URL = "https://slack.com/api/chat.postMessage"
SLACK_TOKEN = ENV["SLACK_TOKEN"]

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

# class SlackApiError < StandardError
# raise SlackApiError, "This id does not exist"
# end

def send_message(message, user: true)
SlackApi::self_message(message, slack_id)
# query_param = {
# token: SLACK_TOKEN,
# channel: slack_id,
# text: message,
# as_user: user,
# }

# response = HTTParty.post(
# "#{BASE_URL}",
# body: {
# token: SLACK_TOKEN,
# text: message,
# channel: slack_id,
# },
# )

# if !response.code == 200 && response.parsed_response["ok"]
# raise SlackApiError, "Oops something went wrong"
# end

# return response.code == 200 && response.parsed_response["ok"]

# if response.code != 200 raise SlackApiError, "This user/channel does not exist"

end

def self.get(url, params)
# SLACK_TOKEN = ENV["SLACK_TOKEN"]
# param = {
# token: SLACK_TOKEN,
# }
# response = HTTParty.get(url, query: param)
end

private

def details
end

def self.list
end
end
65 changes: 65 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "httparty"
require "dotenv"
require_relative "workspace"
require_relative "user"
require_relative "channel"

Dotenv.load

module SlackApi
BASE_URL = "https://slack.com/api/chat.postMessage"
SLACK_TOKEN = ENV["SLACK_TOKEN"]

class SlackApiError < StandardError; end

def self.send_message(message, user)
user_name = User.list.map do |user_list|
user_list[0]
end
# array of all channel names
channel_name = Channel.list.map do |channel|
channel[0]
end

#### TODO: CHANGE VARIABLE NAME
practice = []
User.list.each do |user_name_list|

Choose a reason for hiding this comment

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

Here you end up making the same API call multiple times, this is inefficient. It might be much more useful to have list create an array of User instances with usernames, ids etc.

if user_name_list[0] == user
practice << user_name_list[2]
end
end

test_workspace = Workspace.new

Choose a reason for hiding this comment

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

Why do you create a Workspace in this Recipient class.

if channel_name.include?(user)

Choose a reason for hiding this comment

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

This if-elsif block is a little awkward and could be made much cleaner with duck typing for the User and Channel classes.

response = HTTParty.post(
"#{BASE_URL}",
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
body: {
token: SLACK_TOKEN,
text: message,
channel: test_workspace.select_channel(user), #.select_user(chose_user)
as_user: true,
},
)
elsif user_name.include?(user)
response = HTTParty.post(
"#{BASE_URL}",
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
body: {
token: SLACK_TOKEN,
text: message,
channel: practice[0],
as_user: true,
},
)
else
raise SlackApiError, "The input #{user} is not included in our channels."
end

if response["ok"]
return true
else
raise SlackApiError, "Error when posting #{message} to #{user}, error: #{response["error"]}"
end
end
end
97 changes: 95 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,104 @@
#!/usr/bin/env ruby
require "dotenv"
require_relative "user"
require_relative "channel"
require_relative "workspace"
require_relative "recipient"

def display_options
options = ["1. List Users",
"2. List Channels",
"3. Select User",
"4. Select Channel",
"5. Details",
"6. Send Message",
"7. Quit"]
options.each do |one_option|
puts one_option
end
end

def main
channel_name = Channel.list.map do |channel|
channel[0]
end
user_name = User.list.map do |user|
user[0]
end
puts "Welcome to the Ada Slack CLI!"
puts "We currently have '#{User.list.length}' members and '#{Channel.list.length} channels.'"
puts ""
puts "Please choose one of the the following options"
display_options
selection = gets.chomp.downcase
until selection == "quit"
case selection
when "list users"
puts "Here is the list of users and their details:"
User.list.each do |user|
puts "Username: #{user[0]}"
puts "Real Name: #{user[1]}"
puts "Slack_ID: #{user[2]}"
puts ""
end
puts "What would you like to do next? "
display_options
selection = gets.chomp.downcase
when "list channels"
puts "Here is the list of channels and their details: "
Channel.list.each do |channel|
puts "Name: #{channel[0]}"
puts "Topic: #{channel[1]}"
puts "Member Count: #{channel[2]}"
puts "Slack_ID: #{channel[3]}"
puts ""
end
puts "What would you like to do next? "
display_options
selection = gets.chomp.downcase
when "select user"
puts "Here are all the members' user names: #{user_name}"
chose = gets.chomp.downcase #chose_user
workspace = Workspace.new
details = workspace.select_user(chose)
puts "You just selected #{details}"
puts "What would you like to do next? "
display_options
selection = gets.chomp.downcase
when "select channel"
puts "Here are all the channels' names: #{channel_name}"
chose = gets.chomp.downcase #chose_channel
workspace = Workspace.new
details = workspace.select_channel(chose)
puts "You just selected #{details}"
puts "What would you like to do next? "
display_options
selection = gets.chomp.downcase
when "details"
if details == nil
puts "You must select user or channel first."
else
puts workspace.show_details(details)
end
puts "What would you like to do next? "
selection = gets.chomp.downcase
when "send message"
if details == nil
puts "You must select user or channel first."
else
puts "Please input your message:"
message = gets.chomp.downcase

# TODO project
SlackApi.send_message(message, chose)
end
puts "What would you like to do next? "
display_options
selection = gets.chomp.downcase

Choose a reason for hiding this comment

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

You need to have a default option for this case block, so that you can handle invalid input.

end
end

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

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
# main
27 changes: 27 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "httparty"
require "dotenv"
Dotenv.load

class User
BASE_URL = "https://slack.com/api/users.list"
SLACK_TOKEN = ENV["SLACK_TOKEN"]

Choose a reason for hiding this comment

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

All the comments about the Channel class also apply here.

def self.list
query_param = {
token: SLACK_TOKEN,
}
response = HTTParty.get(BASE_URL, query: query_param)

list = []
response["members"].each do |member|
member_info = []
member_info << member["name"]
member_info << member["real_name"]
member_info << member["id"]

list << member_info
end

return list
end
end
47 changes: 47 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "dotenv"
require "httparty"
require_relative "user"
require_relative "channel"
Dotenv.load

class Workspace
attr_reader :user, :channel
attr_accessor :selected

class SlackApiError < StandardError; end

def select_channel(selected)
selected

Choose a reason for hiding this comment

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

What is the purpose of this method? It takes an argument and just returns the argument?

end

def select_user(selected)
selected
end

def show_details(selected)
user_name = User.list.map do |user|
user[0]
end
channel_name = Channel.list.map do |channel|
channel[0]
end

if channel_name.include?(selected)
Channel.list.each do |instance|
if selected == instance[0]
return instance
end
end
elsif user_name.include?(selected)
User.list.each do |instance|
if selected == instance[0]
return instance
end
end
end

if selected == ""
raise SlackApiError, "No user of channel was selected!"
end
end
end
33 changes: 33 additions & 0 deletions practice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# puts "Welcome to the Ada Slack CLI!"
# puts "We currently have #{User.list.length} members and #{Channel.list.length} channels."

puts ""
puts "Please choose one of the the following number options"
puts "1. List Users"
puts "2. List Channels"
puts "3. Select User"
puts "4. Select Channel"
puts "5. Quit"

selection = gets.chomp

Choose a reason for hiding this comment

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

If this file isn't in your submission, please don't include it in the PR.


loop do
case selection
when "1"
puts "Here is the list of users:"
# puts User.list
when "2"
puts "Here is the list of channels: "
# puts Channel.list
when "3"
chose_user = gets.chomp
workspace = Workspace.new
# puts workspace.show_details_user(chose_user)
when "4"
chose_channel = gets.chomp
workspace = Workspace.new
# puts workspace.show_details_channel(chose_channel)
when "5"
break
end
end
Binary file added specs/.DS_Store
Binary file not shown.
Loading