-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuotedArticle.rb
73 lines (64 loc) · 1.89 KB
/
QuotedArticle.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
#!/usr/bin/env ruby
require "rubygems"
require 'time'
class QuotedArticle
# properties created by couchDB (taken from CouchPotato)
attr_accessor :_id
attr_accessor :_rev
attr_accessor :_deleted
# own properties
attr_accessor :title
attr_accessor :url
attr_accessor :short_url
attr_accessor :springer_id
attr_accessor :highlightings
attr_accessor :clicks
attr_accessor :created_at
# initialize a new instance of this Class with the given attributes
# TODO: this comes from CouchPotato.
def initialize(attributes = {})
# use the current time as a creation time.
# if a time is set in the attributes hash, then this time will be overwritten
@created_at = Time.now.strftime("%Y/%m/%d %H:%M:%S")
# set all attributes from the attributes hash
if attributes
attributes.each do |name, value|
self.send("#{name}=", value)
end
end
yield self if block_given?
end
# it trying to set the created_at field with a string argument, convert it to a time (coming from Couch)
def created_at=(val)
if val.is_a?(String)
@created_at = Time.parse(val)
else
@created_at = val
end
end
# return the qrcode of bitly for this document
# wrap this, so that I could more easily change this if I should ever decide to generate my qrcodes differently
def qrcode
"#{self.short_url}.qrcode"
end
# a shortened version of the title that fits the article listing in route /
def shortened_title
if title.size < 120
return title
else
return "#{title.slice(0,120)}..."
end
end
# serialize object to JSON
def to_json(*a)
{
'title' => @title,
'url' => @url,
'short_url' => @short_url,
'springer_id' => @springer_id,
'highlightings' => @highlightings,
'clicks' => @clicks,
'created_at' => @created_at,
}.to_json(*a)
end
end