Module: Gitpaint

Defined in:
lib/gitpaint.rb,
lib/gitpaint/config.rb,
lib/gitpaint/version.rb,
lib/gitpaint/png_renderer.rb,
lib/gitpaint/github_client.rb

Defined Under Namespace

Classes: Config, GithubClient, PNGRenderer

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.clean_localObject



136
137
138
# File 'lib/gitpaint.rb', line 136

def self.clean_local
  FileUtils.rm_rf "/tmp/#{config.repo}"
end

.configObject



24
25
26
# File 'lib/gitpaint.rb', line 24

def self.config
  Config.instance.config
end

.configure {|Config.instance.config| ... } ⇒ Object

Yields:



20
21
22
# File 'lib/gitpaint.rb', line 20

def self.configure
  yield Config.instance.config
end

.createObject



157
158
159
160
# File 'lib/gitpaint.rb', line 157

def self.create
  r = github_client.create_repository config.repo
  r.ssh_url
end

.current_grid(account) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitpaint.rb', line 28

def self.current_grid  
  response = HTTParty.get 'https://github.com/users/%s/contributions' % 
  data = Nokogiri::HTML(response.body).xpath '//rect'

  weeks = []
  while data.count > 0 do
    weeks.push []
    7.times do
	d = data.shift
	begin
        value = d['data-count'].to_i
	rescue NoMethodError
 value = nil
	end
      weeks.last.push value  
    end
  end
  weeks.transpose
end

.custom_ssh_script(dir = '/tmp') ⇒ Object



149
150
151
152
153
154
155
# File 'lib/gitpaint.rb', line 149

def self.custom_ssh_script dir = '/tmp'
  File.open "#{dir}/custom.sh", 'w' do |f|
    f.write "#!/bin/sh\n"
    f.write 'ssh -i "%s" "$@"' % config.ssh_key
  end
  FileUtils.chmod '+x', "#{dir}/custom.sh"
end

.data_to_dates(data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitpaint.rb', line 56

def self.data_to_dates data
  current_date = sunday_before_a_year_ago
  dates = {}
  line = pad_grid(data).transpose.flatten

  line.each do |value|
    dates[current_date.iso8601] = value if value > 0
    current_date += 1
  end

  dates
end

.github_clientObject



16
17
18
# File 'lib/gitpaint.rb', line 16

def self.github_client
  GithubClient.instance.client
end

.make_commit(date, message: 'The commit is a lie') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitpaint.rb', line 86

def self.make_commit date, message: 'The commit is a lie'
  pieces = []

  pieces.push "GIT_AUTHOR_NAME=%s" % config.username
  pieces.push "GIT_AUTHOR_EMAIL=%s" % config.email
  pieces.push "GIT_AUTHOR_DATE=%sT12:00:00" % date
  pieces.push "GIT_COMMITTER_NAME=%s" % config.username
  pieces.push "GIT_COMMITTER_EMAIL=%s" % config.email
  pieces.push "GIT_COMMITTER_DATE=%sT12:00:00" % date

  "%s git commit --allow-empty -m '%s' > /dev/null" % [
    pieces.join(' '),
    message
  ]
end

.make_commits(data, message:) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gitpaint.rb', line 124

def self.make_commits data,  message:
  FileUtils.chdir "/tmp/#{config.repo}"
  data = scale_commits data
  dates = data_to_dates data
  dates.each_pair do |date, count|
    count.times do
      s = Gitpaint.make_commit date, message: message
      `#{s}`
    end
  end
end

.nukeObject



140
141
142
143
144
145
146
147
# File 'lib/gitpaint.rb', line 140

def self.nuke
  begin
    victim = github_client.repos.select { |r| r.name == config.repo }.first.id
    github_client.delete_repository victim
  rescue NoMethodError => e
    # do something here
  end
end

.pad_grid(grid) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/gitpaint.rb', line 73

def self.pad_grid grid
  grid.map! { |row| pad_row row }

  until grid.count >= 7
    grid.push [0] * 52
  end
  grid
end

.pad_row(row) ⇒ Object



69
70
71
# File 'lib/gitpaint.rb', line 69

def self.pad_row row
  row + [0] * (52 - row.count)
end

.paint(data, message: config.commit_message) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gitpaint.rb', line 102

def self.paint data, message: config.commit_message
  start_dir = Dir.pwd
  clean_local
  nuke

  unless data.flatten.uniq.delete_if { |i| i == 0 } == []
    remote = create
    custom_ssh_script
    Git.configure do |config|
      config.git_ssh = '/tmp/custom.sh'
    end

    g = Git.init "/tmp/#{config.repo}"
    g.add_remote 'origin', remote

    make_commits data, message: message

    push
  end
  FileUtils.chdir start_dir
end

.pushObject



162
163
164
165
# File 'lib/gitpaint.rb', line 162

def self.push
  g = Git.open "/tmp/#{config.repo}"
  g.push 'origin', 'master'
end

.scale_commits(grid) ⇒ Object



82
83
84
# File 'lib/gitpaint.rb', line 82

def self.scale_commits grid
  grid.map { |row| row.map { |v| v * config.scale_factor } }
end

.sunday_before_a_year_agoObject



48
49
50
51
52
53
54
# File 'lib/gitpaint.rb', line 48

def self.sunday_before_a_year_ago
  year_ago = (Date.today - 365)
  if Date.today.strftime('%A') == 'Sunday'
    return Date.today - 364
  end
  year_ago - year_ago.wday
end