gh_contributors 0.8.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.
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,19 @@
1
+ # Github Contributors
2
+
3
+ Update static files with github contributors list.
4
+
5
+ - Static content update for speed and avoiding GH limits.
6
+ - Read GitHub contributions, transform to urls and update files.
7
+ - All public methods return self for easy chaining.
8
+
9
+ # Example
10
+
11
+ ```ruby
12
+ GhContributors.for_org('railsisntaller').to_urls.update_files('public/contributors.html')
13
+ GhContributors.for_repo('railsisntaller/website').to_urls.update_files('public/index.html')
14
+ ```
15
+
16
+ # TODO
17
+
18
+ 1. add tests
19
+ 2. improve docs
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ Kernel.load File.expand_path("../lib/gh_contributors/version.rb", __FILE__)
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gh_contributors"
8
+ s.version = GhContributors::VERSION
9
+ s.authors = ["Michal Papis"]
10
+ s.email = ["mpapis@gmail.com"]
11
+ s.homepage = "https://github.com/mpapis/gh_contributors"
12
+ s.summary = %q{Update static files with github contributors list.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+
17
+ #s.add_development_dependency "smf-gem"
18
+ end
@@ -0,0 +1,116 @@
1
+ #/usr/bin/env ruby
2
+
3
+ ##
4
+ # static content update for speed and avoiding GH limits
5
+ # read GitHub contributions, transform to urls and update files
6
+ # all public methods return self for easy chaining
7
+ #
8
+ # Example:
9
+ #
10
+ # GhContributors.for_org('railsisntaller').to_urls.update_files('public/contributors.html')
11
+ # GhContributors.for_repo('railsisntaller/website').to_urls.update_files('public/index.html', 'public/contributors.html')
12
+ #
13
+
14
+ require 'json'
15
+ require 'open-uri'
16
+
17
+ class GhContributors
18
+ DEFAULT_URL_FORMAT = %q{%Q{<a href="#{data['url']}" title="#{login} - #{data['contributions']}"><img src="#{data['avatar_url']}" alt="#{login} - #{data['contributions']}"/></a>}}
19
+ DEFAULT_SEARCH = /<span class="contributors">.*?<\/span>/m
20
+ DEFAULT_REPLACE = %q{%Q{<span class="contributors">\n#{@data.join("\n")}\n</span>}}
21
+
22
+ attr_reader :data
23
+
24
+ # for_org('railsinstaller')
25
+ def self.for_org(name)
26
+ GhContributors.new.for_org(name)
27
+ end
28
+ def for_org(name)
29
+ @data = load_json(url_builder("orgs/#{name}/repos")).map{ |repo|
30
+ log "repository: #{name}/#{repo['name']}"
31
+ load_json(repo['contributors_url'])
32
+ }.inject(&:+)
33
+ calculate
34
+ self
35
+ end
36
+
37
+ # for_repo('railsinstaller/webapp')
38
+ def self.for_repo(name)
39
+ GhContributors.new.for_repo(name)
40
+ end
41
+ def for_repo(name)
42
+ log "repository: #{name}"
43
+ @data = load_json(url_builder("repos/#{name}/contributors"))
44
+ calculate
45
+ self
46
+ end
47
+
48
+ def to_urls(format=GhContributors::DEFAULT_URL_FORMAT)
49
+ @data.map! {|login, data|
50
+ log "user: #{login} - #{data['contributions']}"
51
+ if block_given?
52
+ yield login, data
53
+ else
54
+ eval format
55
+ end
56
+ }
57
+ self
58
+ end
59
+
60
+ def update_files(*files)
61
+ options = files.last.kind_of?(Hash) ? files.pop : {}
62
+ options[:search] ||= DEFAULT_SEARCH
63
+ options[:replace] ||= DEFAULT_REPLACE
64
+ files = files.first if files.first.kind_of? Array
65
+ files.each do |file|
66
+ log "file: #{file}"
67
+ update_file(file) do |text|
68
+ if block_given?
69
+ text = yield text, @data, file
70
+ else
71
+ text.sub(options[:search], eval(options[:replace]))
72
+ end
73
+ end
74
+ end
75
+ self
76
+ end
77
+
78
+ private
79
+
80
+ # group data, calculate contributions, sort by contributions
81
+ def calculate
82
+ @data = @data.group_by {|contributor|
83
+ contributor['login']
84
+ }.map {|login, data|
85
+ [login, {
86
+ 'avatar_url' => data.first['avatar_url'],
87
+ 'url' => data.first['url'],
88
+ 'contributions' => data.map{|repo| repo['contributions'].to_i}.inject(&:+)
89
+ }]
90
+ }.sort_by{|login, data|
91
+ [1000000/data['contributions'], login]
92
+ }
93
+ end
94
+
95
+ # Build full path to resource to use
96
+ def url_builder(path)
97
+ "https://api.github.com/#{path}"
98
+ end
99
+
100
+ # Load json from url
101
+ def load_json(url)
102
+ open(url){ |json| JSON.load(json) }
103
+ end
104
+
105
+ # Allow editing file text in a block
106
+ # Example: update_file('some.txt'){|text| text.gsub(/bla/,'ble')}
107
+ def update_file(file)
108
+ text = File.read(file)
109
+ text = yield text
110
+ File.open(file, 'w') { |f| f.write(text) }
111
+ end
112
+
113
+ def log(text)
114
+ puts text
115
+ end
116
+ end
@@ -0,0 +1,4 @@
1
+ #/usr/bin/env ruby
2
+ class GhContributors
3
+ VERSION = "0.8.0"
4
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gh_contributors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michal Papis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - mpapis@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - README.md
23
+ - gh_contributors.gemspec
24
+ - lib/gh_contributors.rb
25
+ - lib/gh_contributors/version.rb
26
+ homepage: https://github.com/mpapis/gh_contributors
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.25
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Update static files with github contributors list.
50
+ test_files: []