Class: CampfireExport::Message

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(message, room, date) ⇒ Message



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/campfire_export.rb', line 304

def initialize(message, room, date)
  @id = message.css('id').text
  @room = room
  @date = date
  @body = message.css('body').text
  @type = message.css('type').text

  time = Time.parse message.css('created-at').text
  localtime = CampfireExport::.timezone.utc_to_local(time)
  @timestamp = localtime.strftime '%I:%M %p'

  no_user = ['TimestampMessage', 'SystemMessage', 'AdvertisementMessage']
  unless no_user.include?(@type)
    @user = username(message.css('user-id').text)
  end
  
  @upload = CampfireExport::Upload.new(self) if is_upload?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



302
303
304
# File 'lib/campfire_export.rb', line 302

def body
  @body
end

#dateObject

Returns the value of attribute date.



302
303
304
# File 'lib/campfire_export.rb', line 302

def date
  @date
end

#idObject

Returns the value of attribute id.



302
303
304
# File 'lib/campfire_export.rb', line 302

def id
  @id
end

#roomObject

Returns the value of attribute room.



302
303
304
# File 'lib/campfire_export.rb', line 302

def room
  @room
end

#timestampObject

Returns the value of attribute timestamp.



302
303
304
# File 'lib/campfire_export.rb', line 302

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



302
303
304
# File 'lib/campfire_export.rb', line 302

def type
  @type
end

#uploadObject

Returns the value of attribute upload.



302
303
304
# File 'lib/campfire_export.rb', line 302

def upload
  @upload
end

#userObject

Returns the value of attribute user.



302
303
304
# File 'lib/campfire_export.rb', line 302

def user
  @user
end

Instance Method Details

#indent(string, count) ⇒ Object



345
346
347
# File 'lib/campfire_export.rb', line 345

def indent(string, count)
  (' ' * count) + string.gsub(/(\n+)/) { $1 + (' ' * count) }
end

#is_upload?Boolean



341
342
343
# File 'lib/campfire_export.rb', line 341

def is_upload?
  @type == 'UploadMessage'
end

#to_sObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/campfire_export.rb', line 349

def to_s
  case type
  when 'EnterMessage'
    "[#{user} has entered the room]\n"
  when 'KickMessage', 'LeaveMessage'
    "[#{user} has left the room]\n"
  when 'TextMessage'
    "[#{user.rjust(12)}:] #{body}\n"
  when 'UploadMessage'
    "[#{user} uploaded: #{body}]\n"
  when 'PasteMessage'
    "[" + "#{user} pasted:]".rjust(14) + "\n#{indent(body, 16)}\n"
  when 'TopicChangeMessage'
    "[#{user} changed the topic to: #{body}]\n"
  when 'ConferenceCreatedMessage'
    "[#{user} created conference: #{body}]\n"
  when 'AllowGuestsMessage'
    "[#{user} opened the room to guests]\n"
  when 'DisallowGuestsMessage'
    "[#{user} closed the room to guests]\n"
  when 'LockMessage'
    "[#{user} locked the room]\n"
  when 'UnlockMessage'
    "[#{user} unlocked the room]\n"
  when 'IdleMessage'
    "[#{user} became idle]\n"
  when 'UnidleMessage'
    "[#{user} became active]\n"
  when 'TweetMessage'
    "[#{user} tweeted:] #{body}\n"
  when 'SoundMessage'
    "[#{user} played a sound:] #{body}\n"
  when 'TimestampMessage'
    "--- #{timestamp} ---\n"
  when 'SystemMessage'
    ""
  when 'AdvertisementMessage'
    ""
  else
    log(:error, "unknown message type: #{type} - '#{body}'")
    ""
  end
end

#username(user_id) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/campfire_export.rb', line 323

def username(user_id)
  @@usernames          ||= {}
  @@usernames[user_id] ||= begin
    doc = Nokogiri::XML get("/users/#{user_id}.xml").body
  rescue Exception => e
    "[unknown user]"
  else
    # Take the first name and last initial, if there is more than one name.
    name_parts = doc.css('name').text.split
    if name_parts.length > 1
      name_parts[-1] = "#{name_parts.last[0,1]}."
      name_parts.join(" ")
    else
      name_parts[0]
    end
  end
end