Module: Nelumba::Object
- Included in:
- Activity, Application, Article, Audio, Badge, Binary, Bookmark, Collection, Comment, Device, Event, File, Group, Image, Note, Place, Product, Question, Review, Service, Video
- Defined in:
- lib/nelumba/object.rb
Constant Summary collapse
- USERNAME_REGULAR_EXPRESSION =
Determines what constitutes a username inside an update text
/(^|[ \t\n\r\f"'\(\[{]+)@([^ \t\n\r\f&?=@%\/\#]*[^ \t\n\r\f&?=@%\/\#.!:;,"'\]}\)])(?:@([^ \t\n\r\f&?=@%\/\#]*[^ \t\n\r\f&?=@%\/\#.!:;,"'\]}\)]))?/
Instance Attribute Summary collapse
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#content ⇒ Object
readonly
Natural-language text content.
-
#display_name ⇒ Object
readonly
Returns the value of attribute display_name.
-
#html ⇒ Object
readonly
Holds the content as html.
-
#image ⇒ Object
readonly
The image representation of this object.
-
#published ⇒ Object
readonly
Returns the value of attribute published.
-
#summary ⇒ Object
readonly
Natural-language description of this object.
-
#text ⇒ Object
readonly
Holds the content as plain text.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #init(options = {}, &blk) ⇒ Object
- #initialize(options = {}, &blk) ⇒ Object
-
#mentions(&blk) ⇒ Object
Returns a list of Nelumba::Person’s for those mentioned within the object.
-
#reply_to(&blk) ⇒ Object
Returns a list of Nelumba::Person’s for those replied by the object.
- #to_as1(*args) ⇒ Object
- #to_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object
-
#to_html(&blk) ⇒ Object
Produces an HTML string representing the Object’s content.
-
#to_json(*args) ⇒ Object
Returns a string containing the JSON representation of this Object.
- #to_json_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object
-
#to_text ⇒ Object
TODO: Convert html to safe text.
Instance Attribute Details
#author ⇒ Object (readonly)
Returns the value of attribute author.
10 11 12 |
# File 'lib/nelumba/object.rb', line 10 def @author end |
#content ⇒ Object (readonly)
Natural-language text content
22 23 24 |
# File 'lib/nelumba/object.rb', line 22 def content @content end |
#display_name ⇒ Object (readonly)
Returns the value of attribute display_name.
11 12 13 |
# File 'lib/nelumba/object.rb', line 11 def display_name @display_name end |
#html ⇒ Object (readonly)
Holds the content as html.
31 32 33 |
# File 'lib/nelumba/object.rb', line 31 def html @html end |
#image ⇒ Object (readonly)
The image representation of this object
19 20 21 |
# File 'lib/nelumba/object.rb', line 19 def image @image end |
#published ⇒ Object (readonly)
Returns the value of attribute published.
24 25 26 |
# File 'lib/nelumba/object.rb', line 24 def published @published end |
#summary ⇒ Object (readonly)
Natural-language description of this object
16 17 18 |
# File 'lib/nelumba/object.rb', line 16 def summary @summary end |
#text ⇒ Object (readonly)
Holds the content as plain text.
28 29 30 |
# File 'lib/nelumba/object.rb', line 28 def text @text end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/nelumba/object.rb', line 9 def title @title end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
12 13 14 |
# File 'lib/nelumba/object.rb', line 12 def uid @uid end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
25 26 27 |
# File 'lib/nelumba/object.rb', line 25 def updated @updated end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
13 14 15 |
# File 'lib/nelumba/object.rb', line 13 def url @url end |
Instance Method Details
#init(options = {}, &blk) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nelumba/object.rb', line 37 def init( = {}, &blk) @author = [:author] @content = [:content] @display_name = [:display_name] @uid = [:uid] @url = [:url] @summary = [:summary] @published = [:published] || Time.now @updated = [:updated] || Time.now @title = [:title] || "Untitled" [:published] = @published [:updated] = @updated [:title] = @title # Alternative representations of 'content' @text = [:text] || @content || "" @content = @content || [:text] [:text] = @text @html = [:html] || to_html(&blk) @content = @content || @html [:html] = @html end |
#initialize(options = {}, &blk) ⇒ Object
33 34 35 |
# File 'lib/nelumba/object.rb', line 33 def initialize( = {}, &blk) init(, &blk) end |
#mentions(&blk) ⇒ Object
Returns a list of Nelumba::Person’s for those mentioned within the object.
Requires a block that is given two arguments: the username and the domain that should return a Nelumba::Person that matches when a @username tag is found.
Usage:
note = Nelumba::Note.new(:text => “Hello @foo”) note.mentions do |username, domain|
i = identities.select {|e| e.username == username && e.domain == domain }.first
i. if i
end
With a persistence backend: note.mentions do |username, domain|
i = Identity.first(:username => /^#{Regexp.escape(username)}$/i)
i. if i
end
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/nelumba/object.rb', line 221 def mentions(&blk) if self.respond_to? :text out = self.text || "" else out = self.content || "" end out = CGI.escapeHTML(out) # we let almost anything be in a username, except those that mess with urls. # but you can't end in a .:;, or ! # also ignore container chars [] () "" '' {} # XXX: the _correct_ solution will be to use an email validator ret = [] out.scan(USERNAME_REGULAR_EXPRESSION) do |beginning, username, domain| ret << blk.call(username, domain) if blk end ret end |
#reply_to(&blk) ⇒ Object
Returns a list of Nelumba::Person’s for those replied by the object.
Requires a block that is given two arguments: the username and the domain that should return a Nelumba::Person that matches when a @username tag is found.
Usage:
note = Nelumba::Note.new(:text => “Hello @foo”) note.reply_to do |username, domain|
i = identities.select {|e| e.username == username && e.domain == domain }.first
i. if i
end
With a persistence backend: note.reply_to do |username, domain|
i = Identity.first(:username => /^#{Regexp.escape(username)}$/i
i.author if i
end
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/nelumba/object.rb', line 131 def reply_to(&blk) out = CGI.escapeHTML(@text) # we let almost anything be in a username, except those that mess with urls. # but you can't end in a .:;, or ! # also ignore container chars [] () "" '' {} # XXX: the _correct_ solution will be to use an email validator ret = [] out.match(/^#{USERNAME_REGULAR_EXPRESSION}/) do |beginning, username, domain| ret << blk.call(username, domain) if blk end ret end |
#to_as1(*args) ⇒ Object
247 248 249 |
# File 'lib/nelumba/object.rb', line 247 def to_as1(*args) to_json_hash.delete_if{|k,v| v.nil?}.to_json(*args) end |
#to_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/nelumba/object.rb', line 146 def to_hash(scheme = 'https', domain = 'example.org', port = nil) url_start = "#{scheme}://#{domain}#{port.nil? ? "" : ":#{port}"}" uid = self.uid url = self.url if uid && uid.start_with?("/") uid = "#{url_start}#{uid}" end if url && url.start_with?("/") url = "#{url_start}#{url}" end { :author => self., :summary => self.summary, :content => self.content, :display_name => self.display_name, :uid => uid, :url => url, :published => self.published, :updated => self.updated, :title => self.title, :text => self.text, :html => self.html, } end |
#to_html(&blk) ⇒ Object
Produces an HTML string representing the Object’s content.
Requires a block that is given two arguments: the username and the domain that should return a Nelumba::Person that matches when a @username tag is found.
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 |
# File 'lib/nelumba/object.rb', line 76 def to_html(&blk) return @html if @html return "" if @text.nil? out = CGI.escapeHTML(@text) # Replace any absolute addresses with a link # Note: Do this first! Otherwise it will add anchors inside anchors! out.gsub!(/(http[s]?:\/\/\S+[a-zA-Z0-9\/}])/, "<a href='\\1'>\\1</a>") # we let almost anything be in a username, except those that mess with urls. # but you can't end in a .:;, or ! # also ignore container chars [] () "" '' {} # XXX: the _correct_ solution will be to use an email validator out.gsub!(USERNAME_REGULAR_EXPRESSION) do |match| if blk = blk.call($2, $3) end if "#{$1}<a href='#{.uri}'>@#{$2}</a>" else "#{$1}@#{$2}" end end out.gsub!( /(^|\s+)#(\p{Word}+)/ ) do |match| "#{$1}<a href='/search?search=%23#{$2}'>##{$2}</a>" end out.gsub!(/\n/, "<br/>") out end |
#to_json(*args) ⇒ Object
Returns a string containing the JSON representation of this Object.
243 244 245 |
# File 'lib/nelumba/object.rb', line 243 def to_json(*args) to_json_hash.delete_if{|k,v| v.nil?}.to_json(*args) end |
#to_json_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/nelumba/object.rb', line 175 def to_json_hash(scheme = 'https', domain = 'example.org', port = nil) url_start = "#{scheme}://#{domain}#{port.nil? ? "" : ":#{port}"}" uid = self.uid url = self.url if uid && uid.start_with?("/") uid = "#{url_start}#{uid}" end if url && url.start_with?("/") url = "#{url_start}#{url}" end { :author => self., :content => self.content, :summary => self.summary, :displayName => self.display_name, :id => uid, :url => url, :title => self.title, :published => (self.published ? self.published.to_date.rfc3339 + 'Z' : nil), :updated => (self.updated ? self.updated.to_date.rfc3339 + 'Z' : nil), } end |
#to_text ⇒ Object
TODO: Convert html to safe text
63 64 65 66 67 68 69 |
# File 'lib/nelumba/object.rb', line 63 def to_text() return @text if @text return "" if @html.nil? "" end |