Conversation
…elected before calling message or details
slack.rbWhat We're Looking For
|
| response = HTTParty.post(url, body: params) | ||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackApiError, response["error"] | ||
| end |
There was a problem hiding this comment.
Line 21 raises an exception, but not the one you want.
select user
You chose to select a user. Please provide a username or Slack ID
dan
Traceback (most recent call last):
5: from lib/slack.rb:105:in `<main>'
4: from lib/slack.rb:40:in `main'
3: from lib/slack.rb:40:in `new'
2: from /Users/droberts/Ada/c11/projects/slack-cli/lib/workspace.rb:11:in `initialize'
1: from /Users/droberts/Ada/c11/projects/slack-cli/lib/user.rb:16:in `list'
/Users/droberts/Ada/c11/projects/slack-cli/lib/recipient.rb:33:in `get': uninitialized constant Recipient::SlackApiError (NameError)
Ruby doesn't know what a SlackApiError is, so you get a NameError when you try to raise one. Looks like you're missing require_relative 'apierror' at the top of this file.
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| raise ArgumentError if !name.is_a? String | ||
| @name = name |
There was a problem hiding this comment.
Best practice is to avoid explicit type checking in a dynamic language like Ruby. This is for two reasons. First, if name really needs to be a String, then we'll get an error soon enough one way or the other. Second, if the user passes in something that close enough to a string to work (i.e. implements the String interface), there's no reason for our code to fail.
| def select_channel | ||
| channel_selected = channels.detect do |channel| | ||
| channel.slack_id == selected || channel.name == selected | ||
| end |
There was a problem hiding this comment.
This code is very similar to the code for select_user above. Could you DRY this up somehow?
| def send_message(message, recipient) | ||
| params = {} | ||
| params[:text] = message | ||
| params[:channel] = recipient.slack_id |
There was a problem hiding this comment.
Instead of taking the recipient as an argument here, you should send the message to whatever is saved in @selected. Similarly for details below.
| user_selected = users.detect do |user| | ||
| user.slack_id == selected || user.name == selected | ||
| end | ||
| return user_selected |
There was a problem hiding this comment.
Good use of the .detect enumerable here.
Since this doesn't save the user in @selected, I'm confused what that instance variable is for.
| it "returns nil if username or slack id is not found" do | ||
| VCR.use_cassette("check nil returned") do | ||
| selected = "chewy" #id of nonexistent slackbot | ||
| @workspace2 = Workspace.new(selected: selected) |
There was a problem hiding this comment.
You've done a great job of identifying and testing error cases throughout this project.
slack.rb
Congratulations! You're submitting your assignment!
You and your partner should collaborate on the answers to these questions.
Comprehension Questions