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

Earth/Taylor/Lina #4

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
bf20c30
testing out env
lina5147 Oct 7, 2020
db5c4c4
workspace class
TaylorMililani Oct 7, 2020
f283b95
built out the prompt for wave 1 for listing users and channels
lina5147 Oct 7, 2020
311416b
made a change of the choice exit to quit
lina5147 Oct 7, 2020
e76755e
removed and added require dotenv and httparty
lina5147 Oct 7, 2020
e37e9c5
created a recipient class
lina5147 Oct 7, 2020
6bba394
created a user class
lina5147 Oct 7, 2020
4dadccc
added details and list all methods for user class
lina5147 Oct 7, 2020
4ca0d9a
channel class created
TaylorMililani Oct 7, 2020
a2b1188
user test file created
TaylorMililani Oct 7, 2020
37c45a0
added a initialization test for user class
lina5147 Oct 7, 2020
2de0975
added a test checking datatype for user class
lina5147 Oct 7, 2020
89a28a4
added from_api helper methods to channel and user class
lina5147 Oct 7, 2020
5088311
working on vcr tests
TaylorMililani Oct 7, 2020
37fb327
added more tests to user test
lina5147 Oct 8, 2020
1b2f6f0
here's this mess
TaylorMililani Oct 8, 2020
df97b73
added error test for invalid token
lina5147 Oct 8, 2020
8bd08bd
channel tests failing
TaylorMililani Oct 8, 2020
43cc026
worked on fixing error messages for channel test
lina5147 Oct 8, 2020
fbcfd96
fixed errors in the user_test file and added a select channel method
lina5147 Oct 8, 2020
0b5e6f5
working on wave 2
TaylorMililani Oct 8, 2020
ca6cecd
Merge branch 'master' of https://github.com/TaylorMililani/slack-cli …
TaylorMililani Oct 8, 2020
6cbcc79
made some changes
TaylorMililani Oct 8, 2020
15bc92f
Added a select_channel method
lina5147 Oct 8, 2020
0351ca2
removed extra 'end' syntax
lina5147 Oct 8, 2020
e32cb06
fixed an error
lina5147 Oct 8, 2020
39bd27a
user find method
TaylorMililani Oct 8, 2020
b566978
command loop working for wave 2
TaylorMililani Oct 8, 2020
7f03131
made changes to the from api method for channel
lina5147 Oct 8, 2020
147f03b
made changes to channel tests and added more tests
lina5147 Oct 8, 2020
0d50faa
added tests for recipient class
lina5147 Oct 8, 2020
980bcd9
made a syntax change
lina5147 Oct 8, 2020
3b5efdf
starting wave 3
TaylorMililani Oct 8, 2020
804f7bf
added tests for Workspace class
lina5147 Oct 8, 2020
95c727f
workspace tests mostly passing
TaylorMililani Oct 8, 2020
ab7f1df
tests failing for send message
TaylorMililani Oct 8, 2020
b8bb0d0
fixed errors in slack.rb and workspace test
lina5147 Oct 8, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

# Ignore environemnt variables
.env


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

class Channel < Recipient
attr_reader :topic, :member_count

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

def details
"Slack ID: #{@slack_id}\nName: #{@name}\nTopic: #{@topic}\nMember Count: #{@member_count}"
end

def self.list_all
url = "https://slack.com/api/conversations.list"
query_params = {token: ENV["SLACK_TOKEN"]}

response = self.get(url, query_params)

channels_list = response["channels"].map do |channel|
self.from_api(channel)
end

return channels_list
end

private

def self.from_api(recipient)

Choose a reason for hiding this comment

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

Clever and clean that this is a separate method and you made it private!

topic = {
"value"=>recipient["topic"]["value"],
"creater"=>recipient["topic"]["creator"],
"last_set"=>recipient["topic"]["last_set"]
}

return new(
slack_id: recipient["id"],
name: recipient["name"],
topic: topic,
member_count: recipient["num_members"]
)
end

end



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

Dotenv.load

class SlackTokenError < StandardError; end

class Recipient

attr_reader :slack_id, :name

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

def self.get(url, params)
response = HTTParty.get(url, query: params)

if response["ok"] != true
raise SlackTokenError, "API call failed error message: #{response["error"]}"
end

return response
end

def details
raise NotImplementedError, 'Implement me in a child class!'
end

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

private

def self.from_api(recipient)
raise NotImplementedError, 'Implement me in a child class!'
end

end


68 changes: 66 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
#!/usr/bin/env ruby
require_relative 'workspace'

require 'table_print'

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

# TODO project
choices = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"]

program_running = true

while program_running
puts "Please choose from the following:"

choices.each_with_index do |choice, i|
puts "#{i + 1}: #{choice}"
end

user_input = gets.chomp.downcase

case user_input
when "list users"
puts ""
tp workspace.users, :slack_id, :name, :real_name, :status_text, :status_emoji
puts ""
when "list channels"
puts ""
tp workspace.channels, :slack_id, :name, :topic, :member_count
puts ""
when "select user"
puts "User name or ID?"
user = gets.chomp
selected_item = workspace.select_user(user)

Choose a reason for hiding this comment

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

Because slack.rb is already doing a lot, it would make the program easier for people to read and understand if the Workspace class had an instance variable had an instance variable to track which Recipient is currently selected instead of tracking it here.

if selected_item == nil
puts "Hmm, no user matches that name or ID"
end
when "select channel"
puts "Name or ID?"
channel = gets.chomp
selected_item = workspace.select_channel(channel)
if selected_item == nil
puts "Hmm, no channel matches that name or ID"
end
when "details"
if selected_item != nil
puts ""
puts selected_item.details
puts ""
else
puts "A channel or user has not been selected to show details"
end
when "send message"

if selected_item != nil
puts "What's your message?"
text = gets.chomp

workspace.send_message(selected_item.name, text)
else
puts "A channel or user has not been selected to send message"
end

when "quit"
program_running = false
else
puts "Invalid choice! Please try again!"
end
end

puts "Thank you for using the Ada Slack CLI"

end

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

class User < Recipient
attr_reader :real_name, :status_text, :status_emoji

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

def details
"Slack ID: #{@slack_id}\nUsername: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}"
end

def self.list_all
url = "https://slack.com/api/users.list"
query_params = {token: ENV["SLACK_TOKEN"]}

response = self.get(url, query_params)

users_list = response["members"].map do |member|
self.from_api(member)
end

return users_list
end

private

def self.from_api(recipient)
return new(
slack_id: recipient["id"],
name: recipient["name"],
real_name: recipient["real_name"],
status_text: recipient["profile"]["status_text"],
status_emoji: recipient["profile"]["status_emoji"]
)
end

end





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

require_relative 'user'
require_relative 'channel'

Dotenv.load

class SlackApiError < StandardError; end

class Workspace

attr_reader :users, :channels

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

def select_user(input)
selected = @users.find { |user| user.slack_id.upcase == input.upcase || user.name.upcase == input.upcase}

return selected

end

def select_channel(input)
selected = @channels.find { |channel| channel.slack_id.upcase == input.upcase || channel.name.upcase == input.upcase}

return selected
end

def send_message(selected_item, text)

Choose a reason for hiding this comment

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

This only seems to work for sending messages to channels but not users. Did y'all realize that as you were building it and testing it out?

url = "https://slack.com/api/chat.postMessage"
response = HTTParty.post(url,
body: {
token: ENV["SLACK_TOKEN"],
text: text,
channel: selected_item
},
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
)
unless response.parsed_response["ok"] == true
raise SlackApiError, "Error: #{response.parsed_response["error"]}"
end

return true
end

end



Loading