Class: GithubStreakCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/github_streak_check.rb,
lib/github_streak_check/version.rb

Constant Summary collapse

MAX_TRY =
5
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GithubStreakCheck

Returns a new instance of GithubStreakCheck.



8
9
10
11
12
# File 'lib/github_streak_check.rb', line 8

def initialize(opts)
  @opts = opts

  @pst = ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
end

Instance Method Details

#commited_today?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/github_streak_check.rb', line 32

def commited_today?
  events = JSON.parse(get_status)
  return false if events.length == 0

  return events.any?{|event|
    Time.parse(event["created_at"]).in_time_zone(@pst).to_date == @pst.today
  }
end

#get_statusObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/github_streak_check.rb', line 42

def get_status
  try = 1
  begin
    open("https://api.github.com/users/#{@opts[:username]}/events?per_page=300").read
  rescue OpenURI::HTTPError => ex
    if try == MAX_TRY
      raise
    else
      puts "#{ex.message} (#{ex.class})"
      sleep 30

      try += 1
      retry
    end
  end
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/github_streak_check.rb', line 14

def run
  if commited_today?
    puts "Check OK: you've already done today(#{@pst.today} PST)'s contribution."
    exit 0
  else
    msg = "Check failed: You have not extended today(#{@pst.today} PST)'s streak yet"
    $stderr.puts msg
    if (addr = @opts[:mail_to])
      ret = Pony.mail(to: addr,
                      from: addr,
                      subject: "GithubStreakCheck",
                      body: msg)
      puts "Sent mail: #{ret.inspect}"
    end
    exit 1
  end
end