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

Returns a new instance of Message.



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

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::Account.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.



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

def body
  @body
end

#dateObject

Returns the value of attribute date.



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

def date
  @date
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#roomObject

Returns the value of attribute room.



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

def room
  @room
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#uploadObject

Returns the value of attribute upload.



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

def upload
  @upload
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#indent(string, count) ⇒ Object



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

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

#is_upload?Boolean

Returns:

  • (Boolean)


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

def is_upload?
  @type == 'UploadMessage'
end

#to_sObject



348
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
# File 'lib/campfire_export.rb', line 348

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



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

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