ghuls-lib 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ghuls/lib.rb +180 -0
  3. metadata +3 -17
  4. data/lib/ghuls.rb +0 -178
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c7a9007ef065b4bb7e86d33b12bc7a978b674b7
4
- data.tar.gz: 92acc6ffccaa143c67e39a09efb5e4b6288ba420
3
+ metadata.gz: 93492def48a9670b6caff212c59072b83b2a997d
4
+ data.tar.gz: 55c789087aac45e5129afb58a3c23d86d30fa5a2
5
5
  SHA512:
6
- metadata.gz: 429d845df9afc305ee01232175b538ff903224e01bd9775812b55eb213bc12ce1e4aae555fcbebd0527217b37daff5b233f4cef86da8af63e4c7528d53964400
7
- data.tar.gz: beb997665dc4def6bac22eed0fc7e65821c55f6a2781bb34401483fa8d8079c420073acf8a84dd4635c46cfc755aab6ae1ef033e4a0dd531e2a70610a2fd3012
6
+ metadata.gz: c029fe48c3b090f01281db001e8a9f06ffa81d0d64b6cb03d9290c7b236eb91b3ad82b1cde6f9704c254fe76ec56b5feb42f9c2acbb9190eed4b5e9f016c7ace
7
+ data.tar.gz: 260c39163dd22118e2452ad92c5cc7df268102ccbeebce91ef0efcba7fe95d11afdaa0621c79ab8a32fad182fd890c84c8790c49b289a3d3de4c73ed24f90ff5
data/lib/ghuls/lib.rb ADDED
@@ -0,0 +1,180 @@
1
+ require 'octokit'
2
+ require 'string-utility'
3
+ require 'open-uri'
4
+
5
+ module GHULS
6
+ module Lib
7
+ # Gets the Octokit and colors for the program.
8
+ # @param opts [Hash] The options to use. The ones that are used by this
9
+ # method are: :token, :pass, and :user.
10
+ # @return [Hash] A hash containing objects formatted as
11
+ # { git: Octokit::Client, colors: JSON }
12
+ def self.configure_stuff(opts = {})
13
+ token = opts[:token]
14
+ pass = opts[:pass]
15
+ user = opts[:user]
16
+ gh = Octokit::Client.new(login: user, password: pass) if token.nil?
17
+ gh = Octokit::Client.new(access_token: token) unless token.nil?
18
+ begin
19
+ encode = gh.contents('ozh/github-colors', path: 'colors.json')[:content]
20
+ colors = JSON.parse(Base64.decode64(encode))
21
+ rescue Octokit::Unauthorized
22
+ return false
23
+ end
24
+ { git: gh, colors: colors }
25
+ end
26
+
27
+ # Gets the next value in an array.
28
+ # @param single [Any] The current value.
29
+ # @param full [Array] The main array to parse.
30
+ # @return [Any] The next value in the array.
31
+ def self.get_next(single, full)
32
+ full.at(full.index(single) + 1)
33
+ end
34
+
35
+ # Gets whether or not the user exists.
36
+ # @param username [String] The user to check
37
+ # @param github [Octokit::Client] The instance of Octokit to use.
38
+ # @return [Boolean] True if it does, false if it doesn't.
39
+ def self.user_exists?(username, github)
40
+ begin
41
+ github.user(username)
42
+ rescue Octokit::NotFound
43
+ return false
44
+ end
45
+ true
46
+ end
47
+
48
+ # Returns the repos in the user's organizations that they have actually
49
+ # contributed to.
50
+ # @param username [String] See #user_exists?
51
+ # @param github [Octokit::Client] See #user_exists?
52
+ # @return [Array] All the repository full names that the user has
53
+ # contributed to.
54
+ def self.get_org_repos(username, github)
55
+ orgs = github.organizations(username)
56
+ repos = []
57
+ orgs.each do |o|
58
+ repos += github.repositories(o[:login])
59
+ end
60
+ true_repos = []
61
+ repos.each do |r|
62
+ next if r[:fork]
63
+ contributors = github.contributors(r[:full_name])
64
+ contributors.each do |c|
65
+ if c[:login] =~ /^#{username}$/i
66
+ true_repos.push(r[:full_name])
67
+ else
68
+ next
69
+ end
70
+ end
71
+ end
72
+ true_repos
73
+ end
74
+
75
+ # Gets the langauges and their bytes for the :user.
76
+ # @param username [String] The username to get info for.
77
+ # @param github [OctoKit::Client] The instance of Octokit::Client.
78
+ # @return [Hash] The languages and their bytes, as formatted as
79
+ # { :Ruby => 129890, :CoffeeScript => 5970 }
80
+ def self.get_user_langs(username, github)
81
+ repos = github.repositories(username)
82
+ langs = {}
83
+ repos.each do |r|
84
+ next if r[:fork]
85
+ repo_langs = github.languages(r[:full_name])
86
+ repo_langs.each do |l, b|
87
+ if langs[l].nil?
88
+ langs[l] = b
89
+ else
90
+ existing = langs[l]
91
+ langs[l] = existing + b
92
+ end
93
+ end
94
+ end
95
+ langs
96
+ end
97
+
98
+ def self.get_org_langs(username, github)
99
+ org_repos = get_org_repos(username, github)
100
+ langs = {}
101
+ org_repos.each do |r|
102
+ repo_langs = github.languages(r)
103
+ repo_langs.each do |l, b|
104
+ if langs[l].nil?
105
+ langs[l] = b
106
+ else
107
+ existing = langs[l]
108
+ langs[l] = existing + b
109
+ end
110
+ end
111
+ end
112
+ langs
113
+ end
114
+
115
+ # Gets the percentage for the given numbers.
116
+ # @param part [Fixnum] The partial value.
117
+ # @param whole [Fixnum] The whole value.
118
+ # @return [Fixnum] The percentage that part is of whole.
119
+ def self.calculate_percent(part, whole)
120
+ (part / whole) * 100
121
+ end
122
+
123
+ # Gets the defined color for the language.
124
+ # @param lang [String] The language name.
125
+ # @param colors [Hash] The hash of colors and languages.
126
+ # @return [String] The 6 digit hexidecimal color.
127
+ # @return [Nil] If there is no defined color for the language.
128
+ def self.get_color_for_language(lang, colors)
129
+ if colors[lang].nil? || colors[lang]['color'].nil?
130
+ return StringUtility.random_color_six
131
+ else
132
+ return colors[lang]['color']
133
+ end
134
+ end
135
+
136
+ def self.get_language_percentages(langs)
137
+ total = 0
138
+ langs.each { |_, b| total += b }
139
+ lang_percents = {}
140
+ langs.each do |l, b|
141
+ percent = calculate_percent(b, total.to_f)
142
+ lang_percents[l] = percent.round(2)
143
+ end
144
+ lang_percents
145
+ end
146
+
147
+ def self.analyze_orgs(username, github)
148
+ if user_exists?(username, github)
149
+ langs = get_org_langs(username, github)
150
+ return false if langs.empty?
151
+ get_language_percentages(langs)
152
+ else
153
+ false
154
+ end
155
+ end
156
+
157
+ def self.analyze_user(username, github)
158
+ if user_exists?(username, github)
159
+ langs = get_user_langs(username, github)
160
+ return false if langs.empty?
161
+ get_language_percentages(langs)
162
+ else
163
+ false
164
+ end
165
+ end
166
+
167
+ using StringUtility
168
+ def self.get_random_user(github)
169
+ source = open('https://github.com/search?utf8=%E2%9C%93&q=repos%3A%3E-1' \
170
+ '&type=Users&ref=searchresults').read
171
+ has_repos = false
172
+ while has_repos == false
173
+ userid = rand(source[/Showing (.*?) available users/, 1].to_i_separated)
174
+ user = github.user(userid)[:login]
175
+ has_repos = true unless get_user_langs(user, github).empty?
176
+ end
177
+ user
178
+ end
179
+ end
180
+ 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: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-02 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: octokit
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rainbow
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: string-utility
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,7 +44,7 @@ executables: []
58
44
  extensions: []
59
45
  extra_rdoc_files: []
60
46
  files:
61
- - lib/ghuls.rb
47
+ - lib/ghuls/lib.rb
62
48
  homepage: https://github.com/ghuls-apps/ghuls-lib
63
49
  licenses: []
64
50
  metadata: {}
data/lib/ghuls.rb DELETED
@@ -1,178 +0,0 @@
1
- require 'octokit'
2
- require 'string-utility'
3
- require 'open-uri'
4
-
5
- module Utilities
6
- # Gets the Octokit and colors for the program.
7
- # @param opts [Hash] The options to use. The ones that are used by this method
8
- # are: :token, :pass, and :user.
9
- # @return [Hash] A hash containing objects formatted as
10
- # { git: Octokit::Client, colors: JSON }
11
- def self.configure_stuff(opts = {})
12
- token = opts[:token]
13
- pass = opts[:pass]
14
- user = opts[:user]
15
- gh = Octokit::Client.new(login: user, password: pass) if token.nil?
16
- gh = Octokit::Client.new(access_token: token) unless token.nil?
17
- begin
18
- encoded = gh.contents('ozh/github-colors', path: 'colors.json')[:content]
19
- colors = JSON.parse(Base64.decode64(encoded))
20
- rescue Octokit::Unauthorized
21
- return false
22
- end
23
- { git: gh, colors: colors }
24
- end
25
-
26
- # Gets the next value in an array.
27
- # @param single [Any] The current value.
28
- # @param full [Array] The main array to parse.
29
- # @return [Any] The next value in the array.
30
- def self.get_next(single, full)
31
- full.at(full.index(single) + 1)
32
- end
33
-
34
- # Gets whether or not the user exists.
35
- # @param username [String] The user to check
36
- # @param github [Octokit::Client] The instance of Octokit to use.
37
- # @return [Boolean] True if it does, false if it doesn't.
38
- def self.user_exists?(username, github)
39
- begin
40
- github.user(username)
41
- rescue Octokit::NotFound
42
- return false
43
- end
44
- true
45
- end
46
-
47
- # Returns the repos in the user's organizations that they have actually
48
- # contributed to.
49
- # @param username [String] See #user_exists?
50
- # @param github [Octokit::Client] See #user_exists?
51
- # @return [Array] All the repository full names that the user has contributed
52
- # to.
53
- def self.get_org_repos(username, github)
54
- orgs = github.organizations(username)
55
- repos = []
56
- orgs.each do |o|
57
- repos += github.repositories(o[:login])
58
- end
59
- true_repos = []
60
- repos.each do |r|
61
- next if r[:fork]
62
- contributors = github.contributors(r[:full_name])
63
- contributors.each do |c|
64
- if c[:login] =~ /^#{username}$/i
65
- true_repos.push(r[:full_name])
66
- else
67
- next
68
- end
69
- end
70
- end
71
- true_repos
72
- end
73
-
74
- # Gets the langauges and their bytes for the :user.
75
- # @param username [String] The username to get info for.
76
- # @param github [OctoKit::Client] The instance of Octokit::Client.
77
- # @return [Hash] The languages and their bytes, as formatted as
78
- # { :Ruby => 129890, :CoffeeScript => 5970 }
79
- def self.get_user_langs(username, github)
80
- repos = github.repositories(username)
81
- langs = {}
82
- repos.each do |r|
83
- next if r[:fork]
84
- repo_langs = github.languages(r[:full_name])
85
- repo_langs.each do |l, b|
86
- if langs[l].nil?
87
- langs[l] = b
88
- else
89
- existing = langs[l]
90
- langs[l] = existing + b
91
- end
92
- end
93
- end
94
- langs
95
- end
96
-
97
- def self.get_org_langs(username, github)
98
- org_repos = get_org_repos(username, github)
99
- langs = {}
100
- org_repos.each do |r|
101
- repo_langs = github.languages(r)
102
- repo_langs.each do |l, b|
103
- if langs[l].nil?
104
- langs[l] = b
105
- else
106
- existing = langs[l]
107
- langs[l] = existing + b
108
- end
109
- end
110
- end
111
- langs
112
- end
113
-
114
- # Gets the percentage for the given numbers.
115
- # @param part [Fixnum] The partial value.
116
- # @param whole [Fixnum] The whole value.
117
- # @return [Fixnum] The percentage that part is of whole.
118
- def self.calculate_percent(part, whole)
119
- (part / whole) * 100
120
- end
121
-
122
- # Gets the defined color for the language.
123
- # @param lang [String] The language name.
124
- # @param colors [Hash] The hash of colors and languages.
125
- # @return [String] The 6 digit hexidecimal color.
126
- # @return [Nil] If there is no defined color for the language.
127
- def self.get_color_for_language(lang, colors)
128
- if colors[lang].nil? || colors[lang]['color'].nil?
129
- return StringUtility.random_color_six
130
- else
131
- return colors[lang]['color']
132
- end
133
- end
134
-
135
- def self.get_language_percentages(langs)
136
- total = 0
137
- langs.each { |_, b| total += b }
138
- lang_percents = {}
139
- langs.each do |l, b|
140
- percent = Utilities.calculate_percent(b, total.to_f)
141
- lang_percents[l] = percent.round(2)
142
- end
143
- lang_percents
144
- end
145
-
146
- def self.analyze_orgs(username, github)
147
- if user_exists?(username, github)
148
- langs = get_org_langs(username, github)
149
- return false if langs.empty?
150
- get_language_percentages(langs)
151
- else
152
- false
153
- end
154
- end
155
-
156
- def self.analyze_user(username, github)
157
- if user_exists?(username, github)
158
- langs = get_user_langs(username, github)
159
- return false if langs.empty?
160
- get_language_percentages(langs)
161
- else
162
- false
163
- end
164
- end
165
-
166
- using StringUtility
167
- def self.get_random_user(github)
168
- source = open('https://github.com/search?utf8=%E2%9C%93&q=repos%3A%3E-1&t' \
169
- 'ype=Users&ref=searchresults').read
170
- has_repos = false
171
- while has_repos == false
172
- userid = rand(source[/Showing (.*?) available users/, 1].to_i_separated)
173
- user = github.user(userid)[:login]
174
- has_repos = true unless get_user_langs(user, github).empty?
175
- end
176
- user
177
- end
178
- end