-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Kristina & Ga-Young #8
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
base: master
Are you sure you want to change the base?
Changes from all commits
4349d08
6bced36
0913ab4
f9e9110
185bc2a
55e1d8a
361380a
b41d1da
20bf96d
14588c4
9ffa927
f5e5b96
1dbdd9f
59afa96
f4fe774
f7a7f41
02c4e54
006161e
26454d6
5d0d7c1
1edf180
202adb8
2e5fbb4
f98a3b2
7520454
9f8acc0
8e750a9
b71f28a
3ba429c
22c9cf2
a8df6c0
a55e36f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module Slack | ||
| class Channel | ||
| attr_reader :channel_name, :topic, :member_count, :slack_id | ||
|
|
||
| def initialize(channel_name:, topic:, member_count:, slack_id:) | ||
| @channel_name = channel_name | ||
| @topic = topic | ||
| @member_count = member_count | ||
| @slack_id = slack_id | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,119 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "workspace" | ||
| require_relative "user" | ||
| require_relative "channel" | ||
| require 'httparty' | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| puts "Welcome to the Ada Slack CLI!\n" | ||
| workspace = Slack::Workspace.new | ||
| workspace.user_list | ||
| workspace.channel_list | ||
|
|
||
| puts "\nDarn Cute Puppers has #{workspace.users.count} users and #{workspace.channels.count} channels.\n" | ||
|
|
||
| # TODO project | ||
| prompt = "\nPlease select from the following options: | ||
| List Users | ||
| List Channels | ||
| Select User | ||
| Select Channel | ||
| Quit\n" | ||
|
|
||
| puts prompt | ||
|
|
||
| while command = gets.chomp.downcase | ||
| case command | ||
| when "quit" | ||
| exit | ||
| when "list users" | ||
| puts workspace.print_user_list | ||
| puts prompt | ||
| when "list channels" | ||
| puts workspace.print_channel_list | ||
| puts prompt | ||
| when "select user" | ||
| puts "Please enter a username or Slack ID." | ||
| username = gets.chomp.downcase | ||
| search_result = workspace.search("user", username) | ||
| if search_result == nil | ||
| puts "User not found. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
| else | ||
| puts "User found." | ||
| options = "Please select from the following options: | ||
| Show Details | ||
| Send Message | ||
| Main Menu" | ||
| puts options | ||
| while selected_command = gets.chomp.downcase | ||
| case selected_command | ||
| when "main menu" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding the options "show details" and "send message" to the main menu so that user of the CLI can send a second message to an already selected recipient. |
||
| puts prompt | ||
| break | ||
| when "show details" | ||
| puts workspace.show_details(search_result) | ||
| puts options | ||
| when "send message" | ||
| puts "What message do you want to send?" | ||
| message_body = gets.chomp | ||
| convert_to_username = workspace.selected.username | ||
| workspace.send_message(message_body, "@#{convert_to_username}") | ||
| puts "Message sent. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
| else | ||
| puts "Invalid input. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
| end | ||
| end | ||
| end | ||
| when "select channel" | ||
| puts "Please enter a channel name or Slack ID." | ||
| channel_name = gets.chomp.downcase | ||
| search_result = workspace.search("channel", channel_name) | ||
| if search_result == nil | ||
| puts "Channel not found. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
| else | ||
| puts "Channel found." | ||
| options = "Please select from the following options: | ||
| Show Details | ||
| Send Message | ||
| Main Menu" | ||
| puts options | ||
| while selected_command = gets.chomp.downcase | ||
| case selected_command | ||
| when "main menu" | ||
| puts prompt | ||
| break | ||
| when "show details" | ||
| puts workspace.show_details(search_result) | ||
| puts options | ||
| when "send message" | ||
| puts "What message do you want to send?" | ||
| message_body = gets.chomp | ||
| convert_to_channel_name = workspace.selected.channel_name | ||
| workspace.send_message(message_body, convert_to_channel_name) | ||
| puts "Message sent. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
| else | ||
| puts "Invalid input. Returning to main menu..." | ||
| puts prompt | ||
| break | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider allowing users to re-enter their input rather than breaking the program when the user input is invalid. |
||
| end | ||
| end | ||
| end | ||
| else | ||
| puts "Invalid input. Returning to main menu..." | ||
| break | ||
| end | ||
| end | ||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Slack | ||
| class User | ||
| attr_reader :username, :real_name, :slack_id | ||
|
|
||
| def initialize(username:, real_name:, slack_id:) | ||
| @username = username | ||
| @real_name = real_name | ||
| @slack_id = slack_id | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| module Slack | ||
| class SlackApiError < StandardError; end | ||
| class Workspace | ||
| USER_URL = "https://slack.com/api/users.list" | ||
| CHANNEL_URL = "https://slack.com/api/channels.list" | ||
| POST_MSG_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = [] | ||
| @channels = [] | ||
| @selected = nil | ||
| end | ||
|
|
||
| def get_api(url) | ||
| query_parameters = { | ||
| token: ENV['SLACK_TOKEN'] | ||
| } | ||
| return HTTParty.get(url, query: query_parameters) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure you handle bad requests appropriately. |
||
| end | ||
|
|
||
| def user_list | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you've done here works. Nice work! However, the recommendation to put this method in the user class was there to reduce dependencies between classes. When you instantiate a new User below, this means that Workspace needs to know everything about User. This is a perfect situation for a class method. |
||
| api_user_response = get_api(USER_URL) | ||
| api_members = api_user_response["members"] | ||
| api_members.each do |each_member| | ||
| username = each_member["name"] | ||
| real_name = each_member["real_name"] | ||
| slack_id = each_member["id"] | ||
| @users << Slack::User.new(username: username, real_name: real_name, slack_id: slack_id) | ||
| end | ||
| end | ||
|
|
||
| def print_user_list | ||
| user_counter = 0 | ||
| result = '' | ||
| @users.each do |each_user| | ||
| user_counter += 1 | ||
| result = result + "User #{user_counter} - username: #{each_user.username}, real name: #{each_user.real_name}, Slack ID: #{each_user.slack_id}\n" | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| def channel_list | ||
| api_channel_response = get_api(CHANNEL_URL) | ||
| api_channels = api_channel_response["channels"] | ||
| api_channels.each do |each_channel| | ||
| channel_name = each_channel["name"] | ||
| topic = each_channel["topic"]["value"] | ||
| member_count = each_channel["num_members"] | ||
| slack_id = each_channel["id"] | ||
| @channels << Channel.new(channel_name: channel_name, topic: topic, member_count: member_count, slack_id: slack_id) | ||
| end | ||
| end | ||
|
|
||
| def print_channel_list | ||
| channel_counter = 0 | ||
| result = '' | ||
| @channels.each do |each_channel| | ||
| channel_counter += 1 | ||
| result = result + "Channel #{channel_counter} - channel name: #{each_channel.channel_name}, topic: #{each_channel.topic}, member count: #{each_channel.member_count}, Slack ID: #{each_channel.slack_id}\n" | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| def search(data_source, query_term) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is very similar to select a user and select a channel. Could you DRY this up somehow? |
||
| @selected = nil | ||
| if data_source == "user" | ||
| @users.each do |current_user| | ||
| if current_user.username == query_term || current_user.slack_id == query_term.upcase | ||
| @selected = current_user | ||
| break | ||
| end | ||
| end | ||
| return @selected | ||
| elsif data_source == "channel" | ||
| @channels.each do |current_channel| | ||
| if current_channel.channel_name == query_term || current_channel.slack_id == query_term.upcase | ||
| @selected = current_channel | ||
| break | ||
| end | ||
| end | ||
| return @selected | ||
| end | ||
| end | ||
|
|
||
| def show_details(selected_receiver) | ||
| details = "" | ||
| if selected_receiver.class == Slack::User | ||
| details = details + "Username: #{@selected.username}, Real name: #{@selected.real_name}, Slack ID: #{@selected.slack_id}" | ||
| return details | ||
| elsif selected_receiver.class == Slack::Channel | ||
| details = details + "Channel name: #{@selected.channel_name}, Topic: #{@selected.topic}, Member count: #{@selected.member_count}, Slack ID: #{@selected.slack_id}" | ||
| return details | ||
| end | ||
| end | ||
|
|
||
| def send_message(message, selected_receiver) | ||
| response = HTTParty.post( | ||
| POST_MSG_URL, | ||
| body: { | ||
| token: ENV['SLACK_TOKEN'], | ||
| text: message, | ||
| channel: selected_receiver | ||
| }, | ||
| headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } | ||
| ) | ||
|
|
||
| unless response.parsed_response["ok"] | ||
| raise SlackApiError, "Error when posting #{message} to #{selected_receiver}, error: #{response.parsed_response["error"]}" | ||
| end | ||
| return response | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using helper methods to encapsulate some of the functionality in this CLI.