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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gemverse/timeline.rb', line 174

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



191
192
193
194
195
196
197
198
199
200
# File 'lib/gemverse/timeline.rb', line 191

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



202
203
204
205
206
207
208
209
210
211
# File 'lib/gemverse/timeline.rb', line 202

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

#_gems_by_monthObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gemverse/timeline.rb', line 22

def _gems_by_month
   model = {}
   @versions.each do |rec|

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

    by_year  = model[ year ] ||= {}
    by_month = by_year[ month ] ||= { 'new'     => [],
                                      'updated' => [] }

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

#_gems_by_weekObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gemverse/timeline.rb', line 44

def _gems_by_week
   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
  model
end

#build_by_month(title: 'Timeline') ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
160
161
162
163
164
165
166
167
168
169
# File 'lib/gemverse/timeline.rb', line 120

def build_by_month( title: 'Timeline' )
 ## step 1 - build document model - versions by year & month
 ##                  and split into new & update
 model = _gems_by_month

 ## pp model

 ## step 2 - build document

 buf = String.new
 buf << "# #{title} \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_month|
  ## get totals for year
  gems_new = by_month.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_month|
  buf << "## #{year}\n\n"

  by_month.each do |month, gems|
    buf << "**Month #{month}**\n\n"

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

 buf
end

#build_by_week(title: 'Timeline') ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gemverse/timeline.rb', line 67

def build_by_week( title: 'Timeline' )
 ## step 1 - build document model - versions by year & week
 ##                  and split into new & update
 model = _gems_by_week

 ## pp model

 ## step 2 - build document

 buf = String.new
 buf << "# #{title} \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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/gemverse/timeline.rb', line 217

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, title: nil) ⇒ Object



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

def save( path, title: nil )
   buf = build_by_month( title: title )
   write_text( path, buf )
end