forked from noonat/vcd
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.rb
More file actions
97 lines (81 loc) · 2.36 KB
/
models.rb
File metadata and controls
97 lines (81 loc) · 2.36 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
require 'rubygems'
require 'digest/md5'
require 'dm-core'
require 'dm-timestamps'
require 'dm-types'
require 'dm-validations'
require 'hpricot'
DataMapper.setup(:default, {
:adapter => 'mysql',
:database => 'vcd',
:username => 'root',
:password => '',
:host => 'localhost'
})
class Vessel
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :ip, IPAddress
property :cfe, Text, :lazy => false
property :data, Text, :lazy => false
has n, :vessel_clicks
has n, :vessel_pilot_clicks
validates_present :cfe, :data
class <<self
def parse(data)
matched = (data =~ /<tt style=\"background-color: rgb\(0,0,0\)\">(.+)<\/tt><br\/><a href=\"http:\/\/www\.captainforever\.com\/captainsuccessor\.php\?cfe=([a-z0-9]+)\">Pilot this vessel<\/a>/m)
return nil if matched == nil
cfe = $2
data = Hpricot($1.gsub(/<([^;])/, '<\1'))
data.search('*').each do |node|
if node.elem?
case node.name.downcase
when 'br', 'span'
if node.attributes.respond_to? :delete_if
node.attributes.delete_if { |k,v| k.downcase != 'style' }
end
else
node.parent.children.delete(node)
end
elsif node.comment?
node.parent.children.delete(node)
end
end
data = data.to_s
return {:cfe=>cfe, :data=>data}
end
end
def data_trimmed
data.split('<br/>').find_all do |line|
line.gsub(/ /, '') != ''
end.join('<br/>')
end
def href
"/vessels/#{id}"
end
def md5
return '<null>' if ip.nil?
Digest::MD5.hexdigest(ip.to_s)
end
def pilot_href(track=false)
return "/vessels/#{id}/pilot" if track
"http://www.captainforever.com/captainsuccessor.php?cfe=#{cfe}"
end
end
class VesselClick
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :ip, IPAddress
property :referrer, URI, :length => 1024
belongs_to :vessel
end
class VesselPilotClick
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :ip, IPAddress
property :referrer, URI, :length => 1024
belongs_to :vessel
end