Class: GhContrib::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/gh_contrib/agent.rb

Constant Summary collapse

GITHUB_URL =
'https://github.com'

Instance Method Summary collapse

Constructor Details

#initializeAgent

Returns a new instance of Agent.



8
9
10
# File 'lib/gh_contrib/agent.rb', line 8

def initialize
  @agent = Mechanize.new
end

Instance Method Details

#contributions(username) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/gh_contrib/agent.rb', line 20

def contributions(username)
  page = @agent.get "#{GITHUB_URL}/users/#{username}/contributions"
  doc = Nokogiri::HTML(page.body)
  doc.xpath('//rect').map {|element|
    { date: element["data-date"], count: element["data-count"].to_i }
  }.reverse
end

#contributions_by_month(username) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/gh_contrib/agent.rb', line 28

def contributions_by_month(username)
  contributions(username).each_with_object({}) {|row, h|
    key = row[:date][0, 7] # "2014-10-01" => "2014-10"
    h[key] ||= 0
    h[key] += row[:count].to_i
  }.map {|element|
    { date: element[0], count: element[1].to_i }
  }
end

#login(username, password) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/gh_contrib/agent.rb', line 12

def (username, password)
  page = @agent.get "#{GITHUB_URL}/login"
  form = page.forms[0]
  form.field_with(name: 'login').value = username
  form.field_with(name: 'password').value = password
  @agent.submit(form)
end