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 - Kayla and Mackenzie #15

Open
wants to merge 60 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
6bfc7ff
wave 0
mvlofthus Oct 6, 2020
45892c2
wave 0
mvlofthus Oct 6, 2020
b281d0e
Added Recipient, Channel, and User classes to project
Kaylaj89 Oct 6, 2020
93cbf4f
Deleted duplicate code from test_helper file
Kaylaj89 Oct 6, 2020
307b8c8
Added 'require dotenv' to file
Kaylaj89 Oct 6, 2020
842c464
Added filter to vcr method
Kaylaj89 Oct 6, 2020
b2217b5
Latest update
Kaylaj89 Oct 6, 2020
ff4662c
providing structure for Wave 1
mvlofthus Oct 6, 2020
f2201eb
Kayla's test files for experimenting
Kaylaj89 Oct 7, 2020
7bf2314
scratch file for how to table_print
mvlofthus Oct 7, 2020
361172c
merge?
mvlofthus Oct 7, 2020
d031112
got User to print as a table
mvlofthus Oct 7, 2020
135458b
Delete recipient.rb
mvlofthus Oct 7, 2020
7a314b2
Delete scratch.rb
mvlofthus Oct 7, 2020
9a25e58
Delete user.rb
mvlofthus Oct 7, 2020
a1e3f00
Delete test.rb
mvlofthus Oct 7, 2020
bc83915
User prints, no hardcoded tokens
mvlofthus Oct 7, 2020
46991e7
fixing this
mvlofthus Oct 7, 2020
035321a
re made these files after we panic deleted them, no hardcoded tokens
mvlofthus Oct 7, 2020
0034a2a
fixed typo
mvlofthus Oct 7, 2020
c3fd363
called User.load_all
mvlofthus Oct 7, 2020
10cca2a
made User print table again, got recipient back up to date
mvlofthus Oct 7, 2020
e4984ba
pulls user information correctly
mvlofthus Oct 7, 2020
bb7073b
Added function to the channel.rb
Kaylaj89 Oct 7, 2020
3df54a5
Channel prints and CLI prints channel count
Kaylaj89 Oct 7, 2020
ed32b26
Added case statements and program will now print list users and list …
Kaylaj89 Oct 7, 2020
686116b
refactored .list
mvlofthus Oct 7, 2020
6788196
Select User, Selected Channel, Details all added to CLI
Kaylaj89 Oct 8, 2020
20a3c13
Added select user, select channel, and details options to cli
Kaylaj89 Oct 8, 2020
5308f94
find_recipient method to print details
mvlofthus Oct 8, 2020
ffadce4
to interface with new details methods
mvlofthus Oct 8, 2020
853a12b
Added new username instantiation
Kaylaj89 Oct 8, 2020
a7ac6f2
Added changes to slack select user and select channel options
Kaylaj89 Oct 8, 2020
659d6fc
Added methods to deal with selected users and channels
Kaylaj89 Oct 8, 2020
a257b60
cleaning up helper methods in workspace
mvlofthus Oct 8, 2020
7cd655c
Added required relatives to test_helper file and added a describe cal…
Kaylaj89 Oct 8, 2020
0412387
Made typo change to test_helper.rb
Kaylaj89 Oct 8, 2020
f40d1ec
Not sure what this change is
Kaylaj89 Oct 8, 2020
4383d44
pulled details into child classes
mvlofthus Oct 8, 2020
6509699
pulled details into child classes
mvlofthus Oct 8, 2020
e4c732f
added channel details
Kaylaj89 Oct 8, 2020
9fbca53
Added 'send message' methods to channel, recipient, and slack classe
Kaylaj89 Oct 8, 2020
47b6075
Changed naem
Kaylaj89 Oct 8, 2020
3e7bf22
Added send_msg method to recipient w/url
Kaylaj89 Oct 8, 2020
5c13f20
Renamed file so it works--and added some required elements
Kaylaj89 Oct 8, 2020
65a03c9
started adding send message to slack.rb
Kaylaj89 Oct 8, 2020
89389bd
Send message works
mvlofthus Oct 8, 2020
e457e14
formatting and comments
mvlofthus Oct 8, 2020
57d3fbc
Added test for channel
Kaylaj89 Oct 8, 2020
c5086b3
Merge branch 'master' of https://github.com/mvlofthus/slack-cli into …
Kaylaj89 Oct 8, 2020
02d2307
using .get in recipient class appropriately, change was overwritten
mvlofthus Oct 8, 2020
6d8a0cf
Added recipient test file and edited user_test
Kaylaj89 Oct 8, 2020
2a694ec
VCR finally works and a few tests were written
mvlofthus Oct 9, 2020
1220e25
made more working tests - user instantiation, recipient messages
mvlofthus Oct 9, 2020
776c135
Added more tests to test files
Kaylaj89 Oct 9, 2020
23bfc2b
Tests now failing for details sections of user and channel. This is b…
Kaylaj89 Oct 9, 2020
83bfb59
Renamed user test username to slackbot and changed channel test usern…
Kaylaj89 Oct 9, 2020
6aca832
Added a 'Describe do' block for details section of channel
Kaylaj89 Oct 9, 2020
440c366
Added test to make sure Argument Error is raised when bogus Token is …
Kaylaj89 Oct 9, 2020
e02e66b
added remaining method tests in workspace, adjusted methods so tests …
mvlofthus Oct 9, 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
42 changes: 42 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'httparty'
require 'dotenv'
require 'table_print'

require_relative 'recipient'

Dotenv.load

class Channel < Recipient

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

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

def self.list
response = self.get("https://slack.com/api/conversations.list")

channel_array = []

response["channels"].each do |channel|
slack_id = channel["id"]
name = channel["name"]
purpose = channel["purpose"]["value"]
member_count = channel["num_members"]

temp_channel = self.new(slack_id, name, purpose, member_count)
channel_array << temp_channel
end

return channel_array
end

def details
return "Channel found, here's the scoop!\n Slack ID: #{slack_id}\n Name: #{name}\n Topic: #{topic}\n Member Count: #{member_count}"
end

end

54 changes: 54 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'httparty'
require 'dotenv'

Dotenv.load

class Recipient
attr_reader :slack_id, :name

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

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

unless response.code == 200 && response.parsed_response["ok"]

Choose a reason for hiding this comment

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

Good work error handling.

raise ArgumentError, "SlackApiError. Reason: #{response["error"]}"
end

return response
end

def self.send_msg(message, id)

Choose a reason for hiding this comment

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

Consider making send_msg an instance method that's called on an instance of a User or Channel. In this way, id does not need to be an argument.

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

unless response.code == 200 && response.parsed_response["ok"]

Choose a reason for hiding this comment

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

Nice work error handling with a custom exception.

raise ArgumentError, "SlackApiError. Reason: #{response["error"]}"
end

return response
end


private

def self.list
raise NotImplementedError, 'Implement me in a child class'
end

def self.details
raise NotImplementedError, 'Implement me in a child class'
end
end
51 changes: 49 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
#!/usr/bin/env ruby
require 'table_print'
require_relative 'workspace'

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

# TODO project
puts "There are #{workspace.channels.count} channels in this workspace"
puts "There are #{workspace.users.count} users in this workspace."
puts
puts "*" * 40
Comment on lines -4 to +12

Choose a reason for hiding this comment

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

Consider wrapping this set-up functionality into its own method.


user_input = nil

until user_input == "7" || user_input == "quit"
puts options
user_input = gets.chomp.downcase

case user_input
when "1", "list users"
workspace.list_users
when "2", "list channels"
workspace.list_channels
when "3", "select user"
puts "Please enter User Name or ID"
user_identifier = gets.chomp.downcase
workspace.select_user(user_identifier)
when "4", "select channel"
puts "Please enter Chanel Name or ID"
channel_identifier = gets.chomp.downcase
workspace.select_channel(channel_identifier)
when "5", "details"
if workspace.selected_recipient == nil
puts "Please select a channel or user first to view details"
else
puts workspace.find_details
end
when "6", "send message"
if workspace.selected_recipient == nil
puts "Please select a channel or user first to send message"
else
puts "Send a message!"
message = gets.chomp
workspace.send_msg(message)
puts "\nMessage sent!"
end
end
end
puts "Thank you for using the Ada Slack CLI"
end
Comment on lines +16 to 51

Choose a reason for hiding this comment

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

Consider wrapping this functionality into its own method.


def options
puts "\nWhat would you like to do?\n"
puts "Choose the number that corresponds to the following options:"
puts "[1]'List Users'\n[2]'List Channels'\n[3]'Select User'\n[4]'Select Channel'\n[5]'Details'\n[6]'Send Message'\n[7]'Quit'"
end

main if __FILE__ == $PROGRAM_NAME
41 changes: 41 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'httparty'
require 'dotenv'
require 'table_print'

require_relative 'recipient'

Dotenv.load

class User < Recipient

attr_reader :slack_id, :username, :real_name, :status_emoji

def initialize (slack_id, username, real_name, status_emoji, name)
super(slack_id, name)
@real_name = real_name
@status_emoji = status_emoji
@username = username #for table print
end

def self.list
response = self.get("https://slack.com/api/users.list")

user_array = []
response["members"].each do |user|
slack_id = user["id"]
username = user["name"] #for table print
real_name = user["profile"]["real_name"]
status_emoji = user["profile"]["status_emoji"]
name = user["name"]

temp_user = self.new(slack_id, username, real_name, status_emoji, name)
user_array << temp_user
end
return user_array
end

def details
return "User found, here's the scoop!\n Username: #{name} \n Slack ID: #{slack_id}\n Real Name: #{real_name}\n Status Emoji: #{status_emoji}"
end

end
57 changes: 57 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'dotenv'

require_relative 'user'
require_relative 'channel'

Dotenv.load

class Workspace
attr_reader :users, :channels
attr_accessor :selected_recipient

def initialize
@users = User.list
@channels = Channel.list
@selected_recipient = nil
end

def list_users
return tp @users, "slack_id", "username", "real_name"
end

def list_channels
return tp @channels, "name", "topic", "member_count", "slack_id"
end

def select_user(id)
@users.each do |user|
if user.slack_id.downcase == id || user.username.downcase == id
@selected_recipient = user
puts "Valid user selected"
return @selected_recipient
end
end
puts "User does not exist"
end

def select_channel(id)
@channels.each do |channel|
if channel.slack_id.downcase == id || channel.name.downcase == id
@selected_recipient = channel
puts "Valid channel selected"
return @selected_recipient
end
end
puts "Channel does not exist"
end

def find_details
return @selected_recipient.details
end

def send_msg(message)
Recipient.send_msg(message, @selected_recipient.slack_id)
end

end

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

describe Channel do
before do
slack_id = "111"
name = "random"
topic = "random"
member_count = "79"
@channel= Channel.new(slack_id, name, topic, member_count)
@detail = @channel.details
@slack_id = slack_id
@name = name
@topic = topic
@member_count = member_count

VCR.use_cassette("random_channel") do
@response = Channel.list
end
end

it 'is an instance of Channel' do
expect(@channel).must_be_kind_of Channel
end

it 'channel.list is an instance of an array' do
expect(@response).must_be_kind_of Array
expect(@response.length).must_be_kind_of Integer
expect(@response.length).must_be_close_to 49 #passes at time of submission
end

it 'returns the correct info for members' do
expect _(@response[1].slack_id).must_equal "C0165NC8LHH"
expect _(@response[3].name).must_equal "csmemes"
end

describe 'channel details' do

it 'returns a description of a selected channel' do
expect(@detail).must_equal "Channel found, here's the scoop!\n Slack ID: #{@slack_id}\n Name: #{@name}\n Topic: #{@topic}\n Member Count: #{@member_count}"
end

it 'description returns a string' do
expect(@detail).must_be_kind_of String
end
end
end
67 changes: 67 additions & 0 deletions test/recipient_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require_relative 'test_helper'
require 'httparty'

describe Recipient do
before do
@slack_id = "U0179312KV3"
@name = "kayla.elizabeth89"
@recipient = Recipient.new(@slack_id, @name)
end

describe "instantiation" do
it "creates a new Recipient instance" do
expect(@recipient).must_be_instance_of Recipient
end
end

it "can send a valid message" do
VCR.use_cassette("messages take 4") do
response = Recipient.send_msg("I can post messages!", "U0179312KV3")
expect(response.code).must_equal 200
end
end

it "can send a valid complicated message" do
VCR.use_cassette("messages take 4") do
response = Recipient.send_msg("Kayla! I'm testing a longer message with emojis :party_blob: :confused-dog: I'm sorry if this comes through to you multiple times but if it does I at least hope you are able to enjoy the emojis being sent through our bot!! :gem: :unicorn_face: :zap: **FIST BUMP** :right-facing_fist::left-facing_fist: ", "U0179312KV3")
expect(response.code).must_equal 200
end
end

it "will raise error for invalid address" do
VCR.use_cassette("messages take 4") do
expect{Recipient.send_msg("I can post messages!", "mvlofthus")}.must_raise ArgumentError
end
end

it "will raise error for empty message" do
VCR.use_cassette("messages take 4") do
expect{Recipient.send_msg("", "U0179312KV3")}.must_raise ArgumentError
end
end

describe 'private methods' do

it "will raise Argument error if Recipient.list is called" do
VCR.use_cassette("messages take 4") do
expect{Recipient.list}.must_raise NotImplementedError
end
end

it "will raise Argument error if Recipient.details is called" do
VCR.use_cassette("messages take 4") do
expect{Recipient.details}.must_raise NotImplementedError
end
end

describe 'failed SlackAPI request/post' do

it "Will raise an Argument error if Token is bogus" do #this will work if you give a bogus token in .env
VCR.use_cassette("SlackAPI failed") do
expect{Recipient.get("https://slack.com/api/users.list")}.must_raise ArgumentError
end
end
end
end
end

Loading