Class: OStatus::Feed
- Inherits:
-
Atom::Feed
- Object
- Atom::Feed
- OStatus::Feed
- 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
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.from_data(url, options) ⇒ Object
Creates a new Feed instance that contains the information given by the various instances of author and entries.
- .from_string(str) ⇒ Object
-
.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.
Instance Method Summary collapse
-
#atom ⇒ Object
This method will return a String containing the actual content of the atom feed.
-
#author ⇒ Object
Returns an OStatus::Author that will parse the author information within the Feed.
- #author=(author) ⇒ Object
-
#hubs ⇒ Object
Returns an array of URLs for each hub link tag.
- #hubs=(hubs) ⇒ Object
-
#initialize(str, url, access_token, options) ⇒ Feed
constructor
A new instance of Feed.
-
#link(attribute) ⇒ Object
Returns an array of Atom::Link instances for all link tags that have a rel equal to that given by attribute.
- #links=(given) ⇒ Object
-
#salmon ⇒ Object
Returns the salmon URL from the link tag.
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, ) @str = str @url = url @access_token = access_token @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() end end |
Instance Attribute Details
#url ⇒ Object (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, ) Feed.new(nil, url, nil, ) 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
#atom ⇒ Object
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 |
#author ⇒ Object
Returns an OStatus::Author that will parse the author information within the Feed.
150 151 152 |
# File 'lib/ostatus/feed.rb', line 150 def @options ? @options[:author] : self..first end |
#author=(author) ⇒ Object
154 155 156 |
# File 'lib/ostatus/feed.rb', line 154 def self..clear << end |
#hubs ⇒ Object
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 |
#link(attribute) ⇒ Object
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 |
#salmon ⇒ Object
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 |