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 - Heather and Mina #12

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
904bd3f
testing slack
minams Mar 18, 2019
0afb27b
Updated Slack API test
piffer59 Mar 19, 2019
49e3afa
Updated test_helper file for VCR
piffer59 Mar 19, 2019
f6b7471
added Recipient parent class and initialized
minams Mar 19, 2019
7c9d55b
changed git file
minams Mar 19, 2019
37897a0
made some changes to initialize
minams Mar 19, 2019
b8a1fa8
Channel class self.get method
piffer59 Mar 19, 2019
8115f60
added User class and initial tests
minams Mar 20, 2019
8e47965
created self.list_all method in user class
minams Mar 20, 2019
7e223d5
made minor name changes
minams Mar 20, 2019
da766d5
channel list_all method and test
minams Mar 20, 2019
e611d21
changes in recipient file
minams Mar 20, 2019
573b950
channel list method in progress
piffer59 Mar 20, 2019
f277f55
added channel info to list_all method
minams Mar 20, 2019
389301d
updated list_all methods
piffer59 Mar 20, 2019
2096753
Accessedlist_all methods via Slack.rb file
piffer59 Mar 21, 2019
0be2280
Added workspace class
piffer59 Mar 21, 2019
e8ed772
added methods to Workspace for details about selected classes
minams Mar 21, 2019
8cbafb2
Added select_channel and select_user methods to workspace class with …
piffer59 Mar 21, 2019
a9aaaa6
added UI to slack.rb
minams Mar 21, 2019
41c015d
refactored some of CLI and workspace chanels output
minams Mar 22, 2019
7c51117
updated tests for list_all methods for channel and user classes
piffer59 Mar 22, 2019
f3436e8
Merge branch 'master' of https://github.com/minams/slack-cli
piffer59 Mar 22, 2019
a7a0cc6
added message methods to slack.rb for now
minams Mar 22, 2019
b63fb6c
Added tests for workspace, working on method to list channel details
piffer59 Mar 22, 2019
9039c1e
working on selected channel details
minams Mar 22, 2019
f4ee723
made minor syntax changes
minams Mar 22, 2019
8e924df
made changes to selected_channel_details method in workspace
minams Mar 22, 2019
3764283
Finished list channel/user details method and CLI, added test for lis…
piffer59 Mar 22, 2019
f58feb9
added tests for workspace, moved post message method to recipient class
piffer59 Mar 22, 2019
acd65e2
completed slack prompt for details
minams Mar 22, 2019
8249ccd
Started message method in workspace
piffer59 Mar 22, 2019
ad6efe6
added send message feature in slack.rb
minams Mar 22, 2019
4ecb3fb
added 2 when to selection case
minams Mar 22, 2019
b14f42d
refactor
minams Mar 22, 2019
5a0cd37
removed ap and re-added method in workspace.rb and spec
minams Mar 25, 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 .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ build-iPhoneSimulator/

# Ignore cassette files
/specs/cassettes/

.DS_Store
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Local File",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${file}"
}
]
}

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

module Slack
class Channel < Recipient
attr_reader :slack_id, :name, :num_members, :topic

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

def self.get

Choose a reason for hiding this comment

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

You could implement this method in Recipient and inherit the behavior.

url = "https://slack.com/api/channels.list"
params = {
token: ENV["KEY"],
}

response_from_get = HTTParty.get(url, query: params)

if response_from_get["ok"] == false
raise ArgumentError, "Request is unsuccessful"
else
return response_from_get
end
end

def self.list_all
all_channels = Channel.get["channels"].map do |channel|
self.new(channel["id"], channel["name"], channel["num_members"], channel["topic"]["value"])
end
return all_channels
end
end
end
34 changes: 34 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Slack
class Recipient
attr_reader :slack_id, :name

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

def post_message(message)
url = "https://slack.com/api/chat.postMessage"
params = {
token: ENV["KEY"],
channel: @slack_id || @name,
text: message,
}

message_request = HTTParty.post(url, query: params)
if message_request["ok"] == false
raise ArgumentError, "Request is unsuccessful"

Choose a reason for hiding this comment

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

You should also include the error message.

else
return message_request
end
end

# def self.get(url, params)
# HTTParty.GET(url, query: params)
# end

def self.list_all
raise NotImplementedError, "Please implement in my child."
end
end
end
89 changes: 87 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@
#!/usr/bin/env ruby
# require_relative "../spec/test_helper"
require_relative "channel"
require_relative "user"
require_relative "recipient"
require_relative "workspace"
require "dotenv"
require "httparty"
Dotenv.load

def main
puts "Welcome to the Ada Slack CLI!"

# TODO project
print "Channels loaded: ", Slack::Workspace.channel_list_all.length

print "\nUsers loaded: ", Slack::Workspace.user_list_all.length

puts "\nPlease select from the following options: \n list channels, select channel, list users, select user, details, send message, or quit"
selection = gets.chomp.downcase

until selection == "quit"
case selection
when "list channels"
puts Slack::Workspace.all_channels_details
when "select channel"

Choose a reason for hiding this comment

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

The way you have this here means the channel doesn't stay selected. It also means you have a lot of duplicate code in selecting users and channels and sending messages etc.

puts "Which channel would you like to select?"
channel_identifier = gets.chomp.to_s
selected_channel = Slack::Workspace.select_channel(channel_identifier)
if selected_channel.nil?
puts "This channel doesn't exist. Exiting to main menu"
else
puts "You have selected #{selected_channel.name}. What would you like to do: details, send message, or quit"
answer = gets.chomp.downcase
case answer
when "details"
puts Slack::Workspace.selected_channel_details(selected_channel)
when "send message"
puts "Type message: "
message = gets.chomp
if message == ""
puts "Can't send a blank message. \nType message :"
message = gets.chomp
else
Slack::Recipient.new(selected_channel.slack_id, selected_channel.name).post_message(message)
end
when "quit"
else
puts "Please select a valid input. Returning to main!"
end
end
when "list users"
puts Slack::Workspace.all_users_details
when "select user"
puts "Which user would you like to select?"
user_identifier = gets.chomp.to_s
selected_user = Slack::Workspace.select_user(user_identifier)
if selected_user.nil?
puts "This user doesn't exist. Exiting to main menu"
else
puts "You have selected #{selected_user.name}. What would you like to do: details, send message, or quit"
answer = gets.chomp.downcase
case answer
when "details"
puts Slack::Workspace.selected_user_details(selected_user)
when "send message"
puts "Type message: "
message = gets.chomp
if message == ""
puts "Can't send a blank message. \nType message :"
message = gets.chomp
else
Slack::Recipient.new(selected_user.slack_id, selected_user.name).post_message(message)
end
when "quit"
else
puts "Please select a valid input. Returning to main!"
end
end
when "details"
puts "You haven't selected a user or channel. Exiting to main prompt."
when "send message"
puts "You haven't selected a user or channel. Exiting to main prompt."
when "quit"
else
puts "Please enter a valid selection."
end
puts "What would you like to do next? (list users, select user, list channels, select channel, details, send message, or quit)"
selection = gets.chomp.downcase
end

# Should we use a case statement here for the 3 options?

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

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

module Slack
class User < Recipient
attr_reader :slack_id, :name, :real_name

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

def self.get
url = "https://slack.com/api/users.list"
params = {
token: ENV["KEY"],

Choose a reason for hiding this comment

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

In general don't name your environmental variable "KEY", make the name a bit more specific.

Also both the token and the URL would make great constants.

}

response_from_get = HTTParty.get(url, query: params)

return response_from_get
end

def self.list_all
users = User.get["members"].map do |user|
self.new(user["id"], user["name"], user["profile"]["real_name"])
end
return users
end
end
end
69 changes: 69 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative "channel"
require_relative "user"
require_relative "recipient"
require "dotenv"
require "httparty"
require "pry"
Dotenv.load

module Slack
class Workspace

Choose a reason for hiding this comment

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

This works as a collection of class methods, but if you're using all class methods, you might as well just make Workspace a module. Instead I would suggest making Workspace a class with the attributes @users, @channels and @selected, which means you could keep track of the selected user or channel in your main app.

def self.channel_list_all
channels = Slack::Channel.list_all
name_channels = channels.map do |channel|
channel.name
end
return name_channels
end

def self.all_channels_details
channels = Slack::Channel.list_all.map do |channel|
"Channel ID: #{channel.slack_id}\nChannel name: #{channel.name} \nMembers: #{channel.num_members} \nChannel Topic: #{channel.topic}"
end
return channels
end

def self.select_channel(channel_identifier)
find_channel = Slack::Channel.list_all.find do |channel|
channel.name.downcase == channel_identifier.downcase ||
channel.slack_id.downcase == channel_identifier.downcase
end
return find_channel
end

def self.selected_channel_details(chosen_channel)
details = "Channel ID: #{chosen_channel.slack_id}\nChannel name: #{chosen_channel.name} \nMembers: #{chosen_channel.num_members} \nChannel Topic: #{chosen_channel.topic}"

return details
end

def self.user_list_all
users = Slack::User.list_all
name_users = users.map do |user|
user.name
end
return name_users
end

def self.all_users_details
users = Slack::User.list_all.map do |user|
"User ID: #{user.slack_id}: \nReal name: #{user.real_name} \nDisplay name: #{user.name}."
end
return users
end

def self.select_user(user_identifier)
find_user = Slack::User.list_all.find {
|user|
user.name.downcase == user_identifier.downcase ||
user.slack_id.downcase == user_identifier.downcase
}

return find_user
end

def self.selected_user_details(chosen_user)
details = "User ID: #{chosen_user.slack_id}: \nReal name: #{chosen_user.real_name} \nDisplay name: #{chosen_user.name}."
end
end
end
32 changes: 32 additions & 0 deletions specs/channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative "test_helper.rb"

require "pry"

describe "Channel class" do
it "Creates an instance of the Channel class" do
new_channel = Slack::Channel.new("id", "name", 3, "topic")
expect(new_channel).must_be_instance_of Slack::Channel
end
end

describe "Get method" do
it "Successfully GETs response from Slack API" do
VCR.use_cassette("channel_find") do
response = Slack::Channel.get

expect(response["ok"]).must_equal true
end
end
end

describe "list_all method" do
it "gives a list of all user information from the API and returns a value of a 'general' channel name" do
VCR.use_cassette("channel_find") do
list_response = Slack::Channel.list_all
ap list_response

expect(list_response.length).must_equal 3
expect(list_response[0].name).must_equal "general"
end
end
end
24 changes: 24 additions & 0 deletions specs/recipient_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative "test_helper.rb"
require "pry"

describe "Recipient class" do
it "creates an instance of Recipient class" do
receiver = Slack::Recipient.new("id", "name")
expect(receiver).must_be_instance_of Slack::Recipient
end
end

Choose a reason for hiding this comment

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

You should also test that your abstract method raises an error.

describe "Post message method" do
it "will return a true value for 'ok'" do
VCR.use_cassette("message_find") do
recipient_id = "USLACKBOT"
name = "slackbot"

message = "Hi Slackbot! - Heather and Mina"

new_message = Slack::Recipient.new(recipient_id, name).post_message(message)
expect(new_message["ok"]).must_equal true
expect(new_message["message"]["text"]).must_match "- Heather and Mina"
end
end
end
1 change: 1 addition & 0 deletions specs/slack_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Choose a reason for hiding this comment

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

??

Loading