4
\$\begingroup\$

Because I'm both lazy and wanting to mess around more with time management and Ruby, I thought it would be fun to create a simple script that opens Code Review once every day to go towards the daily login badge awards! There are so many ways to go about this that I'm certain you could probably do this within several lines of code (especially with Ruby).

require 'launchy'

module BadgeBot
  def at_time(time)
    loop do
      before = Time.now
      yield
      interval = time - (Time.now - before)
      sleep interval if interval > 0
    end
  end

  # every 23 hours, open the webpage
  at_time(82800) do
    Launchy.open('http://codereview.stackexchange.com/')
  end
end

I'm also aware that there's no code to handle closing the browser. This is handled manually, because as a software developer I'd be ashamed if I didn't attend the computer once at some point in the day.

\$\endgroup\$
2
  • \$\begingroup\$ +1 for cheating the system by automating it. What happens if your cache gets reset though? You would open the page without being logged in. \$\endgroup\$
    – RubberDuck
    Commented Sep 24, 2015 at 11:38
  • \$\begingroup\$ I just log in again. Like I said, I'd be ashamed if I didn't attend my computer at some point in the day :P \$\endgroup\$
    – T145
    Commented Sep 25, 2015 at 0:24

2 Answers 2

4
\$\begingroup\$

Naming the method at_time doesn't really tell the user what it's doing. If you used ActiveSupport::CoreExtensions::Numeric::Time, you could do something really cool:

every 23.hours do
  # job
end

Some other name might be better, like verbose repeat_with_interval, but every + hours is terse and readable, thus nice. Also, if you are using a module, you could separate definitions (definition, actually) from actual job for clarity - it is good to separate what is being done from how it is done (if BadgeBot contained more methods, body of the module probably isn't where someone would look for method calls).

module BadgeBot
  def every(time)
    # ...
  end
end

BadgeBot.every 23.hours do
  # cheat shamelessly
end
\$\endgroup\$
3
  • \$\begingroup\$ Is there something that does the same thing that isn't ActiveSupport? I'm ok with using it, it's just that its main environment is Rails, and I like separating my dependencies out for what I need specifically. I haven't dug into it yet, but I'm guessing it has a lot of peripheral stuff I don't need. \$\endgroup\$
    – T145
    Commented Sep 25, 2015 at 0:26
  • \$\begingroup\$ I think you could require just numeric/time, like active_support/core_ext/numeric/time. \$\endgroup\$
    – Borsunho
    Commented Sep 25, 2015 at 6:07
  • \$\begingroup\$ @T145 The gem would still be installed with all 'peripherals', but as you can require specific part, there is very little reason to avoid this (download size, maybe...?). I'd recommend sticking with ActiveSupport. \$\endgroup\$
    – Borsunho
    Commented Sep 25, 2015 at 8:44
4
\$\begingroup\$

It's good.

I would make the time_to_wait a constant instead of a number and write it as TIME_TO_WAIT = 23 * 60 * 60

or much more clearly but also more verbosely:

def hours_to_seconds(hours)
  hours * 60 * 60
end

And then

TIME_TO_WAIT = hours_to_seconds(23)

And

sleep interval if interval > 0

^ Looks like premature optimization, sleeping 0 just does nothing, so you may just use

sleep interval

Which advantages does module give you? It is sound to use a name-space for importable scripts, but this one is only ever going to be run as a stand-alone, so you may just remove it.

\$\endgroup\$
2
  • \$\begingroup\$ I had it in a module so that it would a) be easily exported as a gem that I can install on my system, and then use in any of my other gems or command line executables, and b) it just titles the program. \$\endgroup\$
    – T145
    Commented Sep 25, 2015 at 0:21
  • \$\begingroup\$ @T145 sensible points, as far as naming is concerned, you may write a badgebot function \$\endgroup\$
    – Caridorc
    Commented Sep 25, 2015 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.