-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Natalie & Katie #14
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
03a849a
cc0fefd
4fc6986
9565f89
b54a8b1
a7a45cd
646a8e0
8fe71de
d7dc431
d212b8c
f384131
f70f803
ec53974
928e058
0b67fb5
6258d09
d667167
ce44cda
16b95e1
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,36 @@ | ||
| 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:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.list | ||
| response = HTTParty.get("https://slack.com/api/channels.list?token=#{ENV['SLACK_TOKEN']}") | ||
| unless response["ok"] | ||
| raise StandardError.new("Data Load Error") | ||
| end | ||
| array_of_channels = [] | ||
|
|
||
| response["channels"].each do |channel| | ||
| info_hash = { | ||
| slack_id: channel["id"], | ||
| name: channel["name"], | ||
| topic: channel["topic"]["value"], | ||
| member_count: channel["members"].length | ||
| } | ||
| array_of_channels << SlackCLI::Channel.new(info_hash) | ||
| end | ||
| return array_of_channels | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| require 'HTTParty' | ||
| require 'dotenv' | ||
|
|
||
| class SlackApiError < StandardError | ||
| end | ||
|
|
||
| module SlackCLI | ||
| class Recipient | ||
| def initialize | ||
|
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. To fully use your parent class, include |
||
| end | ||
|
|
||
| def self.list | ||
| raise SlackApiError.new("Call this method in child class") | ||
|
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. There's actually a special exception for this case called a |
||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,104 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'awesome_print' | ||
| require_relative 'workspace' | ||
| require "table_print" | ||
|
|
||
| Dotenv.load | ||
| @workspace = SlackCLI::Workspace.new | ||
| def main | ||
|
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. Your CLI flow insures that a user/channel will always be selected before a message is attempted to be sent. However, you may consider giving your user all the menu options rather than forcing them to what you see as the logical next choice after they have say, selected a user. They may want to select a different user! :) |
||
| puts "Welcome to the Ada Slack CLI!" | ||
| menu_method | ||
| selection = gets.chomp | ||
|
|
||
| while selection == "1" || selection == "2" | ||
| print_table(selection) | ||
| puts "************************************" | ||
| learn_more_method(selection) | ||
| message_menu | ||
| menu_method | ||
| selection = gets.chomp | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| # TODO project | ||
| def menu_method | ||
|
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. Nice work encapsulating functionality into methods. |
||
| # menu_options = ["List Users in the Workspace", "List Channels in the Workspace", "Select User", "Select Channel", "Quit program"] | ||
| puts "*********** MAIN MENU **************" | ||
| menu_options = ["List Users in the Workspace", "List Channels in the Workspace"] | ||
| menu_options.each_with_index do |prompt, i| | ||
| puts " #{i + 1}. #{prompt}" | ||
| end | ||
| puts "[Any Other Key] to Quit" | ||
| puts "************************************" | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| def print_table(selection) | ||
| # determines whether you are going to print users or channels | ||
| if selection == "1" | ||
| tp @workspace.users | ||
| elsif selection == "2" | ||
| tp @workspace.channels | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| def learn_more_method(selection) | ||
| # selection "1" is users | ||
| # selection "2" is channels | ||
| puts "Enter \"a\" to search by Channel Name or User Name\nEnter \"b\" to search by Slack ID\n[Any Other Key] to Quit" | ||
| find_by = gets.chomp | ||
|
|
||
| if find_by == "a" | ||
| puts "Enter search term:" | ||
| name_or_id = gets.chomp | ||
| begin | ||
| if selection == "1" | ||
| # this means they're looking for username | ||
| @workspace.select_user(user_name: name_or_id) | ||
| else | ||
| # looking for channel by channel name | ||
| @workspace.select_channel(name: name_or_id) | ||
| end | ||
| rescue | ||
| puts "NOT FOUND" | ||
| end | ||
|
|
||
| elsif find_by == "b" | ||
| puts "Enter search term:" | ||
| name_or_id = gets.chomp | ||
| begin | ||
| if selection == "1" | ||
| @workspace.select_user(slack_id: name_or_id) | ||
| # looking for channel by channel name | ||
| else | ||
| @workspace.select_channel(slack_id: name_or_id) | ||
| # this means they're looking for username | ||
| end | ||
| rescue | ||
| puts "NOT FOUND" | ||
| end | ||
| else | ||
| puts "Invalid menu selection" | ||
| end | ||
| end | ||
|
|
||
| def message_menu | ||
| puts "Enter \"a\" to Show Details\nEnter \"b\" to Send Message\n[Any Other Key] to Quit" | ||
| message_choice = gets.chomp | ||
| if message_choice == "a" | ||
| tp @workspace.show_details | ||
| elsif message_choice == "b" | ||
| begin | ||
| puts "Enter Message: " | ||
| message = gets.chomp | ||
| @workspace.send_message(message) | ||
| rescue | ||
| puts "Message Unable to Send" | ||
| end | ||
| else | ||
| puts "Invalid Menu Choice" | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| require_relative 'recipient' | ||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
| attr_reader :slack_id, :user_name, :real_name | ||
|
|
||
| def initialize(slack_id:, user_name:, real_name:) | ||
| @slack_id = slack_id | ||
| @user_name = user_name | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.list | ||
| response = HTTParty.get("https://slack.com/api/users.list?token=#{ENV['SLACK_TOKEN']}") | ||
|
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 moving the pull request to the class method |
||
| array_of_users = [] | ||
| response["members"].each do |member| | ||
| info_hash = { | ||
| slack_id: member["id"], | ||
| user_name: member["name"], | ||
| real_name: member ["profile"]["real_name"] | ||
| } | ||
| array_of_users << SlackCLI::User.new(info_hash) | ||
| end | ||
| return array_of_users | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| require_relative 'recipient' | ||
| # require_relative 'slack' | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = SlackCLI::User.list | ||
| @channels = SlackCLI::Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_channel(name: nil, slack_id: nil) | ||
|
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 the code for |
||
| @selected = nil | ||
| @channels.each do |channel| | ||
| if channel.name == name || channel.slack_id == slack_id | ||
| @selected = channel | ||
| end | ||
| end | ||
| if @selected.nil? | ||
| raise SlackApiError.new("Channel not found") | ||
| end | ||
| end | ||
|
|
||
| def select_user(user_name: nil, slack_id: nil) | ||
| @selected = nil | ||
| @users.each do |user| | ||
| if user.user_name == user_name || user.slack_id == slack_id | ||
| @selected = user | ||
| end | ||
| end | ||
| if @selected.nil? | ||
| raise SlackApiError.new("User not found") | ||
| end | ||
| end | ||
|
|
||
| def show_details | ||
| @selected | ||
| end | ||
|
|
||
|
|
||
| def send_message(message) | ||
| uri = 'https://slack.com/api/' | ||
| send_message = HTTParty.post("#{uri}/chat.postMessage", body: { | ||
| token: ENV['SLACK_TOKEN'], | ||
| text: message, | ||
| channel: @selected.slack_id }, | ||
| headers: {'Content-Type' => 'application/x-www-form-urlencoded'}) | ||
|
|
||
| unless send_message["ok"] | ||
| raise SlackApiError.new("Message Unable to Send") | ||
| end | ||
| return send_message | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Nice work for raising an exception for an unsuccessful API call.