Class: CampfireExport::Transcript

Inherits:
Object
  • Object
show all
Includes:
IO
Defined in:
lib/campfire_export.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IO

#api_url, #export_dir, #export_file, #get, #log, #verify_export, #zero_pad

Constructor Details

#initialize(room, date) ⇒ Transcript

Returns a new instance of Transcript.



204
205
206
207
# File 'lib/campfire_export.rb', line 204

def initialize(room, date)
  @room     = room
  @date     = date
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



202
203
204
# File 'lib/campfire_export.rb', line 202

def date
  @date
end

#messagesObject

Returns the value of attribute messages.



202
203
204
# File 'lib/campfire_export.rb', line 202

def messages
  @messages
end

#roomObject

Returns the value of attribute room.



202
203
204
# File 'lib/campfire_export.rb', line 202

def room
  @room
end

#xmlObject

Returns the value of attribute xml.



202
203
204
# File 'lib/campfire_export.rb', line 202

def xml
  @xml
end

Instance Method Details

#exportObject



213
214
215
216
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
# File 'lib/campfire_export.rb', line 213

def export
  begin
    log(:info, "#{export_dir} ... ")
    @xml = Nokogiri::XML get("#{transcript_path}.xml").body
  rescue Exception => e
    log(:error, "transcript export for #{export_dir} failed", e)
  else
    @messages = xml.xpath('/messages/message').map do |message|
      CampfireExport::Message.new(message, room, date)
    end
  
    # Only export transcripts that contain at least one message.
    if messages.length > 0
      log(:info, "exporting transcripts\n")
      begin
        FileUtils.mkdir_p export_dir
      rescue Exception => e
        log(:error, "Unable to create #{export_dir}", e)
      else
        export_xml
        export_plaintext
        export_html
        export_uploads
      end
    else
      log(:info, "no messages\n")
    end      
  end
end

#export_htmlObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/campfire_export.rb', line 265

def export_html
  begin
    transcript_html = get(transcript_path)

    # Make the upload links in the transcript clickable from the exported 
    # directory layout.
    transcript_html.gsub!(%Q{href="/room/#{room.id}/uploads/},
                          %Q{href="uploads/})
    # Likewise, make the image thumbnails embeddable from the exported
    # directory layout.
    transcript_html.gsub!(%Q{src="/room/#{room.id}/thumb/},
                          %Q{src="thumbs/})
    
    export_file(transcript_html, 'transcript.html')
    verify_export('transcript.html', transcript_html.length)
  rescue Exception => e
    log(:error, "HTML transcript export for #{export_dir} failed", e)
  end
end

#export_plaintextObject



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/campfire_export.rb', line 252

def export_plaintext
  begin
    date_header = date.strftime('%A, %B %e, %Y').squeeze(" ")
    plaintext = "#{CampfireExport::Account.subdomain.upcase} CAMPFIRE\n"
    plaintext << "#{room.name}: #{date_header}\n\n"
    messages.each {|message| plaintext << message.to_s }
    export_file(plaintext, 'transcript.txt')
    verify_export('transcript.txt', plaintext.length)
  rescue Exception => e
    log(:error, "Plaintext transcript export for #{export_dir} failed", e)
  end
end

#export_uploadsObject



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/campfire_export.rb', line 285

def export_uploads
  messages.each do |message|
    if message.is_upload?
      begin
        message.upload.export
      rescue Exception => e
        path = "#{message.upload.export_dir}/#{message.upload.filename}"
        log(:error, "Upload export for #{path} failed", e)
      end
    end
  end
end

#export_xmlObject



243
244
245
246
247
248
249
250
# File 'lib/campfire_export.rb', line 243

def export_xml
  begin
    export_file(xml, 'transcript.xml')
    verify_export('transcript.xml', xml.to_s.length)
  rescue Exception => e
    log(:error, "XML transcript export for #{export_dir} failed", e)
  end
end

#transcript_pathObject



209
210
211
# File 'lib/campfire_export.rb', line 209

def transcript_path
  "/room/#{room.id}/transcript/#{date.year}/#{date.mon}/#{date.mday}"
end