Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
130b750
set up files and write channel list method
janicewhuang Sep 10, 2019
cf982c8
write channel tests, all tests passing.
janicewhuang Sep 10, 2019
83d3e9f
added gitignore, setup test
emilyvomacka Sep 11, 2019
09b1a4b
edit setup
emilyvomacka Sep 11, 2019
0d491c3
set-up actually added
emilyvomacka Sep 11, 2019
1028f18
add parse function to user
janicewhuang Sep 11, 2019
7e51cfd
added user tests which are failing
emilyvomacka Sep 11, 2019
b728981
thought I did this?
emilyvomacka Sep 11, 2019
ad40cdc
added tests for user and workspace
emilyvomacka Sep 11, 2019
3db3b79
add user.get in workspace
janicewhuang Sep 11, 2019
e8936de
fixed conflict in user, whoa git is nuts
emilyvomacka Sep 11, 2019
3e0f1a4
added list_users and list_channels, added command line
emilyvomacka Sep 11, 2019
0d476c0
finished command line to complete wave 1
emilyvomacka Sep 12, 2019
ce610fe
add select user and select channel functions
janicewhuang Sep 12, 2019
086ea9e
add functionality
janicewhuang Sep 12, 2019
f58d9cf
get full menu up, functions mostly working
janicewhuang Sep 12, 2019
f168335
fixed loop in slack.rb
emilyvomacka Sep 12, 2019
d7799f6
wave 2 is toast
emilyvomacka Sep 12, 2019
85996b0
wave three done.
emilyvomacka Sep 12, 2019
9a88e79
make select_user and select_channel methods in workspace.rb to move f…
janicewhuang Sep 13, 2019
e4169c4
refactor methods' responses to invalid input. in the process of refac…
janicewhuang Sep 13, 2019
c48294f
refactor selection methods
janicewhuang Sep 13, 2019
81a787c
add single quotes for parameters
janicewhuang Sep 13, 2019
1c0620d
light refactor
emilyvomacka Sep 13, 2019
da46702
merged changes from thurs night
emilyvomacka Sep 13, 2019
58805c1
change tests to look for nil instead of 'invalid' string because move…
janicewhuang Sep 13, 2019
7c725b6
added a few tests
emilyvomacka Sep 13, 2019
d5f6628
another commit
emilyvomacka Sep 13, 2019
56cae3a
add helper method for invalid input
emilyvomacka Sep 13, 2019
435efba
2:39, all tests passing
emilyvomacka Sep 13, 2019
0ca4685
made finishing touches
janicewhuang Sep 13, 2019
f500726
final commit, final final:
emilyvomacka Sep 13, 2019
6a7186c
merged changes
emilyvomacka Sep 13, 2019
b414036
added recipient get error msg
emilyvomacka Sep 13, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/tmp/

# Used by dotenv library to load environment variables.
# .env
.env

## Specific to RubyMotion:
.dat*
Expand Down
27 changes: 27 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'httparty'
require 'dotenv'
require_relative 'recipient'

Dotenv.load

module SlackCLI
class Channel < Recipient
attr_reader :slack_id, :name, :topic, :member_count

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

#factory method for producing individual channels from json
def self.json_parse(json)
channels = []
json["channels"].each do|channel|
new_ch = SlackCLI::Channel.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"])
channels << new_ch
end
return channels
end
end
end
38 changes: 38 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'httparty'
require 'dotenv'

Dotenv.load

module SlackCLI
class Recipient
attr_reader :slack_id, :name

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

def send_message(msg)
post_parameters = {
token: ENV['SLACK_KEY'],
channel: self.slack_id,
text: msg
}
response = HTTParty.post('https://slack.com/api/chat.postMessage', query: post_parameters)
if response.code == 200
puts "Message sent successfully."
else
puts "Message not sent, error code #{response.code}."
end
return response
end

def self.get(url, query)
response = HTTParty.get(url, query)
if response.code != 200
puts "Something went wrong, error code #{response.code}."
end
return response
end
end
end
15 changes: 15 additions & 0 deletions lib/setup_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'httparty'
require 'dotenv'
Dotenv.load

url = "https://slack.com/api/channels.list?"

query_parameters = {
token: ENV['SLACK_TOKEN']
}

response = HTTParty.get(url, query: query_parameters)

response["channels"].each do |channel|
puts "This channel is called #{channel["name"]} and has #{channel["num_members"]} members."
end
79 changes: 74 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,80 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require_relative 'user'
require_relative 'channel'
require_relative 'workspace'

def main
puts "Welcome to the Ada Slack CLI!"
Dotenv.load

def invalid_input_check(input)
if input == nil
print "\nSorry. Invalid selection."
return true
else
puts "\n#{input.name} has been selected."
end
end

# TODO project
def selected_is_nil(selected)
if selected == nil
puts "\nNo user or channel selected."
return true
end
end

puts "Thank you for using the Ada Slack CLI"
def main
puts "Welcome to the Ada Slack CLI!"
tsu = SlackCLI::Workspace.new

loop do
print "\nWhat would you like to do?
- list users
- list channels
- select user
- select channel
- show details
- send message
- quit
\nYour choice: "
choice = gets.chomp.downcase
until ["list users", "list channels", "select user", "select channel", "show details", "send message", "quit"].include?(choice)
print "\nSorry. Please enter a valid choice. "
choice = gets.chomp.downcase
end

case choice
when "list users"
tp tsu.list_users
when "list channels"
tp tsu.list_channels
when "select user"
print "Please select a user (by Slack ID or Display Name): "
user_chosen = gets.chomp
tsu.selected = tsu.select_user(user_chosen)
invalid_input_check(tsu.selected)
when "select channel"
print "Please select a channel (by Slack ID or Name): "
channel_chosen = gets.chomp
tsu.selected = tsu.select_channel(channel_chosen)
invalid_input_check(tsu.selected)
when "show details"
if !selected_is_nil(tsu.selected)
tp tsu.print_details(tsu.selected)
end
when "send message"
if !selected_is_nil(tsu.selected)
print "What is your message? "
msg = gets.chomp
tsu.selected.send_message(msg)
end
when "quit"
puts "Thank you for using the Ada Slack CLI"
exit
end
end
end

main if __FILE__ == $PROGRAM_NAME
main

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

Dotenv.load

# https://slack.com/api/conversations.list includes private conversations
# CHANNELS_BASE_URL = "https://slack.com/api/channels.list"
# query_parameters = {
# token: ENV['SLACK_KEY']
# }

# channels_response = HTTParty.get(CHANNELS_BASE_URL, query: query_parameters)
# channels_selected_info = []
# channels_response["channels"].each do |channel|
# channels_selected_info << [channel["id"], channel["members"], channel["name"], channel["name_normalized"]]
# end
# tp channels_selected_info, "channels", "members", "name", "name normalized"

USERS_BASE_URL = "https://slack.com/api/users.list"
query_parameters = {
token: ENV['SLACK_KEY']
}

users_response = HTTParty.get(USERS_BASE_URL, query: query_parameters)
user_array = []
users_response["members"].each do |user|
user_array << user["profile"]["real_name"]
user_array << user["profile"]["display_name"]
end
tp user_array, "real name", "display name"




# CHAT_POST_BASE_URL = "https://slack.com/api/chat.postMessage"
# query_parameters = {
# token: ENV['SLACK_KEY']
# channel:
# text:
# }
28 changes: 28 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'httparty'
require 'dotenv'
require_relative 'recipient'

Dotenv.load

module SlackCLI
class User < Recipient

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

#factory method for producing individual users from json
def self.json_parse(json)
users = []
json["members"].each do |member|
new_user = SlackCLI::User.new(member["id"], member["profile"]["display_name"], member["profile"]["real_name"], member["profile"]["status_text"], member["profile"]["status_emoji"])
users << new_user
end
return users
end
end
end
90 changes: 90 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'httparty'
require 'dotenv'
require 'table_print'
require_relative 'user'
require_relative 'channel'
require_relative 'recipient'

Dotenv.load

module SlackCLI
class Workspace
attr_accessor :users, :channels, :selected

CHANNEL_URL = 'https://slack.com/api/channels.list'
USER_URL = 'https://slack.com/api/users.list'

Choose a reason for hiding this comment

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

I'd put these urls in the respective classes, but that is just me being picky.

GET_PARAMETERS = {
token: ENV['SLACK_KEY']
}

def initialize
@users = SlackCLI::User.json_parse(SlackCLI::User.get(USER_URL, query: GET_PARAMETERS))
@channels = SlackCLI::Channel.json_parse(SlackCLI::Channel.get(CHANNEL_URL, query: GET_PARAMETERS))

Choose a reason for hiding this comment

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

What if these come back with errors? You don't give any useful error messaging so I know what needs to be fixed!

@selected = nil

puts "Workspace loaded! #{users.length} users and #{channels.length} channels available."
end

def list_users
users_hash_array = []
@users.each do |user|
user_hash = {
"slack_id" => user.slack_id,
"display name" => user.name,
"real name" => user.real_name,
"status text" => user.status_text,
"status emoji" => user.status_emoji
}
users_hash_array << user_hash
end
return users_hash_array
end

def list_channels
channels_hash_array = []
@channels.each do |channel|
channel_hash = {
"slack_id" => channel.slack_id,
"name" => channel.name,
"topic" => channel.topic,
"member_count" => channel.member_count
}
channels_hash_array << channel_hash
end
return channels_hash_array
end

def select_user(user_chosen)
selected_user = @users.find { |user| user.slack_id == user_chosen || user.name == user_chosen }

Choose a reason for hiding this comment

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

/Users/devin/Documents/Ada/c12/slack-cli/lib/workspace.rb:58: warning: assigned but unused variable - selected_user

selected_user doesn't actually do anything here.

Choose a reason for hiding this comment

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

If this is a return value, be explicit about that, and don't instantiate a var that won't be used.

end

def select_channel(channel_chosen)
return @channels.find { |channel| channel.slack_id == channel_chosen || channel.name == channel_chosen }
end

def print_details(selected)
if selected.class == SlackCLI::User
user_hash = [
{
"slack_id" => selected.slack_id,
"display name" => selected.name,
"real name" => selected.real_name,
"status text" => selected.status_text,
"status emoji" => selected.status_emoji
}
]
return user_hash
elsif selected.class == SlackCLI::Channel
channel_hash = [
{
"slack_id" => selected.slack_id,
"name" => selected.name,
"topic" => selected.topic,
"member_count" => selected.member_count
}
]
return channel_hash
end
end
end
end
Loading