-
Notifications
You must be signed in to change notification settings - Fork 26
Daniela & Cloudy - Leaves #22
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
c96e696
d6b04bd
0300de9
62092a6
bdaced2
93cf8bb
3209db1
a6f7fd4
c3c746f
13a9df3
214cb9c
83702a2
21e8578
a6d48e8
3e442c7
5400aa6
bf99630
66654ed
da83a78
099722c
6d125e8
bb9ccde
a51f1f5
b0ffc4c
30148ba
5f33fb1
30d5654
7876ed7
99c9d3b
26ea133
f4097e6
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,34 @@ | ||
| #lib/channel.rb | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| require 'dotenv' | ||
| require_relative "recipient" | ||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| CHANNEL_URI = 'https://slack.com/api' | ||
| CHANNEL_KEY = ENV['API_KEY'] | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(name, id, topic, member_count) | ||
| super(name, id) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.channels_list | ||
| channels = [] | ||
| response = HTTParty.get("#{CHANNEL_URI}/channels.list", query: {token: CHANNEL_KEY}) | ||
|
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. The route I'd rather see you go here is to have a generic |
||
| response.parsed_response["channels"].each do |channel| | ||
| name = channel["name"] | ||
| id = channel["id"] | ||
| topic = channel["topic"]["value"] | ||
| member_count = channel["num_members"] | ||
| channel = Slack::Channel.new(name, id, topic, member_count) | ||
| channels << channel | ||
| end | ||
| return channels | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #lib/message.rb | ||
|
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 isn't really a message class, it's a |
||
| require "httparty" | ||
|
|
||
| class SlackApiError < StandardError ; end | ||
|
|
||
| module Slack | ||
| def self.send_msg(message, channel) | ||
|
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're going to go the route of making an API wrapper, move all of the messages into the wrapper! |
||
|
|
||
| response = HTTParty.post( | ||
| "https://slack.com/api/chat.postMessage", | ||
| body: { | ||
| token: ENV['API_KEY'], | ||
| text: message, | ||
| channel: channel | ||
| }, | ||
| headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } | ||
| ) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, "Error when posting #{message} to #{channel}, error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return true | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #lib/recipient.rb | ||
| module Slack | ||
| class Recipient | ||
| attr_reader :name, :id | ||
| def initialize(name, id) | ||
| @name = name | ||
| @id = 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're writing a class this short, ask yourself 2 questions:
|
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,66 @@ | ||
| #!/usr/bin/env ruby | ||
| #lib/slack.rb | ||
| require "httparty" | ||
| require "awesome_print" | ||
| require "colorize" | ||
| require "table_print" | ||
|
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've included this here... |
||
| require 'dotenv' | ||
| require_relative "workspace" | ||
| require_relative 'message' | ||
| Dotenv.load | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
|
|
||
| # TODO project | ||
|
|
||
| @workspace = Slack::Workspace.new() | ||
| while true | ||
| puts "Welcome to the Ada Slack CLI!".colorize(:color => :orange, :mode => :bold) | ||
| puts "\nPlease Choose from the following options: | ||
|
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. These two lines print every time you come back to the top of the loop, but it would make more sense for them to only print when the CLI starts. |
||
| \n1. List Users | ||
| \n2. List Channels | ||
| \n3. Select User | ||
| \n4. Select Channel | ||
| \n5. Details | ||
| \n6. Send Message | ||
| \n7. Quit".colorize(:color => :purple, :mode => :bold) | ||
|
|
||
| option = gets.chomp.downcase | ||
|
|
||
| case option | ||
| when "list users", "1" | ||
| tp @workspace.users, "name", "id", "real_name" | ||
| sleep(2) | ||
| when "list channels", "2" | ||
| tp @workspace.channels, "name", "id", "topic", "member_count" | ||
| sleep(2) | ||
| when "select user", "3" | ||
| puts "Please enter username or Slack ID" | ||
| user_selection = gets.chomp | ||
| @workspace.select_user(user_selection) | ||
|
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 love to see some sort of confirmation message if things went right here. |
||
| when "select channel", "4" | ||
| puts "Please enter channel name or Slack ID" | ||
| channel_selection = gets.chomp | ||
| @workspace.select_channel(channel_selection) | ||
| when "details", "5" | ||
| puts @workspace.show_details | ||
| sleep(2) | ||
| when "send message", "6" | ||
| recipient = @workspace.recipient | ||
| if recipient.class == Slack::Channel | ||
| print "Please enter the message: " | ||
| message = gets.chomp | ||
| name = recipient.name | ||
| Slack.send_msg(message, name) | ||
| elsif recipient.class == Slack::User | ||
| print "Please enter the message: " | ||
| message = gets.chomp | ||
| id = recipient.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. consider breaking these larger blocks out into helper methods so that this logic is easier to read! |
||
| Slack.send_msg(message, id) | ||
| else | ||
| puts "No recipient selected" | ||
| sleep(2) | ||
| end | ||
| when "quit", "7" | ||
| exit | ||
| end | ||
| end | ||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #lib/user.rb | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| require 'dotenv' | ||
| require_relative "recipient" | ||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| class User < Recipient | ||
| attr_reader :real_name | ||
| def initialize(name, id, real_name) | ||
| super(name, id) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.users_list | ||
| users = [] | ||
| response = HTTParty.get("https://slack.com/api/users.list", query: {token: ENV['API_KEY']}) | ||
| response.parsed_response["members"].each do |member| | ||
| name = member["name"] | ||
| id = member["id"] | ||
| real_name = member["real_name"] | ||
| user = Slack::User.new(name, id, real_name) | ||
| users << user | ||
| end | ||
| return users | ||
| end | ||
|
|
||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #lib/workspace.rb | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| module Slack | ||
| class Workspace | ||
| attr_reader :users, :channels, :recipient | ||
| def initialize | ||
| @users = User.users_list | ||
| @channels = Channel.channels_list | ||
| @recipient= nil | ||
| end | ||
|
|
||
| def select_user(input) | ||
| found = false | ||
| @users.each do |user| | ||
| if user.name == input.downcase || user.id == input | ||
| @recipient = user | ||
| found = true | ||
| end | ||
| end | ||
|
|
||
| if found == false | ||
| puts "The provided name/id doesn't match any user" | ||
| sleep(0.5) | ||
| end | ||
| return found | ||
| end | ||
|
|
||
| def select_channel(input) | ||
| found = false | ||
| @channels.each do |channel| | ||
| if channel.name == input.downcase || channel.id == input | ||
| @recipient = channel | ||
| recipient | ||
| found = true | ||
| break | ||
| end | ||
| end | ||
| if found == false | ||
| puts "The provided name/id doesn't match any channel" | ||
| sleep(0.5) | ||
| end | ||
| return found | ||
| end | ||
|
|
||
| def show_details | ||
| details = "" | ||
| if @recipient != nil | ||
| if @recipient.class == Slack::User | ||
| details = "Name: #{@recipient.name}, Id: #{@recipient.id}, Real name: #{@recipient.real_name}\n\n" | ||
| else | ||
| details = "Name: #{@recipient.name}, Id: #{@recipient.id}, Topic: #{@recipient.topic}, Member count: #{@recipient.member_count}\n\n" | ||
| end | ||
| else | ||
| details = "No recipient is currently selected." | ||
| end | ||
| return details | ||
| 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.
and here! Doing this sort of double inclusion causes warnings like this:
/Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old red /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of red was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old green /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of green was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old yellow /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of yellow was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old blue /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of blue was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old cyan /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of cyan was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/colorize-0.8.1/lib/colorize/class_methods.rb:97: warning: method redefined; discarding old white /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/string.rb:19: warning: previous definition of white was here /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/column.rb:13: warning: method redefined; discarding old name= /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/row_group.rb:224: warning: shadowing outer local variable - value /Users/devin/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/returnable.rb:27: warning: mismatched indentations at 'end' with 'def' at 25 /Users/devin/Documents/Ada/c12/slack-cli/test/message_test.rb:20: warning: assigned but unused variable - responseWhich are messy and make it hard to debug, but also often hint at bigger problems with code.