This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.rb
108 lines (91 loc) · 2.74 KB
/
entry.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require './database.rb'
require 'nokogiri'
class Entry
attr_accessor :id
attr_accessor :title
attr_accessor :url
attr_accessor :content
attr_accessor :feed_id
attr_accessor :date
attr_accessor :user
def initialize(title: nil, url: nil, content: nil, feed_id: nil, id: nil, date: nil, user:)
self.user = user
self.title = title
self.url = url
self.content = content
self.feed_id = feed_id
self.id = id
self.date = date
if id && (! title || ! url || ! content || ! feed_id)
self.initializeById(id: id)
end
end
def initializeById(id:)
data = Database.new.getEntryData(id: id)
self.title = data["title"]
self.url = data["url"]
self.content = data["content"]
self.feed_id = data["feed"]
self.date = data["date"]
end
def save!
self.id = Database.new.addEntry(self, feed_id) || self.id
return self
end
def read!
Database.new.setRead(true, self)
end
def read?
Database.new.read?(self)
end
def mark!
Database.new.setMark(true, self)
end
def marked?
return Database.new.marked?(self)
end
def unmark!
Database.new.setMark(false, self)
end
def unread!
Database.new.setRead(false, self)
end
# This gets the feed from the database. Minimize usage, use feed_id wherever possible
def feed
Feed.new(id: feed_id, user: user)
end
# see http://stackoverflow.com/a/15910738/2508518
# we are guessing here that relative links can be transformed to absolute urls
# using the adress of the blog itself. This might fail.
def contentWithAbsLinks
return if ! self.content
return content if ! self.url
blog_uri = URI.parse(URI.escape(self.url))
tags = {
'img' => 'src',
'a' => 'href'
}
doc = Nokogiri::HTML(self.content)
doc.search(tags.keys.join(',')).each do |node|
url_param = tags[node.name]
src = node[url_param]
unless (src.nil? || src.empty? || src.start_with?("mailto:"))
begin
uri = URI.parse(URI.escape(src))
unless uri.host
uri.scheme = blog_uri.scheme
uri.host = blog_uri.host
node[url_param] = uri.to_s
end
rescue URI::InvalidURIError => e
end
end
end
begin
return doc.at('body').inner_html
rescue NoMethodError => nme
warn "Could not get entry: #{nme}"
return ""
end
end
end