Skip to content

Instantly share code, notes, and snippets.

@flada-auxv
Created December 3, 2013 15:39
Show Gist options
  • Save flada-auxv/7771338 to your computer and use it in GitHub Desktop.
Save flada-auxv/7771338 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
LOG_ITEMS = %w(ip_address ident_user_name authenticate_user_name access_date request_line status_code forwarding_byte caller_url user_agent).each
class LogParser
def initialize(logs)
@logs = logs.chomp
@result = {}
@start_idx = 0
end
def parse
@logs.each_char.with_index do |char, idx|
case char
when '['
@start_idx = idx
@bracket = :start
when ']'
@bracket = :end
when '"'
if @quote
@quote = :end
else
@start_idx = idx
@quote = :start
end
when ' '
next if @bracket == :start || @quote == :start
store(@start_idx, idx - @start_idx)
@start_idx = idx + 1
end
end
store(@start_idx, @logs.length)
@result
end
def store(nth, len)
@result[LOG_ITEMS.next] = @logs[nth, len]
@bracket = @quote = nil
end
end
p LogParser.new(ARGF.readlines[0]).parse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment