Class: Gems::Timeline

Inherits:
Object
  • Object
show all
Defined in:
lib/gemverse/timeline.rb

Instance Method Summary collapse

Constructor Details

#initialize(versions) ⇒ Timeline

Returns a new instance of Timeline.



5
6
7
8
9
10
11
12
13
# File 'lib/gemverse/timeline.rb', line 5

def initialize( versions )
  ## 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
end

Instance Method Details

#_build_gem(gem) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gemverse/timeline.rb', line 91

def _build_gem( gem )
  date =  Date.strptime( gem['created'], '%Y-%m-%d' )

  buf = String.new
  buf << "[**"
  buf << gem['name']
  buf << "**]"
  buf << "("
  buf << "https://rubygems.org/gems/#{gem['name']}/versions/#{gem['version']}"
  buf << " "
  buf << %Q{"update no.#{gem['count']} @ #{date.strftime('%a %d %b %Y')}"}
  buf << ") "
  buf << gem['version']
  buf
end

#_build_gems_new(gems) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/gemverse/timeline.rb', line 108

def _build_gems_new( gems )
  buf = String.new
  if gems.size > 0
    buf << "**NEW!** - #{gems.size} Gem(s) - "
    buf <<  gems.map do |gem| _build_gem( gem )
                     end.join( ', ' )
    buf << "\n\n"
  end
  buf
end

#_build_gems_updated(gems) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/gemverse/timeline.rb', line 119

def _build_gems_updated( gems )
  buf = String.new
  if gems.size > 0
    buf << "#{gems.size} Update(s)  - "
    buf <<  gems.map do |gem| _build_gem( gem )
                     end.join( ', ' )
   buf << "\n\n"
  end
  buf
end

#buildObject



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
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
88
# File 'lib/gemverse/timeline.rb', line 22

def build
 ## step 1 - build document model - versions by year & week
 ##                  and split into new & update
 model = {}

 @versions.each do |rec|

   date = Date.strptime( rec['created'], '%Y-%m-%d' )
   year = date.year.to_s
   week = date.strftime('%V')  ## note: return zero-padded string e.g. 01, 02, etc.

   by_year = model[ year ] ||= {}
   by_week = by_year[ week ] ||= { 'new'     => [],
                                   'updated' => [] }

   if rec['count'].to_i == 1
      by_week[ 'new' ] << rec
   else
      by_week[ 'updated' ] << rec
   end
 end

 ## pp model

 ## step 2 - build document

 buf = String.new
 buf << "# Timeline \n\n"

 ## add breadcrumps for years
 buf << model.keys.map do |year|
               "[#{year}](##{year})"
        end.join( ' · ' )
 buf << "\n\n"

 ## add new gems by year
 buf << "## New Gems By Year\n\n"

 model.each do |year, by_week|
  ## get totals for year
  gems_new = by_week.values.reduce( [] ) do |acc,gems|
    acc + gems['new']
  end

  buf << "**#{year}** - "

  buf <<   if gems_new.size > 0
              _build_gems_new( gems_new )
           else
             "Ø \n\n"
           end
 end


 model.each do |year, by_week|
  buf << "## #{year}\n\n"

  by_week.each do |week, gems|
    buf << "**Week #{week}**\n\n"

    buf << _build_gems_new( gems['new'] )
    buf << _build_gems_updated( gems['updated'] )
  end
 end

 buf
end

#export(path) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gemverse/timeline.rb', line 134

def export( path )

  headers = ['week',
         'name',
         'version',
         'count',
         'created',
         'downloads']
 recs = []
 @versions.each_with_index do |h,i|

   date = Date.strptime( h['created'], '%Y-%m-%d' )

   rec = [
      date.strftime('%Y/%V'),
      h['name'],
      h['version'],
      h['count'],
      date.strftime( '%Y-%m-%d' ),  # add (%a) for (Sun), (Mon) or such - why? why not?
      h['downloads'],
    ]
    recs << rec
  end

  write_csv( path, [headers]+recs )
end

#save(path) ⇒ Object



16
17
18
19
# File 'lib/gemverse/timeline.rb', line 16

def save( path )
   buf = build
   write_text( path, buf )
end