Created
May 16, 2012 20:38
-
-
Save mamantoha/2713756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
require 'date' | |
class BirthdayCountdown | |
# Cache @month, @day and @year on initialization | |
def initialize(month, day, zone = '+00:00') | |
@birthday_month = month | |
@birthday_day = day | |
@zone = zone | |
# Current date | |
@month = DateTime.now.month | |
@day = DateTime.now.day | |
@year = DateTime.now.year | |
end | |
# Is it birthday? | |
def isit? | |
@month == @birthday_month && @day == @birthday_day | |
end | |
# For the countdown | |
def seconds_to_go | |
( (next_birthday - DateTime.now) * 24 * 60 * 60 ).to_i | |
end | |
def time_to_go_in_words | |
(day ,hour = (tmp, min = (tmp, sec = (seconds_to_go.to_i).divmod(60))[0].divmod(60))[0].divmod(24)) | |
time_s = '' | |
time_s << " #{day} #{choose_plural(day, 'день', 'дні', 'днів')}" unless day.zero? | |
time_s << " #{hour} #{choose_plural(hour, 'година', 'години', 'годин')}" unless hour.zero? | |
time_s << " #{min} #{choose_plural(min, 'хвилина', 'хвилини', 'хвилин')}" unless min.zero? | |
#time_s << " #{sec} #{choose_plural(sec, 'секудна', 'секунди', 'секунд')}" unless sec.zero? | |
return time_s.strip | |
end | |
:private | |
# Is is the current month after the birthday month? | |
# | |
def passed_birthday_month? | |
@month > @birthday_month | |
end | |
# Is the current month the same month as the birthday month | |
# and the day | |
# | |
def passed_birthday_day? | |
@month == @birthday_month && @day > @birthday_day | |
end | |
# Happened this year | |
# | |
def already_happend? | |
passed_birthday_month? || passed_birthday_day? | |
end | |
# Returns the next birthday | |
# | |
def next_birthday | |
if already_happend? | |
# Add 1 year if it's already happened | |
DateTime.new(@year + 1 , @birthday_month, @birthday_day, 0, 0, 0, @zone) | |
else | |
# Create this years date | |
DateTime.new(@year, @birthday_month, @birthday_day, 0, 0, 0, @zone) | |
end | |
end | |
# Вибирає потрібний відмінок в залежності від числа | |
# | |
def choose_plural( amount, *variants ) | |
variant = ( (amount % 10 == 1 && amount % 100 != 11) ? ( 1 ) : ( amount % 10 >= 2 && amount % 10 <= 4 && (amount % 100 < 10 || amount % 100 >= 20) ) ? 2 : 3 ) | |
variants[variant - 1] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
require 'bundler' | |
ENV['BUNDLE_GEMFILE'] = File.join(File.dirname(__FILE__) ,'Gemfile') | |
Bundler.setup :default | |
require 'vkontakte' | |
require_relative 'countdown' | |
if __FILE__ == $0 | |
CLIENT_SECRET = '' | |
CLIENT_ID = '' | |
email = '' | |
pass = '' | |
year = 2012 | |
month = 6 | |
day = 8 | |
if DateTime.now < DateTime.new(year, month, day) | |
countdown = BirthdayCountdown.new(month, day, '+03:00') | |
text = "До Євро-2012 залишилось #{countdown.time_to_go_in_words}" | |
vk = Vkontakte::Client.new(CLIENT_ID, CLIENT_SECRET) | |
vk.login!(email, pass, scope = '1024') | |
vk.api.status_set(:text => text) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment