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

Conversation

shinenelson
Copy link

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 and mention

    • The 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.
    • Some people prefer wishing their team members with real names while others like mentioning them and spam them with mentions all day. Mentioning is configurable too. This too depends on what values are loaded from the database. If the database has real names and the mention flag is true, the purpose is defeated.
  • Renaming configurations.json to configurations.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.

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.
@mmachado95
Copy link
Member

@teresalazar13

Copy link
Collaborator

@jbernardo95 jbernardo95 left a 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.

birthdays << line.chomp.split
end
file.close
bday_list = YAML.load_file(file_path)
Copy link
Collaborator

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.

Copy link
Author

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.

Copy link
Collaborator

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.

@@ -1,4 +1,5 @@
{
"db_path": "birthdays.yaml",
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 it makes more sense to change db_path to birthdays_path

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.

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)

else
name
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.

Can you give an example where disabling mentions is useful ?

Copy link
Author

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).

Copy link
Collaborator

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
@shinenelson
Copy link
Author

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.

  1. YAML parses values with leading 0 as octal digits, it would be safer if we parsed the dates as strings than just integers.
  2. Handled an exception with empty array after YAML.load.

@mmachado95
Copy link
Member

@jbernardo95

Copy link
Collaborator

@jbernardo95 jbernardo95 left a 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.

birthdays << line.chomp.split
end
file.close
bday_list = YAML.load_file(file_path)
Copy link
Collaborator

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.

username: @config.bot_name,
text: message,
icon_emoji: @config.bot_emoji }.to_json)
puts "Checking who was born today ( #{ Time.now.to_s } )"
Copy link
Collaborator

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.

Copy link
Author

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
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

@shinenelson
Copy link
Author

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 gem dependency that was kind of redundant because ruby already has the net/http standard library built-in. Besides, it was the only dependency and since there's only one outbound HTTP request, it wasn't too much of work to modify the request to work with the net/http standard library.
Of course, it's extra lines of code, but I think it's worth it so that we can remove one redundant dependency ( and the Gemfile altogether ).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants