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

Improvements #9

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
29 changes: 20 additions & 9 deletions lib/birthday_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@ def initialize()

def start!
birthdays = BirthdayReader.get_birthdays(@config.db_path)
today = Time.now

puts "Checking who was born today (#{today.to_s})"
birthdays.each do |b|
if (b[3].to_i == today.month) && (b[4].to_i == today.day)
message = "#{@config.greeting_message} #{b[0]} #{b[1]}"
HTTParty.post(@config.slack_url, body: { channel: @config.channel_name,
username: @config.bot_name,
text: message,
icon_emoji: @config.bot_emoji }.to_json)
puts "Checking who was born today"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should keep Time.now in the log

Copy link
Author

Choose a reason for hiding this comment

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

Ah, you wanted it in the log. The way I handle my crons are, I prepend date before my executing command. But I don't know if this works with Heroku's scheduler (does it?). Else I'll just put the date back in the log.

unless birthdays.nil?
users = "<@#{ birthdays[0] }>"
if birthdays.count > 1
puts "#{ birthdays.count } people were born today"
if birthdays.count == 2
users = " <@#{ birthdays[0] }> and <@#{ birthdays[1] }> "
else
for i in 1..birthdays.count-2
users += ", <@#{ birthdays[i] }>"
end
users += " and <@#{ birthdays[i+1] }> "
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can simplify this:

puts "#{birthdays.count} were born today"

today = Time.now
birthdays_today = birthdays[today.month][today.day]

unless birthdays_today.empty?
  users = build_user_list(birthdays_today)
  message = "#{users} #{@config.greeting_message}"
  HTTParty.post(@config.slack_url, body: { channel: @config.channel_name,
    username: @config.bot_name,
    text: message,
    icon_emoji: @config.bot_emoji }.to_json)
end
 
# ...

def build_user_list(birthdays)
  if birthdays.size == 1
    birthdays.first
  else
    users = birthdays.take(birthdays.count - 1).join(", ")
    users += " and #{birthdays[birthdays.count - 1]}" if birthdays.count > 1
  end
end

What do you think ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@shinenelson Did you take a look at this ?

(mentions are missing here)

message = "#{users} #{@config.greeting_message}"
HTTParty.post(@config.slack_url, body: { channel: @config.channel_name,
username: @config.bot_name,
text: message,
icon_emoji: @config.bot_emoji }.to_json)
else
puts "Today is a day that no one was born"
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you looked at my previous comment with a suggestion to refactor this function ?

puts "#{birthdays.count} were born today"

today = Time.now
birthdays_today = birthdays[today.month][today.day]

unless birthdays_today.empty?
  users = build_user_list(birthdays_today)
  message = "#{users} #{@config.greeting_message}"
  HTTParty.post(@config.slack_url, body: { channel: @config.channel_name,
    username: @config.bot_name,
    text: message,
    icon_emoji: @config.bot_emoji }.to_json)
end
 
# ...

def build_user_list(birthdays)
  if birthdays.size == 1
    birthdays.first
  else
    users = birthdays.take(birthdays.count - 1).join(", ")
    users += " and #{birthdays[birthdays.count - 1]}" if birthdays.count > 1
  end
end

end
end