Class: OStatus::Feed

Inherits:
Atom::Feed
  • Object
show all
Includes:
Atom::SimpleExtensions
Defined in:
lib/ostatus/feed.rb

Overview

This class represents an OStatus Feed object.

Constant Summary collapse

MIME_ORDER =

Store in reverse order so that the -1 from .index “not found” will sort properly

['application/atom+xml', 'application/rss+xml',
'application/xml'].reverse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Feed.



33
34
35
36
37
38
39
40
41
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
# File 'lib/ostatus/feed.rb', line 33

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

  if str

    if str =~ /<html/
      doc = LibXML::XML::HTMLParser.string(str).parse
      links = doc.find(
        "//*[contains(concat(' ',normalize-space(@rel),' '), 'alternate')]"
      ).map {|el|
        {:type => el.attributes['type'].to_s,
         :href => el.attributes['href'].to_s}
      }.sort {|a, b|
        MIME_ORDER.index(b[:type]) || -1 <=>
        MIME_ORDER.index(a[:type]) || -1
      }

      # Resolve relative links
      link = URI::parse(links.first[:href]) rescue URI.new

      unless link.host
        link.host = URI::parse(@url).host rescue nil
      end

      unless link.absolute?
        link.path = File::dirname(URI::parse(@url).path) \
                    + '/' + link.path rescue nil
      end

      @url = link.to_s
      @str = str = open(@url).read
    end

    super(XML::Reader.string(str))
  else
    super(options)
  end
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
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.



91
92
93
# File 'lib/ostatus/feed.rb', line 91

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

.from_string(str) ⇒ Object



95
96
97
# File 'lib/ostatus/feed.rb', line 95

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.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ostatus/feed.rb', line 77

def Feed.from_url(url, access_token = nil)
  if access_token.nil?
    # simply open the url
    str = open(url).read
  else
    # open the url through OAuth
    str = access_token.get(url).body
  end

  Feed.new(str, 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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ostatus/feed.rb', line 132

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
    self.links << Atom::Link.new(:rel => 'self', :href => @url) if @url
    self.links << Atom::Link.new(:rel => 'edit', :href => @url) if @url
    self.to_xml
  end
end

#authorObject

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



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

def author
  @options ? @options[:author] : self.authors.first
end

#author=(author) ⇒ Object



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

def author= author
  self.authors.clear << author
end

#hubsObject

Returns an array of URLs for each hub link tag.



120
121
122
# File 'lib/ostatus/feed.rb', line 120

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

#hubs=(hubs) ⇒ Object



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

def hubs= hubs
  hubs.each do |hub|
    links << Atom::Link.new(:rel => 'hub', :href => hub)
  end
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.


106
107
108
# File 'lib/ostatus/feed.rb', line 106

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

#links=(given) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/ostatus/feed.rb', line 110

def links=(given)
  self.links.clear
  given.each do |rel,links|
    links.each do |l|
      self.links << Atom::Link.new(l.merge({:rel => rel}))
    end
  end
end

#salmonObject

Returns the salmon URL from the link tag.



125
126
127
# File 'lib/ostatus/feed.rb', line 125

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