Class: OpenSourceStats

Inherits:
Object
  • Object
show all
Defined in:
lib/open_source_stats.rb,
lib/open_source_stats/user.rb,
lib/open_source_stats/event.rb,
lib/open_source_stats/version.rb,
lib/open_source_stats/organization.rb

Defined Under Namespace

Classes: Event, Organization, User

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clientObject

Authenticated Octokit client instance



15
16
17
# File 'lib/open_source_stats.rb', line 15

def self.client
  @client ||= Octokit::Client.new :access_token => ENV["GITHUB_TOKEN"]
end

.start_timeObject

Returns a Ruby Time instance coresponding to the earliest possible event timestamp



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

def self.start_time
  @start_time ||= Time.now - 24.hours
end

.start_time=(time) ⇒ Object

Helper method to overide the timestamp



25
26
27
# File 'lib/open_source_stats.rb', line 25

def self.start_time=(time)
  @start_time = time
end

Instance Method Details

#eventsObject

Returns an array of unique Events accross all Users and Organizations



60
61
62
# File 'lib/open_source_stats.rb', line 60

def events
  @events ||= user_events.concat(org_events).uniq
end

#org_eventsObject

Returns an array of Events across all Organziations



55
56
57
# File 'lib/open_source_stats.rb', line 55

def org_events
  @org_events ||= orgs.map { |o| o.events }.flatten
end

#orgsObject

Returns an array of Organizations from the specified list of organizations



50
51
52
# File 'lib/open_source_stats.rb', line 50

def orgs
  @orgs ||= ENV["GITHUB_ORGS"].split(/, ?/).map { |o| Organization.new(o) }
end

#stats(events = events) ⇒ Object

Returns the calculated stats hash



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/open_source_stats.rb', line 65

def stats(events=events)
  {
    :repositories_created => event_count(events, :type =>"CreateEvent"),
    :issue_comments => event_count(events, :type =>"IssueCommentEvent"),
    :issues_opened => event_count(events, :type =>"IssuesEvent", :action => "opened"),
    :issues_closed => event_count(events, :type =>"IssuesEvent", :action => "closed"),
    :repos_open_sourced => event_count(events, :type =>"PublicEvent"),
    :pull_requests_opened => event_count(events, :type =>"PullRequestEvent", :action => "opened"),
    :pull_requests_merged => event_count(events, :type =>"PullRequestEvent", :action => "closed", :merged => true),
    :versions_released => event_count(events, :type =>"ReleaseEvent", :action => "published"),
    :pushes => event_count(events, :type =>"PushEvent"),
    :commits => events.map { |e| e.commits }.flatten.inject{|sum,x| sum + x },
    :total_events => event_count(events)
  }
end

#teamObject



29
30
31
# File 'lib/open_source_stats.rb', line 29

def team
  @team ||= client.team ENV["GITHUB_TEAM_ID"]
end

#user_eventsObject

Returns an array of Events across all Users



45
46
47
# File 'lib/open_source_stats.rb', line 45

def user_events
  @user_events ||= users.map { |u| u.events }.flatten
end

#usersObject

Returns an array of Users from the given team



34
35
36
37
38
39
40
41
42
# File 'lib/open_source_stats.rb', line 34

def users
  @users ||= begin
    users = client.team_members ENV["GITHUB_TEAM_ID"], :per_page => 100
    while client.last_response.rels[:next] && client.rate_limit.remaining > 0
      users.concat client.get client.last_response.rels[:next].href
    end
    users.map { |u| User.new u[:login] }
  end
end