class Pipio::BasicParser

Public Class Methods

new(source_file_path, my_aliases, line_regex, line_regex_status, cleaner) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 3
def initialize(source_file_path, my_aliases, line_regex, line_regex_status, cleaner)
  @my_aliases = my_aliases
  @line_regex = line_regex
  @line_regex_status = line_regex_status
  @my_alias = @my_aliases.first

  @file_reader = FileReader.new(source_file_path, cleaner)
end

Public Instance Methods

create_message(match_data) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 43
def create_message(match_data)
  # Either a regular message line or an auto-reply/away message.
  time = time_parser.parse(match_data[:timestamp])
  if time
    my_alias = match_data[:sn_or_alias]
    my_screen_name = @alias_registry[my_alias]
    body = match_data[:body]
    is_auto_reply = match_data[:auto_reply]

    AutoOrXmlMessageCreator.new(body, time, my_screen_name, my_alias, is_auto_reply).create
  end
end
create_status_or_event_message(match_data) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 56
def create_status_or_event_message(match_data)
  time = time_parser.parse(match_data[:timestamp])
  str = match_data[:body]

  if time && event_we_care_about?(str)
    create_status_message(str, time) || create_event_message(str, time)
  end
end
parse() click to toggle source

This method returns a Chat instance, or false if it could not parse the file.

# File lib/pipio/parsers/basic_parser.rb, line 14
def parse
  if pre_parse
    messages = @file_reader.other_lines.map do |line|
      basic_message_match =  @line_regex.match(line)
      meta_message_match = @line_regex_status.match(line)
      if basic_message_match
        create_message(basic_message_match)
      elsif meta_message_match
        create_status_or_event_message(meta_message_match)
      end
    end

    Chat.new(messages, @metadata)
  end
end
pre_parse() click to toggle source

Extract required data from the file. Run by parse.

# File lib/pipio/parsers/basic_parser.rb, line 31
def pre_parse
  @file_reader.read
  metadata = Metadata.new(MetadataParser.new(@file_reader.first_line).parse)
  if metadata.valid?
    @metadata = metadata
    @alias_registry = AliasRegistry.new(@metadata.their_screen_name)
    @my_aliases.each do |my_alias|
      @alias_registry[my_alias] = @metadata.my_screen_name
    end
  end
end
time_parser() click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 65
def time_parser
  @time_parser ||= TimeParser.new(@metadata.start_year, @metadata.start_month, @metadata.start_mday)
end

Private Instance Methods

create_event_message(text, time) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 75
def create_event_message(text, time)
  EventMessageCreator.new(text, time, @my_alias, @metadata.my_screen_name, @alias_registry).create
end
create_status_message(text, time) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 79
def create_status_message(text, time)
  StatusMessageCreator.new(text, time, @alias_registry).create
end
event_we_care_about?(str) click to toggle source
# File lib/pipio/parsers/basic_parser.rb, line 71
def event_we_care_about?(str)
  Event::IGNORE.none? { |regex| str =~ regex }
end