-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisit.rb
More file actions
134 lines (122 loc) · 3.03 KB
/
visit.rb
File metadata and controls
134 lines (122 loc) · 3.03 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require "uri"
class Visit
attr_reader :id, :ip, :url, :title, :city, :region, :country,
:country_code, :user_agent, :page, :referrer, :time,
:anonymous_proxy
def initialize(args)
@id = args["id"]
@ip = args["ip"]
@new_visitor = args["new_visitor"]
@url = args["url"]
@title = args["title"]
@city = args["city"]
@region = args["region"]
@country = args["country"]
@country_code = args["country_code"]
@anonymous_proxy = args["anonymous_proxy"]
@user_agent= args["user_agent"]
@page = args["page"]
@referrer = args["referrer"]
@time = args["time"] || Time.now.to_i
end
def to_hash
{
"id" => @id,
"ip" => @ip,
"new_visitor" => @new_visitor,
"url" => @url,
"title" => @title,
"city" => @city,
"region" => @region,
"country" => @country,
"country_code" => @country_code,
"anonymous_proxy" => @anonymous_proxy,
"user_agent" => @user_agent,
"page" => @page,
"referrer" => @referrer,
"time" => @time,
}
end
def same_visitor?(other)
if self.new? || other.new?
self.ip == other.ip
else
self.id == other.id
end
end
def new?
@new_visitor
end
def where
result =
[@city,
["US", "CA"].include?(@country_code) && @region,
@country].select{|x|x}.join(", ")
result != "" ? result : nil
end
def when
seconds = Time.now.to_i - @time
return ago(seconds, "second") if seconds < 60
minutes = seconds / 60
return ago(minutes, "minute") if minutes < 60
hours = minutes / 60
return ago(hours, "hour") if hours < 24
days = hours / 24
return ago(days, "day") if days < 30
months = seconds / 2628000
return ago(months, "month") if months < 12
years = days / 365
return ago(years, "year")
end
def ago(delta, unit)
"#{delta} #{unit}#{delta == 1 ? "" : "s"} ago"
end
def display_title
case
when title
# Get rid of the blog title (everything before the first colon), if any
result = title.sub(/^.*?:\s*/, "")
if url && url =~ %r{/search/label/}
result = ["posts tagged", result]
end
result
when url && url =~ %r{\?updated-min=(\d{4})}
$1
else
"a page"
end
end
@@whitelist_params = [
"updated-min", "updated-max", "max-results", "reverse-paginate"
]
def link_url
if url
u = URI.parse(url)
klass = (u.scheme == "https") ? URI::HTTPS : URI::HTTP
# Don't include the fragment.
klass.build(host: u.host, port: u.port, path: u.path,
query: sanitize_query(u.query))
end
end
def sanitize_query(q)
if q
q = q.split("&").map do |p|
param = p.split("=")
if @@whitelist_params.include?(param.first)
if param.first == "max-results"
"max-results=3"
else
param.join("=")
end
else
nil
end
end.compact
else
q = []
end
q << "fav=r"
q = q.join("&")
q == "" ? nil : q
end
end