Class: Mess::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/mess/chat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Chat

Returns a new instance of Chat.

Raises:



16
17
18
19
20
21
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
# File 'lib/mess/chat.rb', line 16

def initialize path
  @path = File.expand_path(path)

  # make sure the path is somewhat valid
  raise ChatInvalidError unless File.exists? @path

  # maybe given a file within the directory instead? -> scope out
  @path = File.dirname(@path) if File.file? @path

  # prepare data buffers
  @title = nil
  @usrs  = nil
  @msgs  = Array.new

  # either .json or .html -> message_1.ext to find out the format
  message_1 = Dir["#@path/message_1\\.*"]
  raise ChatInvalidError unless message_1.one?
  message_1 = message_1.first
  extension = File.extname(message_1)
  extension = extension[1..-1] # remove the dot in .ext

  # reference the get_extension function
  get = method("get_#{extension}")

  # iterate through message_X.ext and use get method to parse the files
  # do it chronologically -> descending X for messenger (reeee)
  total = Dir["#@path/message_*\\.#{extension}"].size
  total.downto(1) do |i|
    file = "#@path/message_#{i}.#{extension}"
    get.call(file) #rescue raise ChatInvalidError
  end

  @title = '*you*' if @title.nil? or @title.empty?
  @msgs.sort_by!(&:time)
  @usrs.sort!
end

Instance Attribute Details

#msgsObject (readonly)

Returns the value of attribute msgs.



14
15
16
# File 'lib/mess/chat.rb', line 14

def msgs
  @msgs
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/mess/chat.rb', line 14

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/mess/chat.rb', line 14

def title
  @title
end

#usrsObject (readonly)

Returns the value of attribute usrs.



14
15
16
# File 'lib/mess/chat.rb', line 14

def usrs
  @usrs
end

Instance Method Details

#sizeObject



53
54
55
# File 'lib/mess/chat.rb', line 53

def size
  @msgs.size
end