Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c96e696
set up environment, added classes for recipient, user, and channel. A…
Sep 10, 2019
d6b04bd
ran into weird errors, pushing so dani can pull.
Sep 10, 2019
0300de9
Finished list of users and channels
dnsanche Sep 11, 2019
62092a6
created the logic for user. created workspace.rb.
Sep 12, 2019
bdaced2
Pre-Cloudy
dnsanche Sep 12, 2019
93cf8bb
Pre Cloudy
dnsanche Sep 12, 2019
3209db1
working driver code. and command line stuff
Sep 12, 2019
a6f7fd4
Updates to slack.rb and workspace.rb
dnsanche Sep 12, 2019
c3c746f
working on main.
Sep 12, 2019
13a9df3
pushing and pulling
Sep 12, 2019
214cb9c
still working on printing all correct properties. almost there.
Sep 12, 2019
83702a2
Same as Cloudy
dnsanche Sep 12, 2019
21e8578
same as Cloudy
dnsanche Sep 13, 2019
a6d48e8
Fixed driver code loop. Removed recursive loop
dnsanche Sep 13, 2019
3e442c7
Added option to enter numbers on the menu, and to slow down what is p…
dnsanche Sep 13, 2019
5400aa6
Finished driver code wave 2
dnsanche Sep 13, 2019
bf99630
Finished setting up sending a message to the channel
dnsanche Sep 13, 2019
66654ed
created message to user
dnsanche Sep 13, 2019
da83a78
Message working
dnsanche Sep 13, 2019
099722c
accepting all changes
Sep 13, 2019
6d125e8
updated all changes
Sep 13, 2019
bb9ccde
started testing for users and channels. all tests pass
Sep 13, 2019
a51f1f5
added more tests. whoohooo,
Sep 13, 2019
b0ffc4c
completing testing, incoming weird errors, were trying to work out th…
Sep 13, 2019
30148ba
Added VCR and finshed some tests in workspace
Sep 13, 2019
5f33fb1
going to update token. have deleted it as its now disabled
Sep 14, 2019
30d5654
Added workspace again
dnsanche Sep 14, 2019
7876ed7
Added the cassetes we lost due token publishing
dnsanche Sep 14, 2019
99c9d3b
Finished tests for details
dnsanche Sep 14, 2019
26ea133
Finished all tests
dnsanche Sep 14, 2019
f4097e6
Added additional test to improve coverage
dnsanche Sep 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/tmp/

# Used by dotenv library to load environment variables.
# .env
.env

## Specific to RubyMotion:
.dat*
Expand Down
34 changes: 34 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#lib/channel.rb
require 'httparty'
require 'awesome_print'

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 - response

Which are messy and make it hard to debug, but also often hint at bigger problems with code.

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})

Choose a reason for hiding this comment

The 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 list method for both channels and users that you implement in recipient, which could call a method that actually parses the response appropriately, similar to what csv_record did in OO ride share

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
25 changes: 25 additions & 0 deletions lib/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#lib/message.rb

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really a message class, it's a slackapi_wrapper class. This holds a bunch of information about how to contact the slack API

require "httparty"

class SlackApiError < StandardError ; end

module Slack
def self.send_msg(message, channel)

Choose a reason for hiding this comment

The 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
10 changes: 10 additions & 0 deletions lib/recipient.rb
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're writing a class this short, ask yourself 2 questions:

  1. If it's this short, do I need it?
  2. If it's needed, is someone else doing its job?

end
end
end
66 changes: 61 additions & 5 deletions lib/slack.rb
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"

Choose a reason for hiding this comment

The 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:

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Expand Down
30 changes: 30 additions & 0 deletions lib/user.rb
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
63 changes: 63 additions & 0 deletions lib/workspace.rb
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
84 changes: 84 additions & 0 deletions test/cassettes/Channel_instantiation.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading