Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Slightly saner parser abstraction

  • Loading branch information...
commit c9b8fe65403456e0fed1825bb8572ebe1aa6fa62 1 parent 0495e57
@jamesu authored
View
4 parsers/bip_parser.rb
@@ -9,6 +9,10 @@ def initialize(collection)
def reset
end
+ def parse(input)
+ parse_all_lines(input)
+ end
+
def extract_sender(name)
# either
# woot!~woot@pool-173-62-200-183.phlapa.fios.verizon.net:
View
22 parsers/colloquy_parser.rb
@@ -15,12 +15,30 @@ class ColloquyParser < Parser
def initialize(collection)
super(collection)
- @xpath = 'log'
- @type = :xml
@current_time = Time.now
@current_sender = nil
end
+ def parse(input)
+ data = Nokogiri::XML.parse(input.read)
+
+ # Parse XML
+ data.xpath('log').each do |log_node|
+ @collection.source = log_node['source']
+ log_node.xpath('*').each do |node|
+ event = parse_node(node)
+
+ unless event.nil?
+ if event.is_a?(Array)
+ event.each {|evt| @collection.add_event(evt)}
+ else
+ @collection.add_event(event)
+ end
+ end
+ end
+ end
+ end
+
def parse_node(entry)
content = nil
time = nil
View
4 parsers/ircii_parser.rb
@@ -24,6 +24,10 @@ def initialize(collection)
@current_time = Time.now
end
+ def parse(input)
+ parse_all_lines(input)
+ end
+
# returns [sender, content, is_action]
def extract_message(input)
sender = nil
View
73 parsers/parser.rb
@@ -1,65 +1,30 @@
class Parser
- attr_accessor :type
-
def initialize(collection)
- @type = :line
@collection = collection
end
- def parse(input)
- if @type == :xml
- data = Nokogiri::XML.parse(input.read)
-
- # Parse XML
- data.xpath(@xpath).each do |log_node|
- @collection.source = log_node['source']
- log_node.xpath('*').each do |node|
- event = parse_node(node)
-
- unless event.nil?
- if event.is_a?(Array)
- event.each {|evt| @collection.add_event(evt)}
- else
- @collection.add_event(event)
- end
- end
- end
- end
- elsif @type == :html
- data = Nokogiri::HTML.parse(input.read)
-
- # Parse HTML
- data.xpath(@xpath).each do |log_node|
- event = parse_node(log_node)
-
- unless event.nil?
- if event.is_a?(Array)
- event.each {|evt| @collection.add_event(evt)}
- else
- @collection.add_event(event)
- end
- end
- end
- else
- while true do
- begin
- line = input.readline
- rescue
- break
- end
-
- if line.nil?
- break
- end
-
- event = parse_line(line)
- unless event.nil?
- @collection.add_event(event)
- end
- end
+ def parse_all_lines(input)
+ while true do
+ begin
+ line = input.readline
+ rescue
+ break
+ end
+
+ if line.nil?
+ break
+ end
+
+ event = parse_line(line)
+ unless event.nil?
+ @collection.add_event(event)
+ end
end
end
+ def parse(input)
+ end
+
def parse_line(line)
end
View
16 parsers/talker_parser.rb
@@ -10,12 +10,22 @@ class TalkerParser < Parser
def initialize(collection)
super(collection)
- @xpath = '//script'
- @type = :html
@current_time = Time.now
@current_sender = nil
end
+ def parse(input)
+ event = parse_script(input.read)
+
+ unless event.nil?
+ if event.is_a?(Array)
+ event.each {|evt| @collection.add_event(evt)}
+ else
+ @collection.add_event(event)
+ end
+ end
+ end
+
def parse_event(event)
room = event['room']
user = event['user']
@@ -37,7 +47,7 @@ def parse_event(event)
end
def parse_script(content)
- match = content.match(/var talkerEvents = (\[[^\]]+\])/)
+ match = content.match(/var talkerEvents = (\[.*)$/)
if match
events = ActiveSupport::JSON.decode(match[1])
events.map do |event|
Please sign in to comment.
Something went wrong with that request. Please try again.