diff --git a/.gitignore b/.gitignore index 3ff4fada..a7163fef 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,8 @@ .DS_Store -# Ignore environemnt variables +# Ignore environment variables .env + + + diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md new file mode 100644 index 00000000..9d5edcae --- /dev/null +++ b/DEVELOPMENT.md @@ -0,0 +1,6 @@ +# Slack CLI Development + +## Commands +1. `rake` - executes the CLI (_same as `rake_run`_) +1. `rake run` - executes the CLI +1. `rake test` - runs unit tests diff --git a/Rakefile b/Rakefile index 0c2d13fe..a61c6097 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,14 @@ +require 'rake/task' require 'rake/testtask' +task :run do + sh "ruby lib/slack.rb" +end + Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true t.test_files = FileList['test/*_test.rb'] end -task default: :test +task default: :run diff --git a/lib/channel.rb b/lib/channel.rb new file mode 100644 index 00000000..38755c61 --- /dev/null +++ b/lib/channel.rb @@ -0,0 +1,37 @@ +require 'httparty' +require 'table_print' + +require_relative 'workspace.rb' +require_relative 'receiver.rb' + + +module SlackCLI + class Channel < Receiver + + attr_reader :focus, :headcount + + def initialize(focus:, headcount:, id:, name:) + @focus = focus + @headcount = headcount + super(id: id, name: name) + end + + def summary + tp self, "id", "name", "focus", "headcount" + end + + def self.show_all + clap_back = SlackCLI::Channel.get_url("https://slack.com/api/conversations.list") + channels = [] + clap_back["channels"].each do |i| + channels << SlackCLI::Channel.new( + name: i["name"], + id: i["id"], + focus: i["topic"]["value"], + headcount: i["num_members"] + ) + end + return channels + end + end +end \ No newline at end of file diff --git a/lib/receiver.rb b/lib/receiver.rb new file mode 100644 index 00000000..30cf091f --- /dev/null +++ b/lib/receiver.rb @@ -0,0 +1,37 @@ +require 'httparty' + +require_relative 'workspace.rb' + +module SlackCLI + class Receiver + + attr_reader :id, :name + + def initialize(id:, name:) + @id = id + @name = name + end + + def give_slack(body_talk) + clap_back = HTTParty.post("https://slack.com/api/chat.postMessage", query: {token: ENV['BOT_TOKEN'], channel: self.id, text: body_talk}) + + unless clap_back.code == 200 || clap_back["ok"] != false + raise ArgumentError.new("Whoops. Someone dropped the butter on the cat toy: #{clap_back["error"]}") + end + return clap_back + end + + def self.get_url(url) + clap_back = HTTParty.get(url, query: {token: ENV['BOT_TOKEN']}) + + unless clap_back.code == 200 || clap_back["ok"] != false + raise ArgumentError.new("Whoops. Someone dropped the butter on the cat toy: #{clap_back["error"]}") + end + return clap_back + end + + def self.show_all + # Just hanging out until my children need me + end + end +end \ No newline at end of file diff --git a/lib/slack.rb b/lib/slack.rb index 8a0b659b..73b5cff5 100755 --- a/lib/slack.rb +++ b/lib/slack.rb @@ -1,12 +1,67 @@ #!/usr/bin/env ruby -def main - puts "Welcome to the Ada Slack CLI!" - workspace = Workspace.new +require 'table_print' +require 'dotenv' +require 'httparty' - # TODO project +Dotenv.load - puts "Thank you for using the Ada Slack CLI" -end +require_relative 'workspace.rb' +require_relative 'receiver.rb' +require_relative 'channel.rb' +require_relative 'user.rb' -main if __FILE__ == $PROGRAM_NAME \ No newline at end of file +module SlackCLI + def self.main + + puts "Welcome to the Ada Slack CLI!\n\n" + workspace = SlackCLI::Workspace.new + + puts "The Ada Slack CLI can do several things:\n + 1. List users\n + 2. Choose user\n + 3. List channel\n + 4. Choose channel\n + 5. Summary\n + 6. Send message\n + 7. Exit program\n\n" + + puts "Please enter the number entry you'd like to execute:" + + input = gets.chomp.to_i + # function exits when input == 7 + while true do + case input + when 1 + puts "The users of this program are:\n" + tp workspace.expose_users, "legal_name", "id", "name" + break + when 2 + puts "Enter a username to send your message to:\n" + puts workspace.choose_user + when 3 + puts "The channels of this program are:\n" + tp workspace.expose_channels, "name", "id", "focus", "headcount" + break + when 4 + puts "Enter a channel name to post your message to:\n" + puts workspace.choose_channel + when 5 + workspace.deets + when 6 + puts "Enter the message you'd like to send here:\n\n" + body_talk = gets.chomp + workspace.speak(body_talk) + when 7 + puts "Thank you for using the Ada Slack CLI\n\n" + return + else + puts "Hmm, I might have the memory of a goldfish; I don't remember that option being listed. Try again now?\n\n" + input = gets.chomp.to_i + end + puts "Please enter a new command." + input = gets.chomp.to_i + end + end + main if __FILE__ == $PROGRAM_NAME +end \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 00000000..42889a55 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,40 @@ +require 'httparty' +require 'table_print' + +require_relative 'workspace.rb' +require_relative 'receiver.rb' + + +module SlackCLI + class User < Receiver + + attr_reader :legal_name, :status_text, :status_emoji + + def initialize(legal_name:, status_text:, status_emoji:, id:, name:) + super(id: id, name: name) + + @legal_name = legal_name + @status_text = status_text + @status_emoji = status_emoji + end + + def summary + tp self, "id", "name", "legal_name" + end + + def self.show_all + clap_back = SlackCLI::User.get_url("https://slack.com/api/users.list") + users = [] + clap_back["members"].each do |spec| + users << SlackCLI::User.new( + legal_name: spec["real_name"], + name: spec["name"], + id: spec["id"], + status_text: spec["profile"]["status_text"], + status_emoji: spec["profile"]["status_emoji"] + ) + end + return users + end + end +end \ No newline at end of file diff --git a/lib/workspace.rb b/lib/workspace.rb new file mode 100644 index 00000000..0a3a8410 --- /dev/null +++ b/lib/workspace.rb @@ -0,0 +1,68 @@ + +require_relative 'receiver.rb' +require_relative 'channel.rb' +require_relative 'user.rb' +require_relative 'slack.rb' + + +module SlackCLI + class Workspace + attr_reader :users, :channels, :chosen + + def initialize + @users = [] + @channels = [] + @chosen = nil + end + + def choose_channel + sought = gets.chomp.downcase + + expose_channels + + channels.each do |channel| + if channel.name == sought || channel.id == sought + @chosen = channel + return "Selected #{channel.name}." + end + end + return "Could not find channel." + end + + def choose_user + sought = gets.chomp.downcase + + expose_users + + users.each do |user| + if user.name == sought || user.id == sought + @chosen = user + return "Selected #{user.name}." + end + end + return "Could not find user." + end + + def expose_users + @users = SlackCLI::User.show_all + end + + def expose_channels + @channels = SlackCLI::Channel.show_all + end + + def deets + if @chosen + @chosen.summary + return + end + puts "" + end + + def speak(body_talk) + unless @chosen == nil + @chosen.give_slack(body_talk) + end + end + end +end \ No newline at end of file diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 00000000..45069fc9 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,14 @@ +require_relative 'test_helper.rb' + +describe "initialize" do + +end + +describe "summary" do + +end + + +describe "self.show_all" do + +end \ No newline at end of file diff --git a/test/receiver_test.rb b/test/receiver_test.rb new file mode 100644 index 00000000..7516e032 --- /dev/null +++ b/test/receiver_test.rb @@ -0,0 +1,21 @@ +require_relative 'test_helper.rb' + + +describe "initialize" do + +end + + +describe "give_slack" do + +end + + +describe "self.get_url" do + +end + + +describe "self.show_all" do + +end \ No newline at end of file diff --git a/test/slack_test.rb b/test/slack_test.rb new file mode 100644 index 00000000..e69de29b diff --git a/test/user_test.rb b/test/user_test.rb new file mode 100644 index 00000000..1b82685e --- /dev/null +++ b/test/user_test.rb @@ -0,0 +1,14 @@ +require_relative 'test_helper.rb' + +describe "initialize" do + +end + +describe "summary" do + +end + +describe "self.show_all" do + +end + diff --git a/test/workspace_test.rb b/test/workspace_test.rb new file mode 100644 index 00000000..e2b6f6bb --- /dev/null +++ b/test/workspace_test.rb @@ -0,0 +1,21 @@ +require_relative 'test_helper.rb' + +describe "initialize" do + +end + +describe "choose_channel" do + +end + +describe "choose_user" do + +end + +describe "show_summary" do + +end + +describe "speak" do + +end \ No newline at end of file