Class: Repos::GithubData

Inherits:
Object
  • Object
show all
Defined in:
lib/repocrawler/crawler.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo_user, repo_name, github_password = '') ⇒ GithubData

Returns a new instance of GithubData.



12
13
14
15
16
17
18
19
20
# File 'lib/repocrawler/crawler.rb', line 12

def initialize(repo_user, repo_name, github_password='')
  @GITHUB_README_URL = "https://raw.githubusercontent.com/#{repo_user}/#{repo_name}/master"
  @GITHUB_API_BASE_URL = "https://api.github.com/repos/#{repo_user}/#{repo_name}"
  @access_token = ENV['github_token']
  @github_password = github_password === '' ? ENV['github_password'] : github_password
  @user_agent = ENV['user_agent']
  @repo_user = repo_user
  @repo_name = repo_name
end

Instance Method Details

#get_contributorsObject

Get the contributors



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/repocrawler/crawler.rb', line 32

def get_contributors
  contributors = HTTParty.get(@GITHUB_API_BASE_URL + "/contributors?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  }).map do |contributor|
    {
      'name' => contributor['login'],
      'contributions' => contributor['contributions']
    }
  end

  contributors
end

#get_forksObject

get numbers of forks, stars and issues



56
57
58
59
60
61
62
63
# File 'lib/repocrawler/crawler.rb', line 56

def get_forks
  repos_meta = HTTParty.get(@GITHUB_API_BASE_URL + "?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })
  forks = repos_meta['forks_count']

  forks
end

#get_issuesObject



74
75
76
77
78
79
80
81
# File 'lib/repocrawler/crawler.rb', line 74

def get_issues
  repos_meta = HTTParty.get(@GITHUB_API_BASE_URL + "?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })
  issues = repos_meta['open_issues_count']

  issues
end

#get_issues_infoObject

get information of the closed issues



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/repocrawler/crawler.rb', line 84

def get_issues_info
  closed_issues = []
  stop = false
  page = 1

  until stop
    issue_fetch = HTTParty.get(@GITHUB_API_BASE_URL + "/issues?state=closed&page=#{page}&access_token=#{@access_token}", headers: {
      "User-Agent" => @user_agent
    })
    if issue_fetch.count === 0
      stop = true
    end


    issue_fetch.each do |issue|
      closed_issues << {
        'number'    => issue['number'],
        'created_at'  => issue['created_at'],
        'closed_at'   => issue['closed_at'],
        'duration'    => (Date.parse(issue['closed_at']) - Date.parse(issue['created_at'])).to_i
      }
    end

    page += 1
  end

  closed_issues.reverse!
end

#get_last_commits_daysObject

get the date of the last commit



114
115
116
117
118
119
120
121
# File 'lib/repocrawler/crawler.rb', line 114

def get_last_commits_days
  github = Github.new basic_auth: "#{ENV['github_account']}:#{@github_password}"

  commit = github.repos.commits.list(@repo_user, @repo_name).to_ary[0].to_hash['commit']['author']['date']
  last_commit = (Date.today - Date.parse(commit)).to_i

  last_commit
end

#get_last_year_commit_activityObject

get the commit activity in last year



23
24
25
26
27
28
29
# File 'lib/repocrawler/crawler.rb', line 23

def get_last_year_commit_activity
  last_year_commit_activity = HTTParty.get(@GITHUB_API_BASE_URL + "/stats/commit_activity?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })

  last_year_commit_activity.delete_if {|record| record['total'] == 0}
end

#get_readme_word_countObject

get the readme file



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/repocrawler/crawler.rb', line 124

def get_readme_word_count
  github_contents = HTTParty.get(@GITHUB_API_BASE_URL + "/contents?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })


  readme_file = ''
  github_contents.each do |content|
    readme_file = content['name'] if content['name'] =~ /^README/
  end

  stop_words = []
  File.open(File.expand_path("../../public/stop_words.txt",  File.dirname(__FILE__)), "r") do |f|
    f.each_line do |line|
      stop_words << line.gsub(/\n/,"")
    end
  end

  readme = HTTParty.get(@GITHUB_README_URL + "/#{readme_file}")
  words = readme.split(' ')
  freqs = Hash.new(0)
  words.each do |word|
    if word =~ /^\w+$/ && !stop_words.include?(word.downcase)
      freqs[word] += 1 
    end
  end
  freqs = freqs.sort_by { |word, freq| freq }.reverse!

  freqs
end

#get_starsObject



65
66
67
68
69
70
71
72
# File 'lib/repocrawler/crawler.rb', line 65

def get_stars
  repos_meta = HTTParty.get(@GITHUB_API_BASE_URL + "?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })
  stars = repos_meta['stargazers_count']

  stars
end

#get_total_commitsObject

get the total commits



46
47
48
49
50
51
52
53
# File 'lib/repocrawler/crawler.rb', line 46

def get_total_commits
  contributors = get_contributors
  commits = contributors.reduce(0) do |sum, num|
    sum + num['contributions']
  end

  commits
end