Class: Entry::Entry

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

Overview

Entity class to represent a standard logging entry of an nginx logfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_string) ⇒ Entry

initialization

Parameters:

  • input_string (String)

    the string representation of the log entry



27
28
29
30
31
32
33
34
35
36
# File 'lib/data/entry.rb', line 27

def initialize(input_string)
  @source = input_string[0]
  @remote_user = input_string[2]
  @timestamp = parse_timestamp(input_string[3], input_string[4])
  @http_request = input_string[5]
  @http_status = parse_integer(input_string[6])
  @element_size = parse_integer(input_string[7])
  @http_referer = input_string[8]
  @user_agent = input_string[9]
end

Instance Attribute Details

#element_sizeInteger (readonly)

Returns the size of the result in bytes.

Returns:

  • (Integer)

    the size of the result in bytes



19
20
21
# File 'lib/data/entry.rb', line 19

def element_size
  @element_size
end

#http_refererString (readonly)

Returns the site from where the request was issued.

Returns:

  • (String)

    the site from where the request was issued



21
22
23
# File 'lib/data/entry.rb', line 21

def http_referer
  @http_referer
end

#http_requestString (readonly)

Returns the http request.

Returns:

  • (String)

    the http request



15
16
17
# File 'lib/data/entry.rb', line 15

def http_request
  @http_request
end

#http_statusInteger (readonly)

Returns the http status.

Returns:

  • (Integer)

    the http status



17
18
19
# File 'lib/data/entry.rb', line 17

def http_status
  @http_status
end

#remote_userString (readonly)

Returns the information of the remote user.

Returns:

  • (String)

    the information of the remote user



11
12
13
# File 'lib/data/entry.rb', line 11

def remote_user
  @remote_user
end

#sourceString (readonly)

Returns the source address stored as a string.

Returns:

  • (String)

    the source address stored as a string



9
10
11
# File 'lib/data/entry.rb', line 9

def source
  @source
end

#timestampTime (readonly)

Returns the timestamp of the representing entry.

Returns:

  • (Time)

    the timestamp of the representing entry



13
14
15
# File 'lib/data/entry.rb', line 13

def timestamp
  @timestamp
end

#user_agentString (readonly)

Returns the used browser program or user agent.

Returns:

  • (String)

    the used browser program or user agent



23
24
25
# File 'lib/data/entry.rb', line 23

def user_agent
  @user_agent
end

Instance Method Details

#parse_integer(int_string) ⇒ Integer (private)

helper method to parse the given string to an integer

Parameters:

  • int_string (String)

    the given string

Returns:

  • (Integer)

    the parsed string



60
61
62
# File 'lib/data/entry.rb', line 60

def parse_integer(int_string)
  Integer(int_string)
end

#parse_timestamp(time_string, time_zone) ⇒ Time (private)

helper method to parse the timestamp from the time informations

Parameters:

  • time_string (String)

    the date information coded in a string

  • time_zone (String)

    the used timezone coded in a string

Returns:

  • (Time)

    the created time object



51
52
53
54
55
# File 'lib/data/entry.rb', line 51

def parse_timestamp(time_string, time_zone)
  time_string.slice!(0)
  time_zone.chomp("]")
  Time.strptime(time_string.concat(time_zone), "%d/%b/%Y:%T%z")
end

#to_stringString

method to get a formatted string

Returns:

  • (String)

    the formatted entry string



40
41
42
43
# File 'lib/data/entry.rb', line 40

def to_string
  "#{@source} #{@remote_user} #{@timestamp} #{@http_request} "\
  "#{@http_status} #{@element_size} byte #{@http_referer}\n#{@user_agent}"
end