Class: PrettyChat::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
10
# File 'lib/pretty_chat.rb', line 5

def initialize(*names)
  abort "You must provide a list of names of the chat participants." if names.empty?
  self.input = `pbpaste`
  self.names = names
  self.output = []
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



3
4
5
# File 'lib/pretty_chat.rb', line 3

def input
  @input
end

#namesObject

Returns the value of attribute names.



3
4
5
# File 'lib/pretty_chat.rb', line 3

def names
  @names
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/pretty_chat.rb', line 3

def output
  @output
end

#paddingObject

Returns the value of attribute padding.



3
4
5
# File 'lib/pretty_chat.rb', line 3

def padding
  @padding
end

Instance Method Details

#parseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pretty_chat.rb', line 12

def parse
  names.each do |name|
    input.gsub!(/^#{name}\n/, "#{name}: ")
    input.gsub!(/^\d{1,2}:\d{2}\n/, "")
  end

  current_name = ""

  input.each_line do |line|
    matches = line.match(/^([\w\s]+): /)
    if matches
      current_name = matches[1]
    else
      line = "#{current_name}: #{line}"
    end
    output << line
  end

  output.join
end