Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
4b2d502
set up the files
Bitaman Mar 19, 2019
f502ab6
created classes and shells for methods in each file
gracemshea Mar 19, 2019
8c83fc0
added base_urls and tokens to recipient, user, and channel
gracemshea Mar 19, 2019
3f9786f
added httparty. refined planned tests for user_spec
gracemshea Mar 19, 2019
ff7f7a2
initial menu drafted for slack.rb
gracemshea Mar 19, 2019
145d312
added require relative to specs, channel params added to channel.rb
gracemshea Mar 19, 2019
e9f005c
channel self.list somewhat working! returns not_authed
gracemshea Mar 19, 2019
af7056c
auth issue overcome by moving .env from specs to lib
gracemshea Mar 19, 2019
7bb84a6
use_casette tests missing parentheses, added
gracemshea Mar 20, 2019
6569872
get the list of users and wrote the VCR for that, it's passing
Bitaman Mar 20, 2019
604fd81
made changes to user
Bitaman Mar 20, 2019
24c7fd2
tried to merge
Bitaman Mar 20, 2019
b8d8051
channel list functioning
gracemshea Mar 20, 2019
9258eb5
merging
Bitaman Mar 20, 2019
4e04cc4
trying to get changed files
Bitaman Mar 20, 2019
2742dd9
channel tests added, debugging
gracemshea Mar 20, 2019
390fedb
refining channel_list output, created workspace files
gracemshea Mar 20, 2019
093b250
workspace tests evolving
gracemshea Mar 20, 2019
fe50b30
resolved HTTPS error
gracemshea Mar 20, 2019
8f059c8
list methods tests/methods refined. passing those tests.
gracemshea Mar 21, 2019
1a3732a
added the detail method to user
Bitaman Mar 21, 2019
a72b53a
had to make changes to user and recipient to make it work
Bitaman Mar 21, 2019
32350f2
user spec finished
gracemshea Mar 22, 2019
d6de455
slack menu completed
gracemshea Mar 22, 2019
21ee33b
adding tests to workspace spec, resolving errors
gracemshea Mar 22, 2019
eef74a4
trying to merge
Bitaman Mar 22, 2019
5cc917a
user_spec edited, passes test
gracemshea Mar 22, 2019
d54ba41
tests refined
gracemshea Mar 22, 2019
8b4e25b
initialized workspace in each describe block
gracemshea Mar 22, 2019
0304ab5
error corrected
gracemshea Mar 22, 2019
855498a
debugging update
gracemshea Mar 22, 2019
47933a2
debugging joy
gracemshea Mar 22, 2019
4dd4e61
syntax error fixed
gracemshea Mar 22, 2019
55e891f
checked the user and channel class
Bitaman Mar 22, 2019
2cc177f
working through workspace_spec issues
gracemshea Mar 22, 2019
017c3b1
marge attempt
gracemshea Mar 22, 2019
18a0e8a
push
gracemshea Mar 22, 2019
a420ef7
merging
Bitaman Mar 22, 2019
04f1abd
Merge branch 'master' of https://github.com/gracemshea/slack-cli
Bitaman Mar 22, 2019
a2c704d
select_user and select_channel by name functional
gracemshea Mar 22, 2019
1d870f8
merging
Bitaman Mar 22, 2019
9e05aaf
print_details passes test
gracemshea Mar 22, 2019
9cb2224
show_details method passing tests
gracemshea Mar 22, 2019
b454049
Merge
Bitaman Mar 22, 2019
282dd5c
slack menu debugged
gracemshea Mar 22, 2019
d35ea14
fixing the slack
Bitaman Mar 22, 2019
478bc3b
merging
Bitaman Mar 22, 2019
2281f75
send message test passing
gracemshea Mar 22, 2019
577f10c
fixing send message
Bitaman Mar 22, 2019
501080d
for sharing
gracemshea Mar 22, 2019
a78220e
merge attempt
gracemshea Mar 22, 2019
f2bd699
reversed a merge error
gracemshea Mar 22, 2019
0db833f
raises errors for invalid channel and user
gracemshea Mar 22, 2019
cfce3ba
passing to bita
gracemshea Mar 22, 2019
0151a13
removing slac spec and recipient spec
Bitaman Mar 22, 2019
791d92a
added status text and imoji to user
Bitaman Mar 29, 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 @@ -53,4 +53,4 @@ build-iPhoneSimulator/
.env

# Ignore cassette files
/specs/cassettes/
/specs/cassette
38 changes: 38 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "recipient"

class Channel < Recipient
attr_reader :topic, :member_count

def initialize(slack_id, name, topic, member_count)
super(slack_id, name)
@topic = topic
@member_count = member_count
end

def self.list
raw_data = self.get("channel")

unless raw_data.code == 200
raise SlackApiError, "Improper request: #{raw_data.message}"
end
channel_list = []
channels = raw_data["channels"]

channels.each do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["topic"]["value"]
member_count = channel["members"].count

new_channel = Channel.new(slack_id, name, topic, member_count)
channel_list << new_channel
end
return channel_list
end

def details
return "#{name} #{topic} member count: #{member_count} slack id: #{slack_id}"
end
end

# puts Channel.list
61 changes: 61 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "dotenv"
require "httparty"
Dotenv.load

class Recipient
class SlackApiError < StandardError; end

attr_accessor :slack_id, :name

def initialize(slack_id, name)
@slack_id = slack_id
@name = name
end

CHANNEL_URL = "https://slack.com/api/channels.list"
USER_URL = "https://slack.com/api/users.list"
POST_URL = "https://slack.com/api/chat.postMessage"

Choose a reason for hiding this comment

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

Ideally the superclass would not need to know these details from the subclass. One of the benefits of inheritance is it allows you to add new subclasses without changing the parent class.

To do so, instead of using a constant for this, you could define a template method list_url which is overridden in the child classes to return the correct endpoint.

That would also allow you to get rid of the if statement in self.get below.


def self.get(type)
params = {
"token" => ENV["SLACK_TOKEN"],
}
if type == "user"
url = USER_URL
elsif type == "channel"
url = CHANNEL_URL
end

response = HTTParty.get(url, query: params)
return response
end

def send_msg(message)
params = {
"token" => ENV["SLACK_TOKEN"],
"channel" => @slack_id,
"text" => message,
"as_user" => true,
}

response = HTTParty.post(
POST_URL,
body: params,
headers: { "Content-Type" => "application/x-www-form-urlencoded" },
)
unless response.code == 200 && response.parsed_response["ok"]
raise SlackApiError, "Error: #{response.parsed_response["error"]}"
end
return response
end

private

def details
raise NotImplementedError
end

def self.list
raise NotImplementedError
end
end
56 changes: 54 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
#!/usr/bin/env ruby
require_relative "workspace"

def main
puts "Welcome to the Ada Slack CLI!"

# TODO project
workspace = Workspace.new
input = ""

while input != "quit"
puts "Choose an option:
\n 1. List users
\n 2. List channels
\n 3. Select user
\n 4. Select channel
\n 5. Get details
\n 6. Send message
\n 7. Quit"

input = gets.chomp.downcase
case input
when "list users"
puts workspace.show_details("users")
when "list channels"
puts workspace.show_details("channels")

Choose a reason for hiding this comment

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

These should be calling print_details, not show_details

when "select user"
print "Enter the user name or Slack ID: "
input_user = gets.chomp
workspace.select_user(input_user)
if workspace.selected == nil
puts "User not found"
end
when "select channel"
print "Enter the channel name or Slack ID: "
input_channel = gets.chomp
workspace.select_channel(input_channel)
if workspace.selected == nil
puts "Channel not found"
end
when "details"
if workspace.selected == nil
puts "Please select a user or channel."
else
puts workspace.show_details
end
when "send message"

Choose a reason for hiding this comment

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

You have the wrong number of arguments to show_details here.

if workspace.selected == nil
puts "Please select a user or channel."
else
print "Enter your message: "
text = gets.chomp
workspace.send_message(text)
end
when "quit"
else
puts "Please select a option from the menu."
end
end

puts "Thank you for using the Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
42 changes: 42 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "httparty"
require "pry"
require_relative "recipient"
require "dotenv"
Dotenv.load

class User < Recipient
attr_reader :real_name, :name, :slack_id, :status_text, :status_imoji

def initialize(slack_id, name, real_name, status_text, status_imoji)
super(slack_id, name)
@real_name = real_name
@slack_id = slack_id
@name = name
@status_text = status_text
@status_imoji = status_imoji
end

def self.list
raw_data = self.get("user")

unless raw_data.code == 200
raise SlackApiError, "Improper request: #{raw_data.message}"
end
members_list = []
members = raw_data["members"]
members.each do |member|
slack_id = member["id"]
name = member["name"]
real_name = member["real_name"]
new_member = User.new(slack_id, name, real_name)
members_list << new_member

Choose a reason for hiding this comment

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

You are missing two arguments in the call to User.new here

end
return members_list
end

def details
return "#{real_name}, slack user name: #{name}, slack_id: #{slack_id}"
end
end

# puts User.list
52 changes: 52 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require_relative "user"
require_relative "channel"

class Workspace
attr_reader :users, :channels, :selected

def initialize
@users = User.list
@channels = Channel.list
@selected = nil
end

# modify this
def select_channel(user_input)
selected = channels.select do |channel|
channel.name == user_input || channel.slack_id == user_input
end
@selected = selected.first

Choose a reason for hiding this comment

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

Ruby's .find enumerable does the same thing as .select.first.

end

def select_user(user_input)
@selected = users.select do |user|
user.name == user_input || user.slack_id == user_input
end
@selected = selected.first
end

# used to show details for selected user or channel
def show_details(selected)
return @selected.details
end

Choose a reason for hiding this comment

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

Do you need the parameter here? You're not using it, and your CLI code doesn't expect to supply it.


# used to show details for all users or all channels
def print_details(recipients)
if recipients == "users"
return_array = []
users.each do |user|
return_array << user.details
end
elsif recipients == "channels"
return_array = []
channels.each do |channel|
return_array << channel.details
end
end
return return_array
end

def send_message(text)
@selected.send_msg(text)
end
end
Binary file added specs/.DS_Store
Binary file not shown.
78 changes: 78 additions & 0 deletions specs/cassettes/get_response.yml

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

Loading