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
cf13667
update test helper vcr and simplecov
indiakato Oct 6, 2020
5e8cfc9
made workspace class with initialize method
indiakato Oct 6, 2020
af96987
test printing resonse from users.list slack API
r-spiel Oct 6, 2020
fba233e
small changes, set up User Class
r-spiel Oct 6, 2020
c3077ca
add .idea/ to gitignore
r-spiel Oct 7, 2020
6392025
Added User method and related parts
r-spiel Oct 7, 2020
13c6c31
get all users + user test
indiakato Oct 7, 2020
068e8d7
added channel_test file and 1 test for Channel Class and minor change…
r-spiel Oct 7, 2020
7e934e0
added channel class and tests for channel
indiakato Oct 7, 2020
9eb1c83
Added username and id as attributes in user class
indiakato Oct 7, 2020
d09c02d
Adding main code for getting choice/printing results
r-spiel Oct 7, 2020
fbfa3c9
added comment
r-spiel Oct 7, 2020
3c904ff
minor reorder in User class attributes, finish printing users list & …
r-spiel Oct 7, 2020
7486479
adding printing for list of channels & minor change to order of Chann…
r-spiel Oct 7, 2020
b207eb4
added comments and tried table print
indiakato Oct 7, 2020
8c2bb7a
call API in recipient class, list_all abstract class, list_all in use…
indiakato Oct 7, 2020
8943a7a
add inheritance for User & Channel classes, update tests for .list_al…
r-spiel Oct 7, 2020
376426a
select user and select channel methods
indiakato Oct 7, 2020
e3a0142
added workspace test file & tests
r-spiel Oct 8, 2020
089fea0
add tests for select_channel
indiakato Oct 8, 2020
6f1ae30
add cassettes
indiakato Oct 8, 2020
03b62a7
add details method to recipient, user and channel, updated slack.rb t…
indiakato Oct 8, 2020
5890086
added test for show details method, added attribute @selected to Work…
r-spiel Oct 8, 2020
faf2b63
Added send_message method to workspace & recipient classes
r-spiel Oct 8, 2020
41f1836
added send_message test in workspace, put send message option in driv…
indiakato Oct 8, 2020
2b6472e
add helper methods to slack.rb, add method for filling empty string f…
r-spiel Oct 9, 2020
5b6bc2c
add error raise in recipient class, in slack.rb got the table printin…
r-spiel Oct 9, 2020
ea7c181
deleted some white space
r-spiel Oct 9, 2020
182a9a3
took out recipient variable and used workspace.selected instead
indiakato Oct 9, 2020
0d93bf5
refactor in slack.rb, in recipient class change @name to @slack_id to…
r-spiel Oct 9, 2020
17a9fc6
added slackapierror test
indiakato Oct 9, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

# Ignore environemnt variables
.env
.idea/

Choose a reason for hiding this comment

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

Thank you! However also please add coverage, it makes for a smaller PR.

Suggested change
.idea/
.idea/
coverage

130 changes: 130 additions & 0 deletions cassettes/channel_instances.yml

Large diffs are not rendered by default.

670 changes: 670 additions & 0 deletions cassettes/send_message_data.yml

Large diffs are not rendered by default.

516 changes: 516 additions & 0 deletions cassettes/user_instances.yml

Large diffs are not rendered by default.

607 changes: 607 additions & 0 deletions cassettes/work_space_data.yml

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'awesome_print'
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
empty_topic
end

def self.list_all
base_url = "https://slack.com/api/conversations.list"

response = self.get(base_url)

Choose a reason for hiding this comment

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

Good helper method


return response["channels"].map do |channel|
Channel.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"] )
end
end

def details
return "This channel's id is: #{@slack_id}, the name is: #{@name}. This channel has #{@member_count} members and is about #{@topic}"
end

private
def empty_topic
if @topic == ""
@topic = "N/A"
end
end
end
45 changes: 45 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class SlackApiError < StandardError; end

class Recipient

Choose a reason for hiding this comment

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

Great abstract class! 😍

attr_reader :slack_id, :name

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

def send_message(message)
url = "https://slack.com/api/chat.postMessage"

response = HTTParty.post(url, body: {
token: ENV["SLACK_TOKEN"],
text: message,
channel: @slack_id
}
)
unless response.code == 200 && response.parsed_response["ok"]
raise SlackApiError, "Error when posting #{message} to #{@name}, error: #{response.parsed_response["error"]}"
end
return response.code == 200 && response.parsed_response["ok"]
end

def self.get(url)
response = HTTParty.get(url, query: {
token: ENV["SLACK_TOKEN"]
}
)
unless response.code == 200 && response.parsed_response["ok"]
raise SlackApiError, "Error connecting to Slack API: #{response.code} - #{response.parsed_response["error"]} - url: #{url}, please try again later."
end
return response
end

def self.list_all
raise NotImplementedError
end

def self.details
raise NotImplementedError
end

end
67 changes: 65 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
#!/usr/bin/env ruby
require_relative 'workspace'
require_relative 'user'
require_relative 'channel'
require 'table_print'
require 'httparty'
require 'dotenv'
Dotenv.load

def print_options
puts "Your options are: \n1. List users \n2. List channels \n3. Select User \n4. Select Channel \n5. Details \n6. Send message \n7. Quit \n\nPlease enter the number of your choice: "
end

def main_select_user(workspace)
puts "Please enter the id or name of the user you seek:"
workspace.select_user(gets.chomp)
if workspace.selected.nil?
puts "User not found"
else
puts "User selected: #{workspace.selected.name}"
end
return workspace.selected
end

def main_select_channel(workspace)
puts "Please enter the id or name of the channel you seek:"
workspace.select_channel(gets.chomp)
if workspace.selected.nil?
puts "Channel not found"
else
puts "Channel selected: #{workspace.selected.name}"
end
return workspace.selected
end

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
puts "There are #{workspace.channels.length} channels, and #{workspace.users.length} users."

print_options
choice = gets.chomp.downcase

# TODO project
until choice == "7" || choice == "quit"
case choice
when "1", "list users"
tp workspace.users, :name, :slack_id, :real_name
when "2", "list channels"
tp workspace.channels, :name, :slack_id, :topic, :member_count
when "3", "select user"
main_select_user(workspace)
when "4", "select channel"
main_select_channel(workspace)
when "5", "details"
workspace.selected.nil? ? (puts "No user or channel selected"): (puts workspace.show_details)
when "6", "send message"
if workspace.selected.nil?
puts "A valid user or channel has not been selected."
else
puts "Please enter message to post:"
message = gets.chomp
workspace.send_message(message)
end
else
puts "not a valid choice, please try again"
end
puts ""
print_options
choice = gets.chomp.downcase
end

puts "Thank you for using the Ada Slack CLI"
puts "Thank you for using India & Richelle's Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME
39 changes: 39 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'awesome_print'
require_relative 'recipient'

class User < Recipient
attr_reader :real_name, :status_emoji, :status_text

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

def self.list_all
base_url = "https://slack.com/api/users.list"

response = self.get(base_url)

return response["members"].map do |user|
User.new(user["id"], user["name"], user["profile"]["real_name"], user["profile"]["status_text"], user["profile"]["status_emoji"] )
end
end

def details
return "This user's id is: #{@slack_id}, the username is: #{@name} and their real name is: #{@real_name}. They have a status emoji of: #{@status_emoji} and their status text is: #{@status_text}"
end

private
def empty_status
if @status_emoji == ""
@status_emoji = "N/A"
end

if @status_text == ""
@status_text = "N/A"
end
end
end
41 changes: 41 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'user'
require_relative 'channel'

class Workspace
attr_reader :users, :channels
attr_accessor :selected

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

def select_user(input)
@selected = @users.find { |user| user.name == input }
if @selected
return @selected
else
@selected = @users.find { |user| user.slack_id == input }
end
return @selected
end
Comment on lines +14 to +22

Choose a reason for hiding this comment

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

Notice that select user and channel do the same thing, just to different arrays, can you make a helper method?


def select_channel(input)
@selected = @channels.find { |channel| channel.name == input }
if @selected
return @selected
else
@selected = @channels.find { |channel| channel.slack_id == input }
end
return @selected
end

def show_details
@selected.details
end

def send_message(message)
@selected.send_message(message)
end
end
17 changes: 17 additions & 0 deletions test/channel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'test_helper'

describe "Channel class" do

Choose a reason for hiding this comment

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

You should also have tests for the attr_reader attributes for channels, and the constructor.

it "channel list all method" do

VCR.use_cassette("channel_instances") do

all_channels = Channel.list_all

expect(all_channels).must_be_instance_of Array

all_channels.each do |channel|
expect(channel).must_be_instance_of Channel
end
end
end
end
5 changes: 5 additions & 0 deletions test/coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": {
"covered_percent": 100.0
}
}
7 changes: 7 additions & 0 deletions test/coverage/.resultset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Unit Tests": {
"coverage": {
},
"timestamp": 1602005599
}
}
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/coverage/assets/0.12.3/application.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions test/coverage/assets/0.12.3/application.js

Large diffs are not rendered by default.

Binary file added test/coverage/assets/0.12.3/colorbox/border.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/colorbox/controls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/colorbox/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/favicon_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/favicon_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/favicon_yellow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/coverage/assets/0.12.3/loading.gif
Binary file added test/coverage/assets/0.12.3/magnify.png
93 changes: 93 additions & 0 deletions test/coverage/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Code coverage for Test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src='./assets/0.12.3/application.js' type='text/javascript'></script>
<link href='./assets/0.12.3/application.css' media='screen, projection, print' rel='stylesheet' type='text/css' />
<link rel="shortcut icon" type="image/png" href="./assets/0.12.3/favicon_green.png" />
<link rel="icon" type="image/png" href="./assets/0.12.3/favicon.png" />
</head>

<body>
<div id="loading">
<img src="./assets/0.12.3/loading.gif" alt="loading"/>
</div>
<div id="wrapper" class="hide">
<div class="timestamp">Generated <abbr class="timeago" title="2020-10-06T10:33:19-07:00">2020-10-06T10:33:19-07:00</abbr></div>
<ul class="group_tabs"></ul>

<div id="content">
<div class="file_list_container" id="AllFiles">
<h2>
<span class="group_name">All Files</span>
(<span class="covered_percent">
<span class="green">
100.0%
</span>

</span>
covered at
<span class="covered_strength">
<span class="red">
0.0
</span>
</span> hits/line
)
</h2>

<a name="AllFiles"></a>

<div>
<b>0</b> files in total.
</div>

<div class="t-line-summary">
<b>0</b> relevant lines,
<span class="green"><b>0</b> lines covered</span> and
<span class="red"><b>0</b> lines missed. </span>
(<span class="green">
100.0%
</span>
)
</div>



<div class="file_list--responsive">
<table class="file_list">
<thead>
<tr>
<th>File</th>
<th class="cell--number">% covered</th>
<th class="cell--number">Lines</th>
<th class="cell--number">Relevant Lines</th>
<th class="cell--number">Lines covered</th>
<th class="cell--number">Lines missed</th>
<th class="cell--number">Avg. Hits / Line</th>

</tr>
</thead>
<tbody>

</tbody>
</table>
</div>
</div>



</div>

<div id="footer">
Generated by <a href="https://github.com/simplecov-ruby/simplecov">simplecov</a> v0.19.0
and simplecov-html v0.12.3<br/>
using Unit Tests
</div>

<div class="source_files">

</div>
</div>
</body>
</html>
Loading