Class: LogParser
- Inherits:
-
Object
- Object
- LogParser
- Defined in:
- lib/log_parser.rb
Overview
Takes a log file where each line has the format of:
PAGE_NAME VISITOR_IP
eg.
/about 123.456.789.123
and turns it into a list of ‘page’ objects. The target object to turn each line in the log into is expected to be able to receive page: and visitor: keyword arguments.
Defined Under Namespace
Classes: MalformedLog
Instance Method Summary collapse
-
#initialize(page_class: Page) ⇒ LogParser
constructor
A new instance of LogParser.
-
#parse_from_file(file_path:) ⇒ Object
arranging by path in a hash first instead of straight into an Array to avoid an iteration of the whole list so far on each check for existence.
- #parse_from_string(string:) ⇒ Object
Constructor Details
Instance Method Details
#parse_from_file(file_path:) ⇒ Object
arranging by path in a hash first instead of straight into an Array to avoid an iteration of the whole list so far on each check for existence. Instead, using the quicker lookup by key to then later just extract the values into the list.
23 24 25 26 27 28 29 30 |
# File 'lib/log_parser.rb', line 23 def parse_from_file(file_path:) page_views = {}.tap do |pages| File.open(file_path).each do |line| build_page line, pages end end [].push(*page_views.values) end |
#parse_from_string(string:) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/log_parser.rb', line 32 def parse_from_string(string:) page_views = {}.tap do |pages| string.split("\n").each do |line| build_page line, pages end end [].push(*page_views.values) end |