Class: Nelumba::Atom::Source

Inherits:
Atom::Source
  • Object
show all
Includes:
Atom::SimpleExtensions
Defined in:
lib/nelumba/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 Nelumba::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
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nelumba/atom/source.rb', line 42

def self.from_canonical(obj)
  hash = obj.to_hash
  hash.delete :items
  hash.delete :author
  hash.delete :total_items
  hash.delete :display_name
  hash.delete :content
  hash.delete :summary

  hash.delete :text
  hash.delete :html

  # Ensure that the generator is encoded.
  if hash[:generator]
    hash[:generator] = Nelumba::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|
    Nelumba::Atom::Author.from_canonical(a)
  }

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

  hash[:categories].map! {|c|
    Nelumba::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

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

  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.



161
162
163
# File 'lib/nelumba/atom/source.rb', line 161

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.


156
157
158
# File 'lib/nelumba/atom/source.rb', line 156

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

#to_canonicalObject



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

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)

  Nelumba::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.



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/nelumba/atom/source.rb', line 166

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