Class: Gems::Cache
- Inherits:
-
Object
- Object
- Gems::Cache
- Defined in:
- lib/gemverse/cache.rb
Instance Method Summary collapse
-
#initialize(basedir = './gems') ⇒ Cache
constructor
A new instance of Cache.
- #read_versions(gems: []) ⇒ Object
- #update_versions(gems: []) ⇒ Object
Constructor Details
#initialize(basedir = './gems') ⇒ Cache
Returns a new instance of Cache.
5 6 7 |
# File 'lib/gemverse/cache.rb', line 5 def initialize( basedir='./gems' ) @basedir = basedir end |
Instance Method Details
#read_versions(gems: []) ⇒ Object
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 88 89 90 91 92 93 94 95 |
# File 'lib/gemverse/cache.rb', line 56 def read_versions( gems: [] ) ## read in and merge all version records versions = [] ## if no gems passed in - get all versions.csv datasets in basedir if gems.empty? paths = Dir.glob( "#{@basedir}/*/versions.csv" ) gems = paths.map { |path| File.basename(File.dirname(path)) } end gems.each_with_index do |gem,i| name = if gem.is_a?( String ) gem elsif gem.is_a?( Hash ) gem['name'] else ## assume our own Gem struct/class for now gem.name end path = "#{@basedir}/#{name}/versions.csv" puts "==> #{i+1}/#{gems.size} reading #{name}..." recs = read_csv( path ) recs.reverse.each_with_index do |rec,n| more = { 'count' => (n+1).to_s } ## auto-add version count(er) e.g. 1,2,3,... versions << rec.merge( more ) end puts " #{recs.size} record(s)" end ## sort by version created and name versions = versions.sort do |l,r| res = r['created'] <=> l['created'] res = l['name'] <=> r['name'] if res == 0 res = r['version'] <=> l['version'] if res == 0 res end versions end |
#update_versions(gems: []) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gemverse/cache.rb', line 10 def update_versions( gems: [] ) delay_in_s = 0.5 gems.each_with_index do |gem,i| name = if gem.is_a?( String ) gem elsif gem.is_a?( Hash ) gem['name'] else ## assume our own Gem struct/class for now gem.name end puts "==> #{i+1}/#{gems.size} #{name}..." ## update versions info puts " sleeping #{delay_in_s} second(s)" sleep( delay_in_s ) data = Gems::API.versions( name ) puts " #{data.size} version record(s) found" headers = ['name', 'version', 'created', 'downloads'] recs = [] data.each do |h| ## only use year/month/day for now now hours etc. created = Date.strptime( h['created_at'], '%Y-%m-%d' ) rec = [ name, h['number'], created.strftime( '%Y-%m-%d' ), h['downloads_count'].to_s, ] recs << rec end write_csv( "#{@basedir}/#{name}/versions.csv", [headers]+recs ) end end |