ghuls-lib 1.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 +7 -0
  2. data/lib/ghuls_utilities.rb +178 -0
  3. metadata +85 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5aa5a88ed940a497b6f2e711a1873ab60c7da4d
4
+ data.tar.gz: a10f8c7350c0f2f675e4c83cdbdc9db6798bc7d0
5
+ SHA512:
6
+ metadata.gz: 36dcf7ff09d9caaeebf7170162d52c4746f298d8a9240026476789ad7afc2bd3c647e4efd782f09f0f7c7cd5cfd7ea15f3bd74f02b46c3ca940924ded72f8a3d
7
+ data.tar.gz: fd52d81bb96e16bad552df6bf7c751d2c8b7e579d7e604611dd16b2d7f884284e7ecf9561e6d6df37cb4fd99eef7ac831a352e2c55e3f0288e326fec2ee160de
@@ -0,0 +1,178 @@
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
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghuls-lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: octokit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
42
+ name: string-utility
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: The library used for and by the GHULS applications.
56
+ email: elifosterwy@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/ghuls_utilities.rb
62
+ homepage: https://github.com/ghuls-apps/ghuls-lib
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '2.0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.5.1
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: The library used for and by the GHULS applications.
85
+ test_files: []