-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests-by-ip.rb
88 lines (83 loc) · 2.42 KB
/
requests-by-ip.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
requests =
File.open("access_log") do |file|
file
.map { |line| { ip: line.split(" ").at(0), url: line.split[5] } }
.group_by { |request| request[:ip] }
.each { |ip, requests| requests.map! { |r| r[:url] } }
end
file = File.open("access_log")
#line = '127.0.0.1 - [10/Oct/2014:13:55:36 +0000] "GET / HTTP/1.1" 200 561'
ip_urls = file.map do|line|{ip: line.split(" ").at(0), url: line.split[5]};end
# returns an array of hashes
# line.split[0] is really line.split()[0], or line.split(" ").at(0), or even line.split(nil).at(0)
# RubyDoc: If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ' were specified.
# irb(main):003:0> file.map do|line|{ip: line.split[0], url: line.split[5]};end
# [
# [0] {
# :ip => "127.0.0.1",
# :url => "/"
# },
# [1] {
# :ip => "127.0.0.1",
# :url => "/images/logo.png"
# },
# [2] {
# :ip => "192.168.0.42",
# :url => "/"
# },
# [3] {
# :ip => "192.168.0.91",
# :url => "/person.jpg"
# },
# [4] {
# :ip => "192.168.0.42",
# :url => "/about.html"
# }
# ]
requests = ip_urls.group_by{|request|request[:ip]}
# group_by will return a hash whose keys are unique ip
# the array associated with each key is the original, so we have duplication on :ip
# {
# "127.0.0.1" => [
# [0] {
# :ip => "127.0.0.1",
# :url => "/"
# },
# [1] {
# :ip => "127.0.0.1",
# :url => "/images/logo.png"
# }
# ],
# "192.168.0.42" => [
# [0] {
# :ip => "192.168.0.42",
# :url => "/"
# },
# [1] {
# :ip => "192.168.0.42",
# :url => "/about.html"
# }
# ],
# "192.168.0.91" => [
# [0] {
# :ip => "192.168.0.91",
# :url => "/person.jpg"
# }
# ]
# }
requests.each{|ip,requests|requests.map!{|r|r[:url]}}
# iterate through each ip/request pair, and only return the urls as array elements
# inner hash transformed to an array of request urls
# {
# "127.0.0.1" => [
# [0] "/",
# [1] "/images/logo.png"
# ],
# "192.168.0.42" => [
# [0] "/",
# [1] "/about.html"
# ],
# "192.168.0.91" => [
# [0] "/person.jpg"
# ]
#}