Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
844f7cc
setup environment, started wave 1
YasminM11 Sep 10, 2019
36f83be
worked on User testing
YasminM11 Sep 10, 2019
fb95aff
added some tests to the user_test.rb, test passed
YasminM11 Sep 11, 2019
afa19e5
worked on pushing together changes
brilatimer Sep 11, 2019
10ba16b
added cassette to test_helper, 1 test passed
YasminM11 Sep 11, 2019
f70e150
hid cassette
brilatimer Sep 11, 2019
5537f71
debugged channels_list, wrote channels tests, worked on wave 1
brilatimer Sep 12, 2019
e6741cb
target errors
brilatimer Sep 12, 2019
6ef2979
psuedocode wave 2 setup
brilatimer Sep 12, 2019
f4ee658
wave 2 logic steps
brilatimer Sep 12, 2019
9cdf5f4
created method for details: user and channel and tests
YasminM11 Sep 12, 2019
ae5e8a9
finalized tests
YasminM11 Sep 12, 2019
c044b57
Ignore yml files
brilatimer Sep 13, 2019
564875a
cleaned up while loop in slack.rb
brilatimer Sep 13, 2019
4d49679
list channels selection working
brilatimer Sep 13, 2019
38a00ad
select user option added
brilatimer Sep 13, 2019
18ba315
select channel method complete and if no recepient match
brilatimer Sep 13, 2019
11ecd2e
started tests for send message
brilatimer Sep 13, 2019
f5300ff
comments
brilatimer Sep 13, 2019
c89eb26
wrote test for channel and user count
brilatimer Sep 13, 2019
bdc4aa4
deleted workspace class and test space
brilatimer Sep 13, 2019
84ca629
spelled recipient correctly, updated in files and file name
brilatimer Sep 13, 2019
8647477
created send message in Recipient class
brilatimer Sep 13, 2019
e535650
completed send_message logic in slack.rb
brilatimer Sep 13, 2019
472368b
added user:read:write from slack doc to permissions on site, added as…
brilatimer Sep 16, 2019
44677bd
send_message in channel.rb working
brilatimer Sep 16, 2019
976c2a6
tests written for invalid selection for user and channels
brilatimer Sep 16, 2019
652bbbb
send_message test written
brilatimer Sep 16, 2019
4011d11
wrote send_message test for user_testand 100% simplecov test coverage…
brilatimer Sep 16, 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: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.gem
*.rbc
*.yml
/.config
/coverage/

Choose a reason for hiding this comment

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

this should be coverage/

/InstalledFiles
Expand Down Expand Up @@ -52,5 +53,3 @@ build-iPhoneSimulator/
# Ignore environemnt variables
.env

# Ignore cassette files
/specs/cassettes/
66 changes: 66 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require_relative 'recipient'
require 'dotenv'
Dotenv.load

module Slack
class Channel
attr_reader :channel_name, :topic, :member_count, :slack_id
CHANNELS_LIST = "https://slack.com/api/channels.list"
CHAT_URL = "https://slack.com/api/chat.postMessage"

def initialize(channel_name, topic, member_count, slack_id)
@channel_name = channel_name
@topic = topic
@member_count = member_count
@slack_id = slack_id
end

def self.list_channels
query = {
token: ENV["SLACK_API_TOKEN"]
}
response = HTTParty.get(CHANNELS_LIST, query: query)

Choose a reason for hiding this comment

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

This is a key place where a parent class that has a self.get(url, params) method could help you make sure this functionality works the same in these 2 classes.

channels = response["channels"]
channels_list = []
channels.each do |channel_hash|
channel_name = channel_hash["name"]
topic = channel_hash["topic"]
member_count = channel_hash["num_members"]
slack_id = channel_hash["id"]
channels_list << Channel.new(channel_name, topic, member_count, slack_id)
end
return channels_list
end

def details
"The channel name is #{channel_name} and the slack id is #{slack_id}"
end

# method for select channel, returns an instance of channel
# input is a string
def self.select_channel(recipient_selection)
channels = list_channels
recipient = channels.find {| channel | channel.channel_name == recipient_selection || channel.slack_id == recipient_selection }
return recipient
end

# method for send_message - just like in User.rb
def send_message(message)

Choose a reason for hiding this comment

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

You should raise an exception here if the post request is not successful. It is best practice to raise a custom exception such as APIError that inherits from StandardError.

response = HTTParty.post(
CHAT_URL,
body: {
token: ENV["SLACK_API_TOKEN"],
text: message,
channel: @slack_id # need slack_id, per Slack documentation for send_message
},
)

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





16 changes: 16 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
Dotenv.load('../.env')
require 'awesome_print'

CHAT_URL = "https://slack.com/api/chat.postMessage"

module Slack
class Recipient

Choose a reason for hiding this comment

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

As you mention in your comprehension questions. You could make good use of inheritance by having User and Channel inherit from Recipient.

attr_reader :slack_id, :name

end
end


78 changes: 74 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require 'awesome_print'
require_relative './user'
require_relative './channel'
Dotenv.load('../.env')

KEY = ENV['KEY']

def main
recipient = nil
users = Slack::User.list_users
channels = Slack::Channel.list_channels

# Wave 1
puts "Welcome to the Ada Slack CLI!"

# TODO project

puts "There are #{users.length} users and #{channels.length} channels."

# Loop
while true
puts "Would you like to view: List Users, List Channels, Select User, Select Channel, See Details, Send Message, or Quit?"
user_input = gets.chomp.split.map(&:capitalize).join(' ')
case user_input
when "List Users"
users.each do |user|
puts "User count: #{users.length}, User Name: #{user.user_name}, Real name: #{user.name}, and Slack ID: #{user.slack_id}."
end
when "List Channels"
channels.each do |channel|
puts "Channel's name: #{channel.channel_name}, Member Count: #{channel.member_count}, and Slack ID: #{channel.slack_id}."
end

# Wave 2: Selection Method
when "Select User"
puts "Please enter a username or slack id to continue."
recipient_selection = gets.chomp
recipient = Slack::User.select_user(recipient_selection)
if recipient == nil
puts "No username or slack id found. Returning you to main menu."
end

when "Select Channel"
puts "Please enter a channel name or slack id to continue."
recipient_selection = gets.chomp
recipient = Slack::Channel.select_channel(recipient_selection)
if recipient == nil
puts "No channel name or slack id found. Returning you to main menu."
end

when "See Details"
if recipient == nil # if no recipient is selected, no details can be shown
puts "No recipient currently selected. Returning you to main menu."
else
puts recipient.details # if recipient selected, show details
end

# Wave 3: Send Message to selected user. Send_message method works for User and Channel
when "Send Message"
puts "Please enter your new message now."
message = gets.chomp
if recipient == nil
puts "No recipient currently selected. Returning you to main menu."
else
recipient.send_message(message)
end

when "Quit"
puts "Exiting program"
quit
else
puts "Incorrect input."
end
end

# Program ends and exits
puts "Thank you for using the Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME

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

module Slack
class User
attr_reader :user_name, :slack_id, :name

USERS_LIST = "https://slack.com/api/users.list"
CHAT_URL = "https://slack.com/api/chat.postMessage"

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

def self.list_users
query = {
token: ENV["SLACK_API_TOKEN"]
}
response = HTTParty.get(USERS_LIST, query: query)
members = response["members"]

users = []
members.each do |user_hash|
slack_id = user_hash["id"]
name = user_hash["real_name"]
user_name = user_hash["name"]
users << User.new(user_name, name, slack_id)
end
return users
end

def details
"The name is #{@name}, the user name is #{@user_name}, and the slack id is #{@slack_id}"
end

def self.select_user(recipient_selection)
users = list_users
recipient = users.find { |user| user.user_name == recipient_selection || user.slack_id == recipient_selection }
return recipient
end

def send_message(message)
response = HTTParty.post(
CHAT_URL,
body: {
token: ENV["SLACK_API_TOKEN"],
text: message,
channel: @user_name,
as_user: true # allows you to pass in a user_name
},
)

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

80 changes: 80 additions & 0 deletions test/cassettes/Channel_details.yml

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

Loading