Skip to content
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

Fire: Ayesha/ Blaine - SLACK API Project Submission #23

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .floo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"url": "https://floobits.com/ayesha/slack-cli"
}
6 changes: 6 additions & 0 deletions .flooignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern
node_modules
tmp
vendor
.idea/workspace.xml
.idea/misc.xml
24 changes: 18 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ Congratulations! You're submitting your assignment. Please reflect on the assign
## Reflection
Question | Answer
--- | ---
How did you go about exploring the Slack API? Did you learn anything that would be useful for your next project involving an API? |
Give a short summary of the _request/response cycle_. Where does your program fit into that scheme? |
How does your program check for and handle errors when using the Slack API? |
How did the design and organization of your project change over time? |
Did you use any of the _inheritance idioms_ we've talked about in class? How? |
How does VCR aid in testing a program that uses an API? |
How did you go about exploring the Slack API? Did you learn anything that would be useful for your next project involving an API? | We opened a new workspace. Used postman and referenced the seven wonders assignment to understand the API content. This was 100% new concept for both of us. Will probably utilize VHS & SimpleCov in the future projects.
Give a short summary of the _request/response cycle_. Where does your program fit into that scheme? | Our Slack CLI program was a client that would make request to the API server using the HTTParty. The API server would receive our request, process our request and send a response back. With the help of Postman app, we deciphered the Server response and utilized the JSON data to interpret, manipulate, and display it to the user in our Slack CLI program.
How does your program check for and handle errors when using the Slack API? | Our program throws a SlackError and rescues it in order to keep the CLI loop going. It also gives the end user reminders to take steps in order.
How did the design and organization of your project change over time? | We began with the CLI slack.rb and a workspace.rb. From there we expanded and created classes for User and Channel.
Did you use any of the _inheritance idioms_ we've talked about in class? How? | Recipient was our abstract class. It served as the parent class to our Channel class and User class. It wasn't instantiated, but used by the child classes
How does VCR aid in testing a program that uses an API? | We used VCR to "RECORD" screenshots of the API calls that were requested. SHence, if we need to process the CLI program when the API server is down or the data changes, we have the DATA requested when the call was orignaly made and successful. It also allows for quicker and more thorough testing practices.

# What is going on in this file?
# Using HTTParty yo request info from Slack server
# Aesthetically chose pretty print/awesome_print was an option
# Requiring the .env to access out Slack Token
# Receiving a response from the server with user data
# How do we run this file?
# We run this file by using the HTTP call method
# which accesses the SLACK TOKEN to request
# the pertaining information from the Slack servers.
# What does the line workspace = Workspace.new do?
# Creates a new instance of the workspace class
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
.DS_Store

# Ignore environemnt variables
.env
.env
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

4 changes: 4 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

38 changes: 38 additions & 0 deletions .idea/slack-cli.iml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

35 changes: 35 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'recipient'
require 'prettyprint'

module Slack

class Channel < Recipient

BASE_CHANNEL_URL = 'https://slack.com/api/conversations.list'

attr_reader :topic, :num_members

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

def get_details
return "Channel Name: #{@name}, Topic: #{@topic}, Members: #{@num_members}, SlackID: #{@slack_id}"
end

def self.get_base_url
BASE_CHANNEL_URL
end

def self.get_result_key
"channels"
end

def self.record_from_hash(hash)
Channel.new(hash["id"], hash["name"], hash["topic"]["value"], hash["num_members"])
end

end
end
56 changes: 56 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'httparty'
require 'prettyprint'
require 'httparty'
require 'dotenv'


module Slack

class SlackError < StandardError; end

class Recipient

attr_reader :slack_id, :name

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

# Returns user/channel data as a hash
def self.list_all
response = self.get
hash_list = response[self.get_result_key]
hash_list.map do |hash|
self.record_from_hash(hash)
end
end

# Retrieves user/channel data
def self.get
response = HTTParty.get(self.get_base_url, query: {
token: ENV['SLACK_TOKEN'],
})
sleep(0.25)
if response.code != 200
raise SlackError, "Something went wrong: #{response.code}"
end
unless response["ok"]
raise SlackError, response["error"]
end
return response
end

def get_details
@selected.get_details
end
Comment on lines +44 to +46

Choose a reason for hiding this comment

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

I'm not sure what this is doing? Should this be a template method? @selected doesn't exist elsewhere in this class.


def post_message(message)
message = HTTParty.post("https://slack.com/api/chat.postMessage", body: {
token: ENV['SLACK_TOKEN'],
channel: @slack_id,
text: message
})
end
end
end
97 changes: 93 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,101 @@
#!/usr/bin/env ruby
require_relative 'workspace'
require 'table_print'
require 'awesome_print'

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
Dotenv.load
# Creates new workspace
workspace = Slack::Workspace.new

# TODO project
puts 'Welcome to the Ada Slack CLI!'
puts "We have #{workspace.users.length} members and #{workspace.channels.length} channels."

puts "Thank you for using the Ada Slack CLI"
loop = true
while loop
command
user_input = gets.chomp.to_s.upcase
case user_input
when 'LIST USERS'
tp workspace.list_users, "slack_id", "name", "real_name"
when 'LIST CHANNELS'
tp workspace.list_channels,"slack_id", "name", "topic", "num_members"
when 'SELECT USER'
handle_select_user(workspace)
when 'SELECT CHANNEL'
handle_select_channel(workspace)
when "DETAILS"
handle_get_details(workspace)
when 'SEND MESSAGE'
handle_send_message(workspace)
when 'QUIT'
loop = false
else
puts "You can do better. Try again. You need to select a User or Channel."
loop = true
end
end
puts 'Thank you for using the Ada Slack CLI'
end

# Command line promp
def command
puts 'Please select from the following:'
puts 'List Users'
puts 'Select User'
puts 'List Channels'
puts 'Select Channel'
puts 'Details'
puts 'Send Message'
puts 'Quit'
print 'Enter your selection here: >'
end

# Helper method to implement User Selection
def handle_select_user(workspace)
puts "Please provide a Name or Slack ID: "
print ">"
selected_user = gets.chomp
selected = workspace.find_user(selected_user)
if selected != nil
puts "We found #{selected.name}!"
else
puts "Oops, #{selected_user}, does not exist as a user. Try again!"
end
end

# Helper method to implement Channel Selection
def handle_select_channel(workspace)
puts "Please provide a Name or Slack ID: "
print ">"
selected_channel = gets.chomp
selected = workspace.find_channel(selected_channel)
if selected != nil
puts "We found #{selected.name}!"
else
puts "Oops, #{selected_channel}, does not exist as a user. Try again!"
end
end

# Helper method responsible for retrieving Selected Details
def handle_get_details(workspace)
pp workspace.get_details
rescue Slack::SlackError
puts "You need to select a User or Channel."
puts "Start again."
end

# End users send message retrieval and prompt
def handle_send_message(workspace)
begin
print "Enter the message you would like to send to #{workspace.selected.name}:>"
end_user_message = gets.chomp
selected.post_message(end_user_message)
puts "Your message has been sent!"
rescue
puts "You need to select a User or Channel."
puts "Start again."
Comment on lines +93 to +97

Choose a reason for hiding this comment

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

This piece of code never seems to send my messages, always telling me that I have to select a user or channel, and I suspect that this is because you've made an error with how you're using the keyword rescue. Here's an example how how to use recuse completely in this situation:

Suggested change
selected.post_message(end_user_message)
puts "Your message has been sent!"
rescue
puts "You need to select a User or Channel."
puts "Start again."
begin
selected.post_message(end_user_message)
rescue
puts "You need to select a User or Channel."
puts "Start again."
else
puts "Your message has been sent!"
end

end
end

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

Choose a reason for hiding this comment

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

Since you required prettyprint in recipient, you don't need to require it here ;)


module Slack

class User < Recipient

BASE_USER_URL = 'https://slack.com/api/users.list?'

attr_reader :real_name

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

def get_details
return "Username: #{@name}, Name: #{@real_name}, SlackID: #{@slack_id}"
end

def self.get_base_url
BASE_USER_URL
end

def self.get_result_key
"members"
end

def self.record_from_hash(hash)
User.new(hash["id"], hash["name"], hash["real_name"])
end

end
end
Loading