Class: GitScrap

Inherits:
Object
  • Object
show all
Defined in:
lib/gitgrass/git_scrap.rb

Constant Summary collapse

GITHUB_URL =
"https://github.com/"
TILE_COLOR =
{ "0": Rainbow("[]").color(235, 237, 240).background(235, 237, 240),
"1": Rainbow("[]").color(155, 233, 168).background(155, 233, 168),
"2": Rainbow("[]").color(111, 197, 100).background(111, 197, 100),
"3": Rainbow("[]").color(90, 162, 79).background(90, 162, 79),
"4": Rainbow("[]").color(59, 111, 58).background(59, 111, 58),
dummy: Rainbow("[]").color(0, 0, 0).background(0, 0, 0) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ GitScrap

Returns a new instance of GitScrap.



16
17
18
19
# File 'lib/gitgrass/git_scrap.rb', line 16

def initialize(user)
  @user = user
  @tile_table = Array.new(7) { [] }
end

Instance Attribute Details

#tile_tableObject (readonly)

Returns the value of attribute tile_table.



6
7
8
# File 'lib/gitgrass/git_scrap.rb', line 6

def tile_table
  @tile_table
end

#year=(value) ⇒ Object (writeonly)

Sets the attribute year

Parameters:

  • value

    the value to set the attribute year to.



7
8
9
# File 'lib/gitgrass/git_scrap.rb', line 7

def year=(value)
  @year = value
end

Instance Method Details

#get_commitObject



21
22
23
24
25
26
# File 'lib/gitgrass/git_scrap.rb', line 21

def get_commit
  url = @year ? "#{GITHUB_URL}#{@user}?tab=overview&from=#{@year}-01-01&to=#{@year}-12-31" : "#{GITHUB_URL}#{@user}"
  html = URI(url)
  body = html.read
  body.scan(/<rect.*data-count=.*data-level="(0|1|2|3|4)".*>/)
end

#make_grass_tableObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitgrass/git_scrap.rb', line 28

def make_grass_table
  commit_data = self.get_commit().flatten
  if @year
    Date.new(@year, 1, 1).wday.times do |n|
      commit_data.unshift("dummy")
    end
  end
  0.step(commit_data.length - 1, 7) do |index|
    oneweek_tile = commit_data.slice(index, 7)
    oneweek_tile.each_with_index do |item, index|
      @tile_table[index] << TILE_COLOR[item.to_sym]
    end
  end
end