Class: Nelumba::Atom::Entry

Inherits:
Atom::Entry
  • Object
show all
Includes:
Atom::SimpleExtensions
Defined in:
lib/nelumba/atom/entry.rb

Constant Summary collapse

THREAD_NAMESPACE =

The XML namespace that identifies the conforming specification of ‘thr’ elements.

"http://purl.org/syndication/thread/1.0"
ACTIVITY_NAMESPACE =

The XML namespace that identifies the conforming specification.

'http://activitystrea.ms/spec/1.0/'
SCHEMA_ROOT =

The XML schema that identifies the conforming schema for objects.

'http://activitystrea.ms/schema/1.0/'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_canonical(obj) ⇒ Object



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
135
136
137
138
139
140
141
142
143
144
145
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
174
175
176
177
# File 'lib/nelumba/atom/entry.rb', line 85

def self.from_canonical(obj)
  entry_hash = obj.to_hash

  # Ensure that the content type is encoded.
  object = obj.object

  title = object.title

  content = object.text
  content = object.html if object.html

  content_type = nil
  content_type = "html" if object.html

  if object.is_a? Nelumba::Note
  elsif object.is_a? Nelumba::Comment
    content = nil
    content_type = nil
    title = nil
    object = Nelumba::Atom::Comment.from_canonical(object.to_hash)
    entry_hash[:activity_object] = object
  else
    content = nil
    content_type = nil
    title = nil
    entry_hash[:activity_object] = object if object.is_a? Nelumba::Person
  end

  if content
    node = XML::Node.new("content")
    node['type'] = content_type if content_type
    node << content

    xml = XML::Reader.string(node.to_s)
    xml.read
    entry_hash[:content] = ::Atom::Content.parse(xml)
    entry_hash.delete :content_type
  end

  entry_hash[:title] = title if title

  if entry_hash[:source]
    entry_hash[:source] = Nelumba::Atom::Source.from_canonical(entry_hash[:source])
  end

  if entry_hash[:actor]
    entry_hash[:authors] = [Nelumba::Atom::Author.from_canonical(entry_hash[:actor])]
  end
  entry_hash.delete :actor

  # Encode in-reply-to fields
  entry_hash[:thr_in_reply_to] = entry_hash[:in_reply_to].map do |t|
    Nelumba::Atom::Thread.new(:href => t.url,
                            :ref  => t.uid)
  end
  entry_hash.delete :in_reply_to

  entry_hash[:links] ||= []

  if entry_hash[:url]
    entry_hash[:links] << ::Atom::Link.new(:rel => "self", :href => entry_hash[:url])
  end
  entry_hash.delete :url

  object_type = entry_hash[:type]
  if object_type
    entry_hash[:activity_object_type] = SCHEMA_ROOT + object_type.to_s
  end
  if entry_hash[:verb]
    entry_hash[:activity_verb] = SCHEMA_ROOT + entry_hash[:verb].to_s
  end
  entry_hash[:activity_target] = entry_hash[:target] if entry_hash[:target]

  entry_hash[:id] = entry_hash[:uid]
  entry_hash.delete :uid

  entry_hash.delete :object
  entry_hash.delete :verb
  entry_hash.delete :target
  entry_hash.delete :type

  entry_hash[:displayName] = entry_hash[:display_name]
  entry_hash.delete :display_name

  # Remove empty entries
  entry_hash.keys.each do |key|
    if entry_hash[key].nil? || entry_hash[key] == ""
      entry_hash.delete key
    end
  end

  self.new(entry_hash)
end

Instance Method Details



77
78
79
# File 'lib/nelumba/atom/entry.rb', line 77

def link
  link_list = links.group_by { |l| l.rel.intern if l.rel }
end

#link=(options) ⇒ Object



81
82
83
# File 'lib/nelumba/atom/entry.rb', line 81

def link= options
  links.clear << ::Atom::Link.new(options)
end

#to_canonicalObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/nelumba/atom/entry.rb', line 179

def to_canonical
  # Reform the activity type
  # TODO: Add new Base schema verbs
  object_type = self.activity_object_type
  if object_type && object_type.start_with?(SCHEMA_ROOT)
    object_type.gsub!(/^#{Regexp.escape(SCHEMA_ROOT)}/, "")
  end

  object_type = "note" if object_type == "status"

  if self.activity_object
    object = self.activity_object.to_canonical
  else
    case object_type
    when "note"
      object = Nelumba::Note.new(:html  => self.content.to_s,
                               :title => self.title)
    end
  end

  source = self.source
  source = source.to_canonical if source
  Nelumba::Activity.new(:actor        => self.authors ? self.authors.first.to_canonical : nil,
                      :uid          => self.id,
                      :url          => self.url,
                      :source       => source,
                      :display_name => self.displayName,
                      :in_reply_to  => self.thr_in_reply_to.map(&:to_canonical),
                      :object       => object,
                      :type         => object_type,
                      :verb         => self.activity_verb,
                      :target       => self.activity_target,
                      :published    => self.published,
                      :updated      => self.updated)
end

#urlObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nelumba/atom/entry.rb', line 65

def url
  if links.alternate
    links.alternate.href
  elsif links.self
    links.self.href
  else
    links.map.each do |l|
      l.href
    end.compact.first
  end
end