-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Improvements #9
Conversation
Parsing plain text files requires more processing than structured file types like YAML / JSON. YAML support is built-into ruby > 1.9.3. The YAML module can easily load a JSON file too. Why was the year field removed? Birthdays are recurring. You need to wish people every year. Why would you want to torment people with the notion of them getting old? 😜
because that is what it is - an example / placeholder configuration file
Supports multiple birthdays from arrays. Earlier version sent multiple wishes for multiple birthdays, this version appends the users to a single array and sends out a single birthday wish message.
You can now configure the birthday database with your team memebers' usernames or real names. The bot will mention the users based on this parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shinenelson Awesome PR ! :) The birthday settings with json/yaml is a really nice addition.
Left a couple of comments for you to take a loo.
lib/birthday_reader.rb
Outdated
birthdays << line.chomp.split | ||
end | ||
file.close | ||
bday_list = YAML.load_file(file_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this class is to read birthdays only, I don't think we should return only the birthdays according to Time.now
, instead we should return all of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary reason I switched to a structured format for the database was to drill down to the exact date to fetch all the birthdays on the day. What would be the use case to have return all the birthdays together? It would just use more memory (for no particular gain, whatsoever) IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not against the structuring.
What I am saying is that the purpose of the function is to read birthdays. It is not to read and filter. That is why I think we should not filter the birthdays here.
And because we are just managing strings of birthdays I guess memory is not a concern.
configurations.json
Outdated
@@ -1,4 +1,5 @@ | |||
{ | |||
"db_path": "birthdays.yaml", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense to change db_path
to birthdays_path
lib/birthday_bot.rb
Outdated
username: @config.bot_name, | ||
text: message, | ||
icon_emoji: @config.bot_emoji }.to_json) | ||
puts "Checking who was born today" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 cron
s 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.
lib/birthday_bot.rb
Outdated
users += ", <@#{ birthdays[i] }>" | ||
end | ||
users += " and <@#{ birthdays[i+1] }> " | ||
end | ||
end |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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)
else | ||
name | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example where disabling mentions is useful ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The earlier version of the database stored the real names of users. The bot wished users with their real names. So, I thought that could be a valid use-case
But like my commit message said, some people might prefer wishing their team-mates by their real names (probably for more family-togetherness) and/or not choosing to spam the users with mentions on the wishes (but that wil probably be taken care of by other team-mates, anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks !
The YAML module parses keys with a leading 0 as octcal digits, invalidating 08 and 09. Parsing them as strings would be safer
I'll leave a friendly reminder here that this pull request is still un-merged. Also, I've handled 2 new issues that I came across.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shinenelson Sorry for the late reply I didn't get any notifications with updates on this.
I left a few extra comments for you to take a look.
Next time tag me using @jbernardo95
to make sure I see the notifications.
lib/birthday_reader.rb
Outdated
birthdays << line.chomp.split | ||
end | ||
file.close | ||
bday_list = YAML.load_file(file_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not against the structuring.
What I am saying is that the purpose of the function is to read birthdays. It is not to read and filter. That is why I think we should not filter the birthdays here.
And because we are just managing strings of birthdays I guess memory is not a concern.
lib/birthday_bot.rb
Outdated
username: @config.bot_name, | ||
text: message, | ||
icon_emoji: @config.bot_emoji }.to_json) | ||
puts "Checking who was born today ( #{ Time.now.to_s } )" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
puts "Checking who was born today (#{ Time.now.to_s })"
remove the spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt it would be more legible with the spaces.
text: message, | ||
icon_emoji: @config.bot_emoji }.to_json) | ||
else | ||
puts "Today is a day that no one was born" | ||
end |
There was a problem hiding this comment.
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
ab417b8
to
b282753
Compare
I don't know whether this repository is still in maintenance, but I had a use-case for this bot that made me come back to it. Upon navigating the repository is when I remembered that I hadn't worked on the changes that @jbernardo95 requested here. I've now made the changes that were requested. Plus, I refactored away the |
HTTParty is the only gem dependency of the bot. I thought it was redundant to maintain a single dependency especially when it was achievable with the standard net/http library except that it would some extra lines of code. I thought the extra lines of code was worth it rather than having to maintain a single dependency.
b282753
to
0596516
Compare
This is almost like a whole re-factor of the bot.
The commit messages should be self-explanatory.
Change birthdays database to YAML / JSON
The YAML module can load and parse JSON files, so that's an added benefit. I initially thought of using only JSON since the configuration file was JSON too. But since YAML is a more easier-to-understand format for everyone, so I thought I'd add support for that as well.
Configurize
db_path
andmention
birthdays.txt
was being hard-coded and loaded from the code. This makes the bot less-flexible in terms of the database filename. Since I was re-factoring the code, I thought why not. Configurizing the database filename would make it more flexible. But the database file should still be parse-able by the YAML module, so that's there.mention
flag istrue
, the purpose is defeated.Renaming
configurations.json
toconfigurations.json.example
- because that's what it is, only an example placeholder file, not an actual configuration file.The bot has been optimized to work with the above changes and the README file has been updated with more information with regard on how to configure the bot and use it.