Class: Rbuzz::Feed

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed_url, options = {}) ⇒ Feed

Returns a new instance of Feed.



8
9
10
# File 'lib/rbuzz/feed.rb', line 8

def initialize(feed_url, options = {})
  @feed_url = feed_url
end

Instance Attribute Details

#atom_feedObject (readonly)

Returns the value of attribute atom_feed.



6
7
8
# File 'lib/rbuzz/feed.rb', line 6

def atom_feed
  @atom_feed
end

#feed_urlObject (readonly)

Returns the value of attribute feed_url.



5
6
7
# File 'lib/rbuzz/feed.rb', line 5

def feed_url
  @feed_url
end

Class Method Details

.discover(profile_name) ⇒ Object

Extract the feed url from the users’s google profile name



19
20
21
# File 'lib/rbuzz/feed.rb', line 19

def self.discover(profile_name)
  self.discover_by_url "http://www.google.com/profiles/#{profile_name}"
end

.discover_by_email(email) ⇒ Object

Extract the feed url from the users’s e-mail using the webfinger protocol



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rbuzz/feed.rb', line 24

def self.discover_by_email(email)
  resource = Net::HTTP.get(URI.parse("http://www.google.com/s2/webfinger/?q=acct:#{email}"))
  begin
    doc = XML::Document.string(resource)
  rescue XML::Error => e
    raise FeedError, "Could not find google profile for #{email}"
  end
  if profile_url = doc.find('//XRD:Alias', 'XRD:http://docs.oasis-open.org/ns/xri/xrd-1.0').first
    self.discover_by_url profile_url.content
  else
    raise FeedError, "Could not find google profile page for #{email}"
  end
end

.discover_by_url(profile_url) ⇒ Object

Extract the feed url from the users’s google profile page



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbuzz/feed.rb', line 39

def self.discover_by_url(profile_url)
  begin
    page = open(profile_url).read
  rescue OpenURI::HTTPError => e
    if e.io.status[0] == '404'
      raise FeedError, "Could not find profile at #{profile_url}"
    end
  end
  
  if match = page.match(%r{<link rel="alternate" type="application/atom\+xml" href="([^"]+)})
    match[1]
  else
    raise FeedError, "Could not find atom feed on profile page"
  end
end

.retrieve(feed_url) ⇒ Object



12
13
14
15
16
# File 'lib/rbuzz/feed.rb', line 12

def self.retrieve(feed_url)
  feed = Feed.new(feed_url)
  feed.fetch
  feed
end

Instance Method Details

#entriesObject

Retrieve the entries in the buzz atom feed as an array of FeedEntry objects This will fetch the feed if it has not already been fetched



63
64
65
66
67
# File 'lib/rbuzz/feed.rb', line 63

def entries
  return @feed_entries if @feed_entries
  fetch if @atom_feed.nil?
  @feed_entries = @atom_feed.entries.collect {|e| FeedEntry.new(e) }
end

#fetchObject

Retrieve and parse the buzz atom feed



56
57
58
59
# File 'lib/rbuzz/feed.rb', line 56

def fetch
  @feed_entries = nil
  @atom_feed = Atom::Feed.load_feed(URI.parse(@feed_url))
end