Class: Lotus::Atom::Source

Inherits:
Atom::Source
  • Object
show all
Includes:
Atom::SimpleExtensions
Defined in:
lib/lotus/atom/source.rb

Overview

This class represents an OStatus Feed object.

Constant Summary collapse

ACTIVITY_NAMESPACE =

The XML namespace that identifies the conforming specification.

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

The XML namespace the specifies this content.

'http://portablecontacts.net/spec/1.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_canonical(obj) ⇒ Object

Creates an Atom generator for the given Lotus::Feed.



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
# File 'lib/lotus/atom/source.rb', line 42

def self.from_canonical(obj)
  hash = obj.to_hash
  hash.delete :entries

  # Ensure that the generator is encoded.
  if hash[:generator]
    hash[:generator] = Lotus::Atom::Generator.from_canonical(hash[:generator])
  end

  hash[:links] ||= []

  if hash[:salmon_url]
    hash[:links] << ::Atom::Link.new(:rel => "salmon", :href => hash[:salmon_url])
  end
  hash.delete :salmon_url

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

  hash[:hubs].each {|h|
    hash[:links] << ::Atom::Link.new(:rel => "hub", :href => h)
  }
  hash.delete :hubs

  hash[:authors].map! {|a|
    Lotus::Atom::Author.from_canonical(a)
  }

  hash[:contributors].map! {|a|
    Lotus::Atom::Author.from_canonical(a)
  }

  hash[:categories].map! {|c|
    Lotus::Atom::Category.from_canonical(c)
  }

  # title/subtitle content type
  node = XML::Node.new("title")
  node['type'] = hash[:title_type] if hash[:title_type]
  node << hash[:title]

  xml = XML::Reader.string(node.to_s)
  xml.read
  hash[:title] = ::Atom::Content.parse(xml)
  hash.delete :title_type

  if hash[:subtitle]
    node = XML::Node.new("subtitle")
    node['type'] = hash[:subtitle_type] if hash[:subtitle_type]
    node << hash[:subtitle]

    xml = XML::Reader.string(node.to_s)
    xml.read
    hash[:subtitle] = ::Atom::Content.parse(xml)
  else
    hash.delete :subtitle
  end
  hash.delete :subtitle_type

  self.new(hash)
end

Instance Method Details

#hubsObject

Returns an array of URLs for each hub link tag.



150
151
152
# File 'lib/lotus/atom/source.rb', line 150

def hubs
  link('hub').map { |link| link.href }
end

Returns an array of ::Atom::Link instances for all link tags that have a rel equal to that given by attribute.

For example:

link(:hub).first.href -- Gets the first link tag with rel="hub" and
                         returns the contents of the href attribute.


145
146
147
# File 'lib/lotus/atom/source.rb', line 145

def link(attribute)
  links.find_all { |l| l.rel == attribute.to_s }
end

#to_canonicalObject



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
# File 'lib/lotus/atom/source.rb', line 106

def to_canonical
  generator = nil
  generator = self.generator.to_canonical if self.generator

  salmon_url = nil
  if self.link('salmon').any?
    salmon_url = self.link('salmon').first.href
  end

  url = self.url

  categories = self.categories.map(&:to_canonical)

  Lotus::Feed.new(:title         => self.title,
                  :title_type    => self.title ? self.title.type : nil,
                  :subtitle      => self.subtitle,
                  :subtitle_type => self.subtitle ? self.subtitle.type : nil,
                  :id            => self.id,
                  :url           => url,
                  :categories    => categories,
                  :icon          => self.icon,
                  :logo          => self.,
                  :rights        => self.rights,
                  :published     => self.published,
                  :updated       => self.updated,
                  :authors       => self.authors.map(&:to_canonical),
                  :contributors  => self.contributors.map(&:to_canonical),
                  :hubs          => self.hubs,
                  :salmon_url    => salmon_url,
                  :generator     => generator)
end

#urlObject

Returns a string of the url for this feed.



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/lotus/atom/source.rb', line 155

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