-
Notifications
You must be signed in to change notification settings - Fork 50
Time - HannahT #47
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?
Time - HannahT #47
Changes from all commits
63ee7e8
45217a6
91cd7de
2ac8ced
14427cb
8363d5e
a8964ad
e112060
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 |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| require 'rake/testtask' | ||
| require "rake/testtask" | ||
|
|
||
| Rake::TestTask.new do |t| | ||
| t.libs = ["lib"] | ||
| t.warning = true | ||
| t.test_files = FileList['test/*_test.rb'] | ||
| t.test_files = FileList["test/*_test.rb"] | ||
| end | ||
|
|
||
| task default: :test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| require "dotenv" | ||
| require "httparty" | ||
| require "awesome_print" | ||
|
|
||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| BASE_URL = "https://slack.com/api/channels.list?" | ||
|
|
||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(topic, member_count, name, slack_id) | ||
| super(slack_id: slack_id, name: name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def print_details | ||
| return { topic: topic, member_count: member_count, slack_id: slack_id, name: name } | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require "httparty" | ||
|
|
||
| module SlackCLI | ||
| class SlackApiError < Exception; end | ||
|
|
||
| class Recipient | ||
|
Comment on lines
+3
to
+6
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. 👍 |
||
| MESSAGE_URL = "https://slack.com/api/chat.postMessage" | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def send_message(message, selected) | ||
| response = HTTParty.post( | ||
| MESSAGE_URL, | ||
| headers: { | ||
| "Content-Type" => "application/x-www-form-urlencoded", | ||
| }, | ||
| body: { | ||
| token: ENV["SLACK_TOKEN"], | ||
| text: message, | ||
| channel: selected.slack_id, | ||
| }, | ||
| ) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return true | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,81 @@ | ||
| #!/usr/bin/env ruby | ||
| require "dotenv" | ||
| require "httparty" | ||
| require "table_print" | ||
| require "awesome_print" | ||
|
|
||
| require_relative "workspace" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
|
|
||
| def allowed_input | ||
| puts "Your options are: list users, list channel, select user, | ||
| select channel, details, and send message or quit: what would you like to see?" | ||
| return gets.chomp() | ||
| end | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| workspace = Workspace.new | ||
| workspace = SlackCLI::Workspace.new | ||
| puts "Welcome to the Ada Slack CLI! | ||
| at this time, our workspace has #{workspace.users.count} users | ||
| and #{workspace.channels.count} channels." | ||
|
|
||
| user_input = allowed_input | ||
|
|
||
| until user_input == "quit" || user_input == "exit" | ||
| case user_input | ||
|
|
||
| when "list users" | ||
| tp workspace.users, "slack_id", "name" | ||
| puts "\n" | ||
| when "list channel" | ||
| tp workspace.channels, "name", "topic", "member_count", "slack_id" | ||
| puts "\n" | ||
| when "select user" | ||
| puts "please enter a user name or slack_id" | ||
| user_input = gets.chomp() | ||
| if workspace.select_user(user_input) == nil | ||
| puts "no user with that user name exist" | ||
| else | ||
| puts "selected user with id #{workspace.selected.slack_id}" | ||
| end | ||
| when "select channel" | ||
| puts "please enter a channel name or slack_id" | ||
| user_input = gets.chomp() | ||
| if workspace.select_channel(user_input) == nil | ||
| puts "no channel with that channel name exist" | ||
| else | ||
| puts "selected channel with id #{workspace.selected.slack_id}" | ||
| end | ||
| when "detail", "details" | ||
| if workspace.selected == nil | ||
| puts "no user selected" | ||
| else | ||
| ap workspace.selected.print_details | ||
| end | ||
| if workspace.selected == nil | ||
| puts "no channel selected" | ||
| else | ||
| ap workspace.selected.print_details | ||
| end | ||
| when "send message" | ||
| if workspace.selected == nil | ||
| puts "you need to selec a user or a channel." | ||
| puts "\n" | ||
| else | ||
| puts "enter your message: " | ||
| user_input = gets.chomp() | ||
| success = workspace.send_message(user_input) | ||
| puts "\n" | ||
| puts "sent message" if success | ||
| end | ||
| end | ||
|
|
||
| # TODO project | ||
| user_input = allowed_input | ||
| 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,20 @@ | ||
| require_relative "recipient" | ||
| require "httparty" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class User < Recipient | ||
|
Comment on lines
+6
to
+7
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. 👍 |
||
| BASE_URL = "https://slack.com/api/users.list?" | ||
| attr_reader :channels, :name, :slack_id | ||
|
|
||
| def initialize(name, slack_id, channels = []) | ||
| super(slack_id: slack_id, name: name) | ||
| @channels = channels | ||
| end | ||
|
|
||
| def print_details | ||
| return { name: name, slack_id: slack_id, channels: channels } | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
| require "pry" | ||
|
|
||
| module SlackCLI | ||
| class SlackApiError < Exception; end | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels, :selected, :selected, :query | ||
|
|
||
| Dotenv.load | ||
|
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 noticed you have this line in several places, just maybe there is a way to only need to do this once. Otherwise well done. |
||
|
|
||
| def initialize() | ||
| @query = { token: SLACK_TOKEN } | ||
| @users = self.list_all_user | ||
| @channels = self.list_all_channel | ||
| @selected = nil | ||
| @selected = nil | ||
| end | ||
|
|
||
| def list_all_user | ||
| all_users = [] | ||
| response = HTTParty.get(SlackCLI::User::BASE_URL, query: query)["members"] | ||
|
|
||
| response.each do |person| | ||
| all_users << SlackCLI::User.new(person["name"], person["id"]) | ||
| end | ||
| return all_users | ||
| end | ||
|
Comment on lines
+21
to
+29
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. Methods like this for user and Channel could be instead class methods in the User and Channel class and that would help separate the command-line stuff in workspace with the HTTP requests in Recipient, Channel and User. |
||
|
|
||
| def list_all_channel | ||
| all_channel = [] | ||
| response = HTTParty.get(SlackCLI::Channel::BASE_URL, query: query)["channels"] | ||
| response.each do |channel| | ||
| all_channel << SlackCLI::Channel.new( | ||
| channel["topic"]["value"], | ||
| channel["members"].length, | ||
| channel["name"], | ||
| channel["id"] | ||
| ) | ||
| end | ||
| return all_channel | ||
| end | ||
|
|
||
| def select_user(user_id) | ||
| @users.each do |user| | ||
| if user_id == user.slack_id | ||
| @selected = user | ||
| elsif user_id == user.name | ||
| @selected = user | ||
| end | ||
| end | ||
| return @selected | ||
| end | ||
|
|
||
| def select_channel(channel_id) | ||
| @channels.each do |channel| | ||
| if channel_id == channel.slack_id | ||
| @selected = channel | ||
| elsif channel_id == channel.name | ||
| @selected = channel | ||
| end | ||
| end | ||
| return @selected | ||
| end | ||
|
|
||
| #get request, client wants data from the server (doens't make any change) | ||
| #post request, changing stuff on the server (makes changes) | ||
|
|
||
| def send_message(message) | ||
| if @selected | ||
| return @selected.send_message(message, @selected) | ||
| end | ||
| 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.
Minor suggestion, I would call this method
get_detailsjust because it doesn't actually print.