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
   = 
  @user_agent = user_agent
  @repo_user = repo_user
  @repo_name = repo_name
end

Instance Method Details

#get_commits_historyObject

get commits history



114
115
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
# File 'lib/repocrawler/crawler.rb', line 114

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['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



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

def get_contributors
  contributors = HTTParty.get(@GITHUB_API_BASE_URL + "/contributors?access_token=#{@access_token}", headers: {
    "User-Agent" => @user_agent
  })
  if contributors['message'] === 'Not Found'
    contributor = 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



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

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

  if repos_meta['message'] === 'Not Found'
    forks = nil
  else
    forks = repos_meta['forks_count']
  end

  forks
end

#get_issuesObject



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

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

  if 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



146
147
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
# File 'lib/repocrawler/crawler.rb', line 146

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['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



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

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['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
# 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['message'] === 'Not Found'
    get_last_year_commit_activity = nil
  else
    last_year_commit_activity.delete_if {|record| record['total'] == 0}
  end

end

#get_readme_word_countObject

get the readme file



197
198
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
# File 'lib/repocrawler/crawler.rb', line 197

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['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!

    freqs
  end
end

#get_starsObject



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

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

  if repos_meta['message'] === 'Not Found'
    stars = nil
  else
    stars = repos_meta['stargazers_count']
  end

  stars
end

#get_total_commitsObject

get the total commits



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

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