Class: LogEntry

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

Constant Summary collapse

TIME_FORMAT =
'%d/%b/%Y:%H:%M:%S'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ LogEntry

Returns a new instance of LogEntry.



8
9
10
# File 'lib/log_entry.rb', line 8

def initialize(attrs)
  @ip, @resp_code, @time = attrs[:ip], attrs[:resp_code], attrs[:time]
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



6
7
8
# File 'lib/log_entry.rb', line 6

def ip
  @ip
end

#resp_codeObject (readonly)

Returns the value of attribute resp_code.



6
7
8
# File 'lib/log_entry.rb', line 6

def resp_code
  @resp_code
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/log_entry.rb', line 6

def time
  @time
end

Class Method Details

.parse(line) ⇒ Object

Format here is definitely for nginx logs, but easily adaptable

192.168.0.128 - - [25/Jan/2019:06:57:32 +0000] "GET / HTTP/1.1" 200 708 "-" "Awsome Browser"


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/log_entry.rb', line 14

def self.parse(line)
  ip_r   = '(\d+\.\d+\.\d+\.\d+)'
  date_r = '\[(.*?)\]'
  str_r  = '"(.*?)"'
  num_r  = '(\d+)'
  regex  = /#{ip_r} - - #{date_r} #{str_r} #{num_r} #{num_r} #{str_r} #{str_r}/

  if(line =~ regex)
    ip, time, req, resp_code, bytes, _, agent = IP.new($1), $2, $3, $4, $5, $6, $7
    time = Time.strptime(time, TIME_FORMAT)

    { ip: ip, resp_code: resp_code, time: time }
  end
end

Instance Method Details

#redirect?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/log_entry.rb', line 29

def redirect?
  @resp_code == '301'
end