Class: Atomic::Atom::Feed

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/atomic/atom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#deserialize, included

Constructor Details

#initialize(params = {}) ⇒ Feed

Returns a new instance of Feed.


214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/atomic/atom.rb', line 214

def initialize(params = {})
  params.symbolize_keys!
  params.assert_valid_keys(:id, :title, :subtitle, :updated_at, :links, :rights, :generator, :entries)
  self.id = params[:id]
  self.title = params[:title]
  self.subtitle = params[:subtitle]
  self.updated_at = params[:updated_at]
  self.links = params[:links] || []
  self.rights = params[:rights]
  self.generator = params[:generator]
  self.entries = params[:entries] || []
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def entries
  @entries
end

#generatorObject

Returns the value of attribute generator.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def generator
  @generator
end

#idObject

Returns the value of attribute id.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def id
  @id
end

Returns the value of attribute links.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def links
  @links
end

#rightsObject

Returns the value of attribute rights.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def rights
  @rights
end

#subtitleObject

Returns the value of attribute subtitle.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def subtitle
  @subtitle
end

#titleObject

Returns the value of attribute title.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.


212
213
214
# File 'lib/atomic/atom.rb', line 212

def updated_at
  @updated_at
end

Instance Method Details

#handle_close_element(node) ⇒ Object


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/atomic/atom.rb', line 253

def handle_close_element(node)
  case [node.depth, node.uri, node.name]
  when [0, NS_ATOM, 'feed']
  when [1, NS_ATOM, 'title']
    self.title = node.text
  when [1, NS_ATOM, 'subtitle']
    self.subtitle = node.text
  when [1, NS_ATOM, 'updated']
    self.updated_at = Time.parse(node.text)
  when [1, NS_ATOM, 'id']
    self.id = node.text
  when [1, NS_ATOM, 'link']
    self.links << {:href => node.attributes['href'], :rel => node.attributes['rel'], :type => node.attributes['type']}
  when [1, NS_ATOM, 'rights']
    self.rights = node.text
  when [1, NS_ATOM, 'generator']
    self.generator = node.text
  when [1, NS_ATOM, 'entry']
  else
    puts("Feed ==> Unexpected Close Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect} #{node.text}")
  end
end

#handle_open_element(node, reader) ⇒ Object


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/atomic/atom.rb', line 233

def handle_open_element(node, reader)
  progressed = false
  case [node.depth, node.uri, node.name]
  when [0, NS_ATOM, 'feed']
  when [1, NS_ATOM, 'title']
  when [1, NS_ATOM, 'subtitle']
  when [1, NS_ATOM, 'updated']
  when [1, NS_ATOM, 'id']
  when [1, NS_ATOM, 'link']
  when [1, NS_ATOM, 'rights']
  when [1, NS_ATOM, 'generator']
  when [1, NS_ATOM, 'entry']
    self.entries << Entry.new.deserialize(reader)
    progressed = true
  else
    puts("Feed ==> Unexpected Open Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect}")
  end
  return progressed
end

#to_hashObject


276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/atomic/atom.rb', line 276

def to_hash
  {
    :id => self.id,
    :title => self.title,
    :subtitle => self.subtitle,
    :updated_at => self.updated_at,
    :links => self.links,
    :rights => self.rights,
    :generator => self.generator,
    :entries => self.entries.collect { |entry| entry.to_hash }
  }
end

#to_xml(as_document = true, target = nil) ⇒ Object


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/atomic/atom.rb', line 289

def to_xml(as_document=true, target=nil)
  nsdec = as_document ? {:"xmlns:atom" => NS_ATOM} : {}
  xml = Builder::XmlMarkup.new(:target => target)
  xml.instruct! if as_document
  xml.atom(:feed, nsdec) do
    xml.atom(:id, self.id)
    xml.atom(:title, self.title)
    xml.atom(:updated, self.updated_at.iso8601)
    xml.atom(:subtitle, self.subtitle) if self.subtitle
    self.links.each do |link|
      xml.atom(:link, link)
    end
    xml.atom(:rights, self.rights) if self.rights
    xml.atom(:generator, self.generator) if self.generator
    self.entries.each do |entry|
      entry.to_xml(false, xml)
    end
  end
  xml.target!
end