fav_language 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/exe/fav_lang +18 -0
- data/fav_language.gemspec +26 -0
- data/lib/fav_language.rb +9 -0
- data/lib/fav_language/detector.rb +35 -0
- data/lib/fav_language/language_stats.rb +26 -0
- data/lib/fav_language/repo.rb +18 -0
- data/lib/fav_language/repo_list.rb +25 -0
- data/lib/fav_language/version.rb +3 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d33b67c8a53086172cfd5b5056465e9123b193cb
|
4
|
+
data.tar.gz: b5ce800c0bab8898c7e92cce1a02d2550809455d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 23675df6b94cb6cd528ba6a803fa962ef6ecf536765d4240277a1f2383297f43480cb4c826c998018ea40a0a967599e796ca6171ef51d445795fde9ab24e51ea
|
7
|
+
data.tar.gz: 227855d528a9695effd6f80ea0a9d4bef58ac1b9452b44d800fa3f1bb8257ba4352a512427965a13677c6c5c5d8d9931477ae2566db3618e34b59e49380afba9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Tadas Tamosauskas
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# FavLanguage
|
2
|
+
|
3
|
+
This is a gem written as a part of a coding challenge. Given a GitHub username it tries to guess the user's favourite programming language.
|
4
|
+
|
5
|
+
The criteria used is the amount of bytes written in each language in all public owned repos of the user (omits forked repos).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
NB: It is not pushed to rubygems yet
|
10
|
+
|
11
|
+
This gem requires ruby >= 2.1
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'fav_language'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install fav_language
|
26
|
+
|
27
|
+
## Using the binary
|
28
|
+
|
29
|
+
The gem has a command line interface. If you have the gem installed:
|
30
|
+
|
31
|
+
```
|
32
|
+
fav_lang -h
|
33
|
+
```
|
34
|
+
|
35
|
+
Example:
|
36
|
+
```
|
37
|
+
fav_lang -u tadast
|
38
|
+
> JavaScript: 418357 bytes written
|
39
|
+
```
|
40
|
+
|
41
|
+
If you have the repo cloned, you can do the above, just append `bundle exec exe/` e.g. `bundle exec exe/fav_lang -u tadast`
|
42
|
+
|
43
|
+
## Using the library
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require 'fav_language'
|
47
|
+
fav_stats = FavLanguage::Detector.new(username: 'tadast').favourite_languages
|
48
|
+
# returns an array of objects that have two properties: weight and language
|
49
|
+
# it is an array in case the user has two equally favourite languages
|
50
|
+
fav_stats.size
|
51
|
+
> 1
|
52
|
+
fav_stats.first.language
|
53
|
+
> "JavaScript"
|
54
|
+
fav_stats.first.weight
|
55
|
+
> 353577 # bytes commited in this language
|
56
|
+
```
|
57
|
+
|
58
|
+
## API rate limits
|
59
|
+
|
60
|
+
GitHub limits the number of requests you can make to the API. To increase this number you may want to get an oauth token. You can follow the instructions [on the github_api gem readme](https://github.com/peter-murach/github#3-authentication) to get the token. Then you can pass in the token value via the command line
|
61
|
+
|
62
|
+
```
|
63
|
+
fav_lang -u tadast -t <token>
|
64
|
+
```
|
65
|
+
|
66
|
+
or as an argument
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
fav_stats = FavLanguage::Detector.new(username: 'tadast', token: "<token>")
|
70
|
+
```
|
71
|
+
## Development
|
72
|
+
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it ( https://github.com/[my-github-username]/fav_language/fork )
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fav_language"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/fav_lang
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fav_language"
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
optparse = OptionParser.new do |opts|
|
8
|
+
opts.on('-u', '--username username', String, 'GitHub username') { |o| options[:username] = o }
|
9
|
+
opts.on('-t', '--token token', String, 'GitHub auth token to increase rate limit. https://github.com/peter-murach/github#3-authentication') { |o| options[:token] = o }
|
10
|
+
end.parse!
|
11
|
+
|
12
|
+
begin
|
13
|
+
FavLanguage::Detector.new(options).print
|
14
|
+
rescue ArgumentError => e
|
15
|
+
puts e.message
|
16
|
+
abort("see 'fav_lang -h' for usage")
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fav_language/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fav_language"
|
8
|
+
spec.version = FavLanguage::VERSION
|
9
|
+
spec.authors = ["Tadas Tamosauskas"]
|
10
|
+
spec.email = ["tadas@pdfcv.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Guess the favourite language of a github user}
|
13
|
+
spec.description = %q{Guesses the favourite language of a github username by looking at the amount of code committed to the owned public repos}
|
14
|
+
spec.homepage = "https://github.com/tadast/fav_language"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "github_api", "~> 0.12"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "pry", "~> 0"
|
26
|
+
end
|
data/lib/fav_language.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module FavLanguage
|
2
|
+
class Detector
|
3
|
+
def initialize(username:, token: nil, stats_class: LanguageStats)
|
4
|
+
@username = username
|
5
|
+
@token = token
|
6
|
+
@stats_class = stats_class
|
7
|
+
end
|
8
|
+
|
9
|
+
def print
|
10
|
+
message = favourite_languages.map do |weighted_language|
|
11
|
+
"#{weighted_language.language}: #{weighted_language.weight} bytes written"
|
12
|
+
end.join("\n")
|
13
|
+
puts message
|
14
|
+
end
|
15
|
+
|
16
|
+
def favourite_languages
|
17
|
+
max_weight = weighted_languages.map(&:weight).max
|
18
|
+
weighted_languages.select{ |lww| lww.weight == max_weight }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
attr_reader :username, :token
|
23
|
+
|
24
|
+
def weighted_languages
|
25
|
+
validate_username
|
26
|
+
@weighted_languages ||= @stats_class.new(username: username, token: token).fetch
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_username
|
30
|
+
if username.to_s.size == 0
|
31
|
+
raise "Please provide GitHub username"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FavLanguage
|
2
|
+
class LanguageStats
|
3
|
+
def initialize(username:, token: nil, repo_list_class: RepoList)
|
4
|
+
@username = username
|
5
|
+
@token = token
|
6
|
+
@repo_list_class = repo_list_class
|
7
|
+
end
|
8
|
+
|
9
|
+
def fetch
|
10
|
+
language_stats.group_by(&:language).map do |language, stats|
|
11
|
+
OpenStruct.new(language: language, weight: stats.inject(0){ |sum, next_one| sum + next_one.bytes })
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
attr_reader :username, :token
|
17
|
+
|
18
|
+
def language_stats
|
19
|
+
@repo_list_class.new(github: github, username: username).language_stats
|
20
|
+
end
|
21
|
+
|
22
|
+
def github
|
23
|
+
@github ||= Github.new(oauth_token: token || ENV['GH_AUTH_TOKEN'])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'github_api'
|
2
|
+
|
3
|
+
module FavLanguage
|
4
|
+
class Repo
|
5
|
+
attr_reader :github
|
6
|
+
|
7
|
+
def initialize(raw, github:)
|
8
|
+
@raw = raw
|
9
|
+
@github = github
|
10
|
+
end
|
11
|
+
|
12
|
+
def language_stats
|
13
|
+
github.get_request(@raw['languages_url']).body.to_h.map do |language, bytes|
|
14
|
+
OpenStruct.new(language: language, bytes: bytes.to_i)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'github_api'
|
2
|
+
|
3
|
+
module FavLanguage
|
4
|
+
class RepoList
|
5
|
+
attr_reader :github, :username
|
6
|
+
|
7
|
+
def initialize(github:, username:)
|
8
|
+
@github = github
|
9
|
+
@username = username
|
10
|
+
end
|
11
|
+
|
12
|
+
def fetch
|
13
|
+
@repos ||= github.repos
|
14
|
+
.list(user: username, auto_pagination: true)
|
15
|
+
.reject{ |r| r["fork"] }
|
16
|
+
.map{ |r| Repo.new(r, github: github) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def language_stats
|
20
|
+
fetch.flat_map do |repo|
|
21
|
+
repo.language_stats
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fav_language
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tadas Tamosauskas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: github_api
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Guesses the favourite language of a github username by looking at the
|
70
|
+
amount of code committed to the owned public repos
|
71
|
+
email:
|
72
|
+
- tadas@pdfcv.com
|
73
|
+
executables:
|
74
|
+
- fav_lang
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- exe/fav_lang
|
88
|
+
- fav_language.gemspec
|
89
|
+
- lib/fav_language.rb
|
90
|
+
- lib/fav_language/detector.rb
|
91
|
+
- lib/fav_language/language_stats.rb
|
92
|
+
- lib/fav_language/repo.rb
|
93
|
+
- lib/fav_language/repo_list.rb
|
94
|
+
- lib/fav_language/version.rb
|
95
|
+
homepage: https://github.com/tadast/fav_language
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.2.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Guess the favourite language of a github user
|
119
|
+
test_files: []
|