Class: Lotus::Atom::Link

Inherits:
Atom::Link
  • Object
show all
Includes:
Atom::Xml::Parseable
Defined in:
lib/lotus/atom/link.rb

Overview

This represents an Atom parser/generator for <link> tags.

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ Link

Create a link.

o

An XML::Reader containing a link element or a Hash of attributes.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lotus/atom/link.rb', line 18

def initialize(o)
  case o
  when XML::Reader
    if current_node_is?(o, 'link')
      self.text = o.read_string
      parse(o, :once => true)
    else
      raise ArgumentError, "Link created with node other than atom:link: #{o.name}"
    end
  when Hash
    [:href, :rel, :type, :length, :hreflang, :title].each do |attr|
      self.send("#{attr}=", o[attr])
    end
  else
    raise ArgumentError, "Don't know how to handle #{o}"
  end
end

Instance Method Details

#==(o) ⇒ Object

Two links are equal when their href is exactly the same regardless of case.



54
55
56
# File 'lib/lotus/atom/link.rb', line 54

def ==(o)
  o.respond_to?(:href) && o.href == self.href
end

#fetch(options = {}) ⇒ Object

This will fetch the URL referenced by the link.

If the URL contains a valid feed, a Feed will be returned, otherwise, the body of the response will be returned.

TODO: Handle redirects.



65
66
67
68
69
70
71
# File 'lib/lotus/atom/link.rb', line 65

def fetch(options = {})
  begin
    ::Atom::Feed.load_feed(URI.parse(self.href), options)
  rescue ArgumentError
    Net::HTTP.get_response(URI.parse(self.href)).body
  end
end

#hrefObject

Returns the href of the link, or the content of the link if no href is provided.



43
44
45
# File 'lib/lotus/atom/link.rb', line 43

def href
  @href || self.text
end

#inspectObject

:nodoc:



74
75
76
# File 'lib/lotus/atom/link.rb', line 74

def inspect
  "<Lotus::Link href:'#{href}' type:'#{type}'>"
end

#length=(v) ⇒ Object



37
38
39
# File 'lib/lotus/atom/link.rb', line 37

def length=(v)
  @length = v.to_i
end

#to_sObject

Reports the href of the link.



48
49
50
# File 'lib/lotus/atom/link.rb', line 48

def to_s
  self.href
end