class LinkHeader::Link

Represents a link - an href and a list of attributes (key value pairs)

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"]]).to_s
=> '<http://example.com/foo>; rel="self"'

Attributes

attr_pairs[R]

The link's attributes, an array of key-value pairs

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"], ["rel", "canonical"]]).attr_pairs
=> [["rel", "self"], ["rel", "canonical"]]
href[R]

The link's URI string

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"]]).href
=> 'http://example.com/foo>'

Public Class Methods

new(href, attr_pairs) click to toggle source

Initialize a Link from an href and attribute list

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"]]).to_s
=> '<http://example.com/foo>; rel="self"'
# File lib/link_header.rb, line 152
def initialize(href, attr_pairs)
  @href, @attr_pairs = href, attr_pairs
end

Public Instance Methods

[](key) click to toggle source

Access attrs by key

# File lib/link_header.rb, line 171
def [](key)
  attrs[key]
end
attrs() click to toggle source

Lazily convert the attribute list to a Hash

Beware repeated attribute names (it's safer to use attr_pairs if this is risk):

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"], ["rel", "canonical"]]).attrs
=> {"rel" =>"canonical"}
# File lib/link_header.rb, line 164
def attrs
  @attrs ||= Hash[*attr_pairs.flatten]
end
to_a() click to toggle source

Convert to a JSON-friendly Array

LinkHeader::Link.new("http://example.com/foo", [["rel", "self"], ["rel", "canonical"]]).to_a
=> ["http://example.com/foo", [["rel", "self"], ["rel", "canonical"]]]
# File lib/link_header.rb, line 181
def to_a
  [href, attr_pairs]
end
to_html() click to toggle source

Bonus! Render as an HTML link element

LinkHeader::Link.new(["http://example.com/foo", [["rel", "self"]]]).to_html
#=> '<link href="http://example.com/foo" rel="self">'
# File lib/link_header.rb, line 203
def to_html
  ([%Q(<link href="#{href}")] + attr_pairs.map{|k, v| "#{k}=\"#{v.gsub(/"/, '\"')}\""}).join(' ') + '>'
end
to_s() click to toggle source

Convert to string representation as per the link header spec. This includes backspace-escaping doublequote characters in quoted attribute values.

Convert to string representation as per the link header spec

LinkHeader::Link.new(["http://example.com/foo", [["rel", "self"]]]).to_s
#=> '<http://example.com/foo>; rel="self"'
# File lib/link_header.rb, line 194
def to_s
  (["<#{href}>"] + attr_pairs.map{|k, v| "#{k}=\"#{v.gsub(/"/, '\"')}\""}).join('; ')
end