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_token, github_password, github_account, user_agent) ⇒ GithubData

Returns a new instance of GithubData.



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

def initialize(repo_user, repo_name, github_token, github_password, , user_agent)
  @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 = github_token
  @github_password = github_password
  @github_account = 
  @user_agent = user_agent
  @repo_user = repo_user
  @repo_name = repo_name
end

Instance Method Details

#get_commits_historyObject

get commits history



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/repocrawler/crawler.rb', line 116

def get_commits_history
  commits_info = []
  stop = false
  page = 1

  until stop
    commits_fetch = HTTParty.get(@GITHUB_API_BASE_URL + "/commits?page=#{page}&access_token=#{@access_token}", headers: {
      "User-Agent" => @user_agent
    })

    if commits_fetch.is_a?(Hash) && commits_fetch['message'] === 'Not Found'
      break
    end

    if commits_fetch.count === 0
      stop = true
    end

    commits_fetch.each do |commit|
      commits_info << {
        "committer"     => commit['commit']['committer']['name'],
        "created_at"    => commit['commit']['committer']['date']
      }
    end

    page += 1
  end

  commits_info.reverse!
end

#get_contributorsObject

Get the contributors



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/repocrawler/crawler.rb', line 39

def get_contributors
  contributors = HTTParty.get(@GITHUB_API_BASE_URL + "/contributors?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })
  if contributors.is_a?(Hash) && contributors['message'] === 'Not Found'
    contributors = nil
  else
    contributors.map! do |contributor|
      {
        'name' => contributor['login'],
        'contributions' => contributor['contributions']
      }
    end
  end

  contributors
end

#get_forksObject

get numbers of forks, stars and issues



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/repocrawler/crawler.rb', line 73

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

  if repos_meta.is_a?(Hash) && repos_meta['message'] === 'Not Found'
    forks = nil
  else
    forks = repos_meta['forks_count']
  end

  forks
end

#get_issuesObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/repocrawler/crawler.rb', line 101

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

  if repos_meta.is_a?(Hash) && repos_meta['message'] === 'Not Found'
    issues = nil
  else
    issues = repos_meta['open_issues_count']
  end

  issues
end

#get_issues_infoObject

get information of the closed issues



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/repocrawler/crawler.rb', line 148

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.is_a?(Hash) && issue_fetch['message'] === 'Not Found'
      break
    end

    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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/repocrawler/crawler.rb', line 183

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

  if commits_fetch.is_a?(Hash) && commits_fetch['message'] === 'Not Found'
    last_commit = nil
  else
    last_commit_date = commits_fetch.first['commit']['author']['date']
    last_commit = (Date.today - Date.parse(last_commit_date)).to_i
  end

  last_commit
end

#get_last_year_commit_activityObject

get the commit activity in last year



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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
  })

  if last_year_commit_activity.is_a?(Hash) && last_year_commit_activity['message'] === 'Not Found'
    last_year_commit_activity = nil
  else
    last_year_commit_activity.delete_if {|record| record['total'] == 0}
  end

  last_year_commit_activity

end

#get_readme_raw_textObject

get readme raw text



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/repocrawler/crawler.rb', line 234

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

  if readme.is_a?(Hash) && readme['message'] === 'Not Found'
    return nil
  else
    readme_content = {
      'content'   => readme['content'],
      'encoding'  => readme['encoding']
    }
  end

  readme_content
end

#get_readme_word_countObject

get the readme file



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/repocrawler/crawler.rb', line 199

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

  if github_contents.is_a?(Hash) && github_contents['message'] === 'Not Found'
    return nil
  else
    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!

    return freqs
  end
end

#get_starsObject



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

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

  if repos_meta.is_a?(Hash) && repos_meta['message'] === 'Not Found'
    stars = nil
  else
    stars = repos_meta['stargazers_count']
  end

  stars
end

#get_testObject

check if the project has test TODO: recursively search



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/repocrawler/crawler.rb', line 253

def get_test
  has_test = 0;

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

  if contents.is_a?(Hash) && contents['message'] === 'Not Found'
    return has_test
  else
    contents.select do |content|
      (content['name'] === 'spec' || content['name'] === 'test') && content['type'] === 'dir' ? has_test = 1 : has_test = 0
    end

    return has_test
  end
end

#get_total_commitsObject

get the total commits



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/repocrawler/crawler.rb', line 58

def get_total_commits
  contributors = get_contributors

  if contributors.nil?
    commits = nil
  else
    commits = contributors.reduce(0) do |sum, num|
      sum + num['contributions']
    end
  end

  commits
end