Class: OStatus::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/ostatus/feed.rb

Overview

This class represents an OStatus Feed object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, url, access_token, options) ⇒ Feed

Returns a new instance of Feed.



12
13
14
15
16
17
18
19
20
21
# File 'lib/ostatus/feed.rb', line 12

def initialize(str, url, access_token, options)
  @str = str
  @url = url
  @access_token = access_token
  @options = options

  if options == nil
    @xml = Nokogiri::XML::Document.parse(self.atom)
  end
end

Class Method Details

.from_data(url, options) ⇒ Object

Creates a new Feed instance that contains the information given by the various instances of author and entries.



31
32
33
# File 'lib/ostatus/feed.rb', line 31

def Feed.from_data(url, options)
  Feed.new(nil, url, nil, options)
end

.from_string(str) ⇒ Object



35
36
37
# File 'lib/ostatus/feed.rb', line 35

def Feed.from_string(str)
  Feed.new(str, nil, nil, nil)
end

.from_url(url, access_token = nil) ⇒ Object

Creates a new Feed instance given by the atom feed located at ‘url’ and optionally using the OAuth::AccessToken given.



25
26
27
# File 'lib/ostatus/feed.rb', line 25

def Feed.from_url(url, access_token = nil)
  Feed.new(nil, url, access_token, nil)
end

Instance Method Details

#atomObject

This method will return a String containing the actual content of the atom feed. It will make a network request (through OAuth if an access token was given) to retrieve the document if necessary.



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
# File 'lib/ostatus/feed.rb', line 90

def atom
  if @str != nil
    @str
  elsif @options == nil and @access_token == nil
    # simply open the url
    open(@url).read
  elsif @options == nil and @url != nil
    # open the url through OAuth
    @access_token.get(@url).body
  else
    # build the atom file from internal information
    feed = TinyAtom::Feed.new(
      self.id,
      self.title,

      @url,

      :author_name => self.author.name,
      :author_email => self.author.email,
      :author_uri => self.author.uri,

      :hubs => self.hubs
    )

    @options[:entries].each do |entry|
      entry_url = entry.url
      entry_url = @url if entry_url == nil

      feed.add_entry(
        entry.id,
        entry.title,
        entry.updated,

        entry_url,

        :published => entry.published,

        :content => entry.content,

        :author_name => self.author.name,
        :author_email => self.author.email,
        :author_uri => self.author.uri
      )
    end

    feed.make(:indent => 2)
  end
end

#authorObject

Returns an OStatus::Author that will parse the author information within the Feed.



166
167
168
169
170
171
# File 'lib/ostatus/feed.rb', line 166

def author
  return @options[:author] unless @options == nil

  author_xml = @xml.at_css('author')
  OStatus::Author.new(author_xml)
end

#entriesObject

This method gives you an array of OStatus::Entry instances for each entry listed in the feed.



175
176
177
178
179
180
181
182
183
# File 'lib/ostatus/feed.rb', line 175

def entries
  return @options[:entries] unless @options == nil

  entries_xml = @xml.css('entry')

  entries_xml.map do |entry|
    OStatus::Entry.new(entry)
  end
end

#hubsObject

Returns an array of URLs for each hub link tag.



58
59
60
61
62
63
64
65
66
# File 'lib/ostatus/feed.rb', line 58

def hubs
  if link(:hub)
    link(:hub).map do |link|
      link[:href]
    end
  else
    []
  end
end

#iconObject

Returns the icon



81
82
83
84
85
# File 'lib/ostatus/feed.rb', line 81

def icon
  return @options[:icon] unless @options == nil

  pick_first_node(@xml.xpath('/xmlns:feed/xmlns:icon'))
end

#idObject



148
149
150
151
152
# File 'lib/ostatus/feed.rb', line 148

def id
  return @options[:id] unless @options == nil

  pick_first_node(@xml.xpath('/xmlns:feed/xmlns:id'))
end

Returns an array of Nokogiri::XML::Element instances for all link tags that have a rel equal to that given by attribute. This can be used generally as a Hash where the keys are intern strings that give an attribute.

For example:

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


47
48
49
50
51
52
53
54
55
# File 'lib/ostatus/feed.rb', line 47

def link(attribute)
  return @options[:links][attribute] unless @options == nil or @options[:links] == nil
  return nil if @options != nil

  # get all links with rel attribute being equal to attribute
  @xml.xpath('/xmlns:feed/xmlns:link').select do |link|
    link[:rel] == attribute.to_s
  end
end

#logoObject

Returns the logo



74
75
76
77
78
# File 'lib/ostatus/feed.rb', line 74

def 
  return @options[:logo] unless @options == nil

  pick_first_node(@xml.xpath('/xmlns:feed/xmlns:logo'))
end

#salmonObject

Returns the salmon URL from the link tag.



69
70
71
# File 'lib/ostatus/feed.rb', line 69

def salmon
  link(:salmon).first[:href]
end

#titleObject



154
155
156
157
158
# File 'lib/ostatus/feed.rb', line 154

def title
  return @options[:title] unless @options == nil

  pick_first_node(@xml.xpath('/xmlns:feed/xmlns:title'))
end

#urlObject



160
161
162
# File 'lib/ostatus/feed.rb', line 160

def url
  return @url
end