-
Notifications
You must be signed in to change notification settings - Fork 26
Branches -- Angele & Paige #17
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
1bf4bfb
c7d314f
2be7169
3aedac1
cc78eca
fede551
0875424
c89c942
09d3f01
a16cf80
d727d4b
19d7b70
62270c4
ef7d608
84b14fd
1a3a95c
6c5aeca
e2d73e7
b13fa09
df4857e
f9cff44
c1f0c8a
bf8a8ad
c317d0d
504008e
51c83c4
3588db0
2fb2f5a
43098a4
66ce941
f5af80e
c85040b
2b77254
1842a8d
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 |
|---|---|---|
|
|
@@ -54,3 +54,6 @@ build-iPhoneSimulator/ | |
|
|
||
| # Ignore cassette files | ||
| /specs/cassettes/ | ||
|
|
||
| # Ignore .DS_Store | ||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||
| require_relative 'recipient' | ||||||
|
|
||||||
| module SlackCLI | ||||||
| class Channel < Recipient | ||||||
| attr_reader :topic, :member_count | ||||||
|
|
||||||
| def initialize(slack_id:, name:, topic:, member_count:) | ||||||
| super(slack_id: slack_id, name: name) | ||||||
| @topic = topic | ||||||
| @member_count = member_count | ||||||
| end | ||||||
|
|
||||||
| def self.list | ||||||
| response = self.get("https://slack.com/api/channels.list") | ||||||
|
|
||||||
| channels = response["channels"].map do |channel| | ||||||
| slack_id = channel["id"] | ||||||
| name = channel["name"] | ||||||
| topic = channel["topic"]["value"] | ||||||
| member_count = channel["members"].length | ||||||
|
|
||||||
| SlackCLI::Channel.new( | ||||||
| slack_id: slack_id, | ||||||
| name: name, | ||||||
| topic: topic, | ||||||
| member_count: member_count | ||||||
| ) | ||||||
| end | ||||||
|
|
||||||
| return channels | ||||||
| end | ||||||
|
|
||||||
| def details | ||||||
| key = ENV["API_TOKEN"] | ||||||
| response = HTTParty.get("https://slack.com/api/channels.info", query: {token: key , channel: @slack_id}) | ||||||
| if response.keys.include? "error" || response["ok"] == false | ||||||
| raise SlackCLI::SlackApiError | ||||||
|
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. It would be nice if you'd included what the error was in the message here.
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| purpose = response["channel"]["purpose"]["value"] | ||||||
| is_private = response["channel"]["is_private"] ? "true" : "false" | ||||||
|
|
||||||
| "Slack ID: #{slack_id}\n" + | ||||||
| "Name: #{name}\n" + | ||||||
| "Topic: #{topic}\n" + | ||||||
| "Member Count: #{member_count}\n" + | ||||||
| "Purpose: #{purpose}\n" + | ||||||
| "Is Private? #{is_private}" | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require 'httparty' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class SlackApiError < 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) | ||
| key = ENV["API_TOKEN"] | ||
| response = HTTParty.get(url, query: {token: key }) | ||
| if response.keys.include? "error" || response["ok"] == false | ||
| raise SlackCLI::SlackApiError | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def send_message(message) | ||
| # pull out key to be @key? | ||
| key = ENV["API_TOKEN"] | ||
| response = HTTParty.post( | ||
| "https://slack.com/api/chat.postMessage", | ||
| headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, | ||
| body: { | ||
| "token": key, | ||
| "channel": @slack_id, | ||
| "text": message | ||
| } | ||
| ) | ||
|
|
||
| if response.keys.include? "error" || response["ok"] == false | ||
|
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. You should be checking |
||
| raise SlackCLI::SlackApiError.new("Message not sent due to error: #{response["error"]}") | ||
| else | ||
| return true | ||
|
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. I appreciate what you're doing here but since this method raises the only thing it can ever return is |
||
| end | ||
| end | ||
|
|
||
| def details | ||
| raise NotImplementedError.new("Details should be implemented in child class") | ||
| end | ||
|
|
||
| def self.list | ||
| raise NotImplementedError.new("Self.list should be implemented 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. Abstract methods! 🎉 |
||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,110 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative 'workspace' | ||
| require 'table_print' | ||
| require 'terminal-table' | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| puts "\nWelcome to the Ada Slack CLI!" | ||
| workspace = SlackCLI::Workspace.new | ||
| channels_count = workspace.channels.length | ||
| users_count = workspace.users.length | ||
| puts "\n#{channels_count} channels and #{users_count} users loaded" | ||
|
|
||
| choice = "" | ||
| until choice == "7" || choice == "quit" | ||
| puts "\nWhat would you like to do?" | ||
| menu_options | ||
| print "Action: " | ||
| choice = gets.chomp.downcase | ||
|
|
||
| case choice | ||
| when "1","list users" | ||
| puts "\n" | ||
| list_users(workspace) | ||
|
|
||
| # TODO project | ||
| when "2","list channels" | ||
| puts "\n" | ||
| list_channels(workspace) | ||
|
|
||
| when "3","select user" | ||
| puts "Select User" | ||
| print "Please enter a Username or Slack ID: " | ||
| query = gets.chomp.downcase | ||
| query_result = workspace.select_user(query) | ||
| if query_result == nil | ||
| puts "\nUser with Username or Slack ID '#{query}' does not exist." | ||
| else | ||
| puts "\nUser '#{query_result.name}' selected" | ||
| end | ||
|
|
||
| when "4","select channel" | ||
| puts "Select Channel" | ||
| print "Please enter a Channel Name or Slack ID: " | ||
| query = gets.chomp.downcase | ||
| query_result = workspace.select_channel(query) | ||
|
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. I'd have liked to have seen the error here handled with an exception and then Plus, it could give you to opportunity to DRY up your code by putting all of your error handling in a single |
||
| if query_result == nil | ||
| puts "\nChannel with Name or Slack ID '#{query}' does not exist." | ||
| else | ||
| puts "\nChannel '#{query_result.name}' selected" | ||
| end | ||
|
|
||
| when "5","details" | ||
| if workspace.show_details == nil | ||
| puts "\nNo user or channel selected." | ||
| else | ||
| puts "\nDetails:" | ||
| puts "#{workspace.show_details}" | ||
| end | ||
|
|
||
| when "6","send message" | ||
| if workspace.selected == nil | ||
| puts "\nNo recipient selected to send message to." | ||
| else | ||
| puts "Send Message to '#{workspace.selected.name}'" | ||
| print "Please enter message text: " | ||
| message_text = gets.chomp | ||
|
|
||
| until message_text != "" | ||
| puts "No message entered." | ||
| print "Please enter message text: " | ||
| message_text = gets.chomp | ||
| end | ||
| if workspace.send_message(message_text) | ||
| puts "Message successfully sent" | ||
| end | ||
| end | ||
|
|
||
| when "7","quit" | ||
| break | ||
| else | ||
| puts "That option does not exist" | ||
| end | ||
|
|
||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| def menu_options | ||
| numbered_options = [ | ||
| ["1", "list users"], | ||
| ["2", "list channels"], | ||
| ["3", "select user"], | ||
| ["4", "select channel"], | ||
| ["5", "details"], | ||
| ["6", "send message"], | ||
| ["7","quit"] | ||
| ] | ||
| menu_table = Terminal::Table.new :headings => ["#", "Action"], :rows => numbered_options | ||
| puts menu_table | ||
| end | ||
|
|
||
| def list_users(workspace) | ||
| tp workspace.users, :slack_id, :name, :real_name | ||
| end | ||
|
|
||
| def list_channels(workspace) | ||
| tp workspace.channels, :slack_id, :name, :topic, :member_count | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require_relative 'recipient' | ||
| require 'httparty' | ||
| require 'dotenv' | ||
|
|
||
| Dotenv.load | ||
|
|
||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
| attr_reader :real_name | ||
|
|
||
| def initialize(slack_id:, name:, real_name:) | ||
| super(slack_id: slack_id, name: name) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.list | ||
| response = self.get("https://slack.com/api/users.list") | ||
|
|
||
| users = response["members"].map do |member| | ||
| slack_id = member["id"] | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
|
|
||
| SlackCLI::User.new(slack_id: slack_id, name: name, real_name: real_name) | ||
| end | ||
|
|
||
| return users | ||
| end | ||
|
|
||
| def details | ||
| key = ENV["API_TOKEN"] | ||
| response = HTTParty.get("https://slack.com/api/users.info", query: {token: key , user: @slack_id}) | ||
|
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. If you made |
||
| if response.keys.include? "error" || response["ok"] == false | ||
| raise SlackCLI::SlackApiError | ||
| end | ||
|
|
||
| status = response["user"]["profile"]["status_text"] | ||
| is_bot = response["user"]["is_bot"] ? "true" : "false" | ||
|
|
||
| "Slack ID: #{slack_id}\n" + | ||
| "Name: #{name}\n" + | ||
| "Real Name: #{real_name}\n" + | ||
| "Status: #{status}\n" + | ||
| "Is Bot?: #{is_bot}" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
|
|
||
| module SlackCLI | ||
| class Workspace | ||
| attr_reader :users, :channels | ||
| attr_accessor :selected | ||
|
|
||
| def initialize | ||
| @users = SlackCLI::User.list | ||
| @channels = SlackCLI::Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def find_instance(recipient_list, name_or_id) | ||
| instance = recipient_list.find do |recipient| | ||
| recipient.name.downcase == name_or_id.downcase || | ||
| recipient.slack_id.downcase == name_or_id.downcase | ||
| end | ||
|
|
||
| return instance | ||
| end | ||
|
|
||
| def select_user(name_or_id) | ||
| user = find_instance(users, name_or_id) | ||
| @selected = user if user != nil | ||
|
|
||
| return @selected | ||
| end | ||
|
|
||
| def select_channel(name_or_id) | ||
| channel = find_instance(channels, name_or_id) | ||
| @selected = channel if channel != nil | ||
|
|
||
| return @selected | ||
| end | ||
|
|
||
| def show_details | ||
| selected ? selected.details : nil | ||
| end | ||
|
|
||
| def send_message(message_text) | ||
| @selected.send_message(message_text) | ||
| return true | ||
| 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.
You can ignore these globally:
https://coderwall.com/p/vz6ymw/how-to-globally-gitignore-ds_store