Class: Hubba::Stats
- Inherits:
-
Object
- Object
- Hubba::Stats
- Defined in:
- lib/hubba/stats.rb
Overview
keep track of repo stats over time (with history hash)
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
todo/check: rename to GithubRepoStats or RepoStats - why? why not?.
Instance Method Summary collapse
-
#fetch(gh) ⇒ Object
fetch / read / write methods.
- #full_name ⇒ Object
-
#initialize(full_name) ⇒ Stats
constructor
A new instance of Stats.
-
#read(data_dir: './data') ⇒ Object
note: use read instead of load (load is kind of keyword for loading code).
- #size ⇒ Object
- #stars ⇒ Object
- #write(data_dir: './data') ⇒ Object
Constructor Details
#initialize(full_name) ⇒ Stats
Returns a new instance of Stats.
12 13 14 15 |
# File 'lib/hubba/stats.rb', line 12 def initialize( full_name ) @data = {} @data['full_name'] = full_name # e.g. poole/hyde etc. end |
Instance Attribute Details
#data ⇒ Object (readonly)
todo/check: rename to GithubRepoStats or RepoStats - why? why not?
10 11 12 |
# File 'lib/hubba/stats.rb', line 10 def data @data end |
Instance Method Details
#fetch(gh) ⇒ Object
fetch / read / write methods
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hubba/stats.rb', line 48 def fetch( gh ) ## update stats / fetch data from github via api puts "fetching #{full_name}..." repo = gh.repo( full_name ) ## e.g. 2015-05-11T20:21:43Z ## puts Time.iso8601( repo.data['created_at'] ) @data['created_at'] = repo.data['created_at'] @data['size'] = repo.data['size'] # size in kb (kilobyte) rec = {} puts "stargazers_count" puts repo.data['stargazers_count'] rec['stargazers_count'] = repo.data['stargazers_count'] today = Date.today.strftime( '%Y-%m-%d' ) ## e.g. 2016-09-27 puts "add record #{today} to history..." pp rec # check if stargazers_count is a number (NOT a string) @data[ 'history' ] ||= {} @data[ 'history' ][ today ] = rec ########################## ## also check / keep track of (latest) commit commits = gh.repo_commits( full_name ) puts "last commit/update:" ## pp commits commit = { 'committer' => { 'date' => commits.data[0]['commit']['committer']['date'], 'name' => commits.data[0]['commit']['committer']['name'] }, 'message' => commits.data[0]['commit']['message'] } ## for now store only the latest commit (e.g. a single commit in an array) @data[ 'commits'] = [commit] pp @data end |
#full_name ⇒ Object
17 |
# File 'lib/hubba/stats.rb', line 17 def full_name() @data['full_name']; end |
#read(data_dir: './data') ⇒ Object
note: use read instead of load (load is kind of keyword for loading code)
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hubba/stats.rb', line 97 def read( data_dir: './data' ) ## note: use read instead of load (load is kind of keyword for loading code) ## note: skip reading if file not present basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde filename = "#{data_dir}/#{basename}.json" if File.exist?( filename ) puts "reading (loading) from #{basename}..." json = File.open( filename, 'r:utf-8' ) { |file| file.read } ## todo/fix: use read_utf8 @data = JSON.parse( json ) else puts "skipping reading (loading) from #{basename} -- file not found" end end |
#size ⇒ Object
20 21 22 23 |
# File 'lib/hubba/stats.rb', line 20 def size # size of repo in kb (as reported by github api) @data['size'] || 0 ## return 0 if not found - why? why not? (return nil - why? why not??) end |
#stars ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/hubba/stats.rb', line 25 def stars ## return last stargazers_count entry (as number; 0 if not found) t1 = 0 history = @data['history'] if history history_keys = @data['history'].keys.sort.reverse ## todo/fix: for now assumes one entry per week ## simple case [0] and [1] for a week later ## check actual date - why? why not? stats_t1 = history_keys[0] ? history[ history_keys[0] ] : nil if stats_t1 t1 = stats_t1['stargazers_count'] || 0 end end t1 end |
#write(data_dir: './data') ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/hubba/stats.rb', line 89 def write( data_dir: './data' ) basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde puts "writing (saving) to #{basename}..." File.open( "#{data_dir}/#{basename}.json", 'w:utf-8' ) do |f| f.write JSON.pretty_generate( data ) end end |