ghuls-lib 2.3.3 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ghuls/lib.rb +50 -57
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08d2b21c69bc7c1524ea501fb386e677320c865f
4
- data.tar.gz: 0fe798091c4f6057de33d10eb52df6d585d08e02
3
+ metadata.gz: 3083ad25f7f8a118bb82f1f26f83ac1bb494a501
4
+ data.tar.gz: 25d9923736008cdfe00aa882a8181f008a5b2a3e
5
5
  SHA512:
6
- metadata.gz: 7ac334aea2eecee3e666726945fc51b333d3fc6789c5817925d0ef5d773285b638e3ac6b67d5fecd5014baf38f664a250906efc0a6cda9a496a826e1cf18245d
7
- data.tar.gz: 5549af9117b51d76e2b0647a3767ff3e7d08b5fe8faf09e72c4b355e7f980561df41af6406354fe97277946482aed34164b69a987561e8f2ecd89864bd280b67
6
+ metadata.gz: d3cd5fb0c187408234bf475b6bc9ae3986ccad57499ecf09747ef453ff99f38114e8eaadf5b72c0882ca960a31af42e5193b5cad9d3de63913b0f301d6847fdd
7
+ data.tar.gz: b80d92d0c4d4d60b3e8e63d5ac07efac812fd8edc9f62fc01299f40bcbe1b988567ce99555105dd89011bd792adcd34c25843a27609d9cea6c204493e4bcf7af
data/lib/ghuls/lib.rb CHANGED
@@ -4,15 +4,21 @@ require 'open-uri'
4
4
  require 'faraday-http-cache'
5
5
 
6
6
  module GHULS
7
- module Lib
8
- module_function
7
+ class Lib
8
+ # @return [Octokit::Client] The octokit instance.
9
+ attr_reader :octokit
10
+
11
+ # @return [Hash<String, Hash<String, String>] The colors for each GitHub valid lang.
12
+ # The keys are the language name strings, and the values are hashes. The value hash contains a 'color' and 'url'.
13
+ # The value hash color might be nil. Code expecting nil.
14
+ attr_reader :colors
9
15
 
10
16
  # Gets the Octokit and colors for the program.
11
17
  # @param opts [Hash] The options to use. The ones that are used by this
12
18
  # method are: :token, :pass, and :user.
13
19
  # @return [Hash] A hash containing objects formatted as
14
20
  # { git: Octokit::Client, colors: JSON }
15
- def configure_stuff(opts = {})
21
+ def initialize(opts = {})
16
22
  token = opts[:token]
17
23
  pass = opts[:pass]
18
24
  user = opts[:user]
@@ -24,21 +30,17 @@ module GHULS
24
30
  builder.adapter :net_http_persistent
25
31
  end
26
32
  gh.middleware = stack
27
- begin
28
- encode = gh.contents('ozh/github-colors', path: 'colors.json')[:content]
29
- { git: gh, colors: JSON.parse(Base64.decode64(encode)) }
30
- rescue Octokit::Unauthorized
31
- return false
32
- end
33
+ encode = gh.contents('ozh/github-colors', path: 'colors.json')[:content]
34
+ @octokit = gh
35
+ @colors = JSON.parse(Base64.decode64(encode))
33
36
  end
34
37
 
35
38
  # Gets the user and checks if it exists in the process.
36
39
  # @param user [Any] The user ID or name.
37
- # @param github [Octokit::Client] The instance of Octokit::Client.
38
40
  # @return [Hash] Their username and avatar URL.
39
41
  # @return [Boolean] False if it does not exist.
40
- def get_user_and_check(user, github)
41
- user_full = github.user(user)
42
+ def get_user_and_check(user)
43
+ user_full = @octokit.user(user)
42
44
  {
43
45
  username: user_full[:login],
44
46
  avatar: user_full[:avatar_url]
@@ -50,20 +52,19 @@ module GHULS
50
52
  # Returns the repos in the user's organizations that they have actually
51
53
  # contributed to, organized by forks, privates, publics, and mirrors.
52
54
  # @param username [String] See #get_user_and_check
53
- # @param github [Octokit::Client] See #get_user_and_check
54
55
  # @return [Array] All the repository full names that the user has
55
56
  # contributed to.
56
- def get_org_repos(username, github)
57
- orgs = github.organizations(username)
57
+ def get_org_repos(username)
58
+ orgs = @octokit.organizations(username)
58
59
  repos = []
59
60
  orgs.each do |o|
60
- this_org_repos = github.repositories(o[:login])
61
+ this_org_repos = @octokit.repositories(o[:login])
61
62
  next unless this_org_repos.any?
62
63
  repos.concat(this_org_repos)
63
64
  end
64
65
  true_repos = []
65
66
  repos.each do |r|
66
- contributors = github.contributors(r[:full_name])
67
+ contributors = @octokit.contributors(r[:full_name])
67
68
  next if contributors.empty?
68
69
  contributors.each do |c|
69
70
  if c[:login] =~ /^#{username}$/i
@@ -79,43 +80,39 @@ module GHULS
79
80
  # Gets the user's repositories organized by whether they are forks,
80
81
  # private, public, or mirrors.
81
82
  # @param username [String] See #get_user_and_check
82
- # @param github [Octokit::Client] See #get_user_and_check
83
83
  # @return [Hash] All the repositories under the user's account.
84
- def get_user_repos(username, github)
85
- get_organized_repos(github.repositories(username))
84
+ def get_user_repos(username)
85
+ get_organized_repos(@octokit.repositories(username))
86
86
  end
87
87
 
88
88
  # Gets the number of forkers, stargazers, and watchers.
89
89
  # @param repository [String] The full repository name.
90
- # @param github [Octkit::Client] See #get_user_and_check
91
90
  # @return [Hash] The forks, stars, and watcher count.
92
- def get_forks_stars_watchers(repository, github)
91
+ def get_forks_stars_watchers(repository)
93
92
  {
94
- forks: github.forks(repository).length,
95
- stars: github.stargazers(repository).length,
96
- watchers: github.subscribers(repository).length
93
+ forks: @octokit.forks(repository).length,
94
+ stars: @octokit.stargazers(repository).length,
95
+ watchers: @octokit.subscribers(repository).length
97
96
  }
98
97
  end
99
98
 
100
99
  # Gets the number of followers and users followed by the user.
101
100
  # @param username [String] See #get_user_and_check
102
- # @param github [Octokit::Client] See #get_user_and_check
103
101
  # @return [Hash] The number of following and followed users.
104
- def get_followers_following(username, github)
102
+ def get_followers_following(username)
105
103
  {
106
- following: github.following(username).length,
107
- followers: github.followers(username).length
104
+ following: @octokit.following(username).length,
105
+ followers: @octokit.followers(username).length
108
106
  }
109
107
  end
110
108
 
111
109
  # Gets the number of closed/open issues and
112
110
  # closed (without merge)/open/merged pull requests for a repository
113
111
  # @param repository [String] See #get_forks_stars_watchers
114
- # @param github [Octokit::Client] See #get_user_and_check
115
112
  # @return [Hash] The number of issues and pulls.
116
- def get_issues_pulls(repository, github)
117
- issues = github.list_issues(repository, state: 'all')
118
- pulls = github.pull_requests(repository, state: 'all')
113
+ def get_issues_pulls(repository)
114
+ issues = @octokit.list_issues(repository, state: 'all')
115
+ pulls = @octokit.pull_requests(repository, state: 'all')
119
116
  issues_open = 0
120
117
  issues_closed = 0
121
118
  pulls_open = 0
@@ -148,15 +145,14 @@ module GHULS
148
145
 
149
146
  # Gets the langauges and their bytes for the user.
150
147
  # @param username [String] See #get_user_and_check
151
- # @param github [Octokit::Client] See #get_user_and_check
152
148
  # @return [Hash] The languages and their bytes, as formatted as
153
149
  # { :Ruby => 129890, :CoffeeScript => 5970 }
154
- def get_user_langs(username, github)
155
- repos = get_user_repos(username, github)
150
+ def get_user_langs(username)
151
+ repos = get_user_repos(username)
156
152
  langs = {}
157
153
  repos[:public].each do |r|
158
154
  next if repos[:forks].include? r
159
- repo_langs = github.languages(r)
155
+ repo_langs = @octokit.languages(r)
160
156
  repo_langs.each do |l, b|
161
157
  if langs[l].nil?
162
158
  langs[l] = b
@@ -170,14 +166,13 @@ module GHULS
170
166
 
171
167
  # Gets the languages and their bytes for the user's organizations.
172
168
  # @param username [String] See #get_user_and_check
173
- # @param github [Octokit::Client] See #get_user_and_check
174
169
  # @return [Hash] See #get_user_langs
175
- def get_org_langs(username, github)
176
- org_repos = get_org_repos(username, github)
170
+ def get_org_langs(username)
171
+ org_repos = get_org_repos(username)
177
172
  langs = {}
178
173
  org_repos[:public].each do |r|
179
174
  next if org_repos[:forks].include? r
180
- repo_langs = github.languages(r)
175
+ repo_langs = @octokit.languages(r)
181
176
  repo_langs.each do |l, b|
182
177
  if langs[l].nil?
183
178
  langs[l] = b
@@ -189,21 +184,12 @@ module GHULS
189
184
  langs
190
185
  end
191
186
 
192
- # Gets the percentage for the given numbers.
193
- # @param part [Fixnum] The partial value.
194
- # @param whole [Fixnum] The whole value.
195
- # @return [Fixnum] The percentage that part is of whole.
196
- def calculate_percent(part, whole)
197
- (part / whole) * 100
198
- end
199
-
200
187
  # Gets the defined color for the language.
201
188
  # @param lang [String] The language name.
202
- # @param colors [Hash] The hash of colors and languages.
203
189
  # @return [String] The 6 digit hexidecimal color.
204
190
  # @return [Nil] If there is no defined color for the language.
205
- def get_color_for_language(lang, colors)
206
- color_lang = colors[lang]
191
+ def get_color_for_language(lang)
192
+ color_lang = @colors[lang]
207
193
  color = color_lang['color']
208
194
  if color_lang.nil? || color.nil?
209
195
  return StringUtility.random_color_six
@@ -235,9 +221,8 @@ module GHULS
235
221
  # to find the maximum number of users, which may not be the best way to do
236
222
  # it. However, none of the documented GitHub APIs show that we can get the
237
223
  # total number of GitHub users.
238
- # @param github [Octokit::Client] See #get_user_and_check
239
224
  # @return [Hash] See #get_user_and_check.
240
- def get_random_user(github)
225
+ def get_random_user
241
226
  source = open('https://github.com/search?utf8=%E2%9C%93&q=repos%3A%3E0' \
242
227
  '&type=Users&ref=searchresults').read
243
228
  continue = false
@@ -245,13 +230,21 @@ module GHULS
245
230
  # Really, GitHub? ’ and not '?
246
231
  max = source[/We['’]ve found (.*?) users/] || source[/Showing (.*?) available users/]
247
232
  userid = rand(max.to_i_separated)
248
- user = get_user_and_check(userid, github)
249
- continue = true if user != false && !get_user_langs(user, github).empty?
233
+ user = get_user_and_check(userid)
234
+ continue = true if user != false && !get_user_langs(user).empty?
250
235
  end
251
236
  # noinspection RubyScope
252
237
  user
253
238
  end
254
239
 
240
+ # Gets the percentage for the given numbers.
241
+ # @param part [Any] The partial value. Must implement #to_f, or be a Float.
242
+ # @param whole [Any] The whole value. Must implement #to_f, or be a Float.
243
+ # @return [Float] The percentage that part is of whole.
244
+ def self.calculate_percent(part, whole)
245
+ (part.to_f / whole.to_f) * 100
246
+ end
247
+
255
248
  private
256
249
 
257
250
  # Gets the organized repository hash for the main repository hash given
@@ -285,4 +278,4 @@ module GHULS
285
278
  }
286
279
  end
287
280
  end
288
- end
281
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghuls-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.3
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-16 00:00:00.000000000 Z
11
+ date: 2016-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit