Class: LinkHeader::Link
- Inherits:
-
Object
- Object
- LinkHeader::Link
- Defined in:
- lib/link_header.rb
Overview
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"'
Instance Attribute Summary collapse
-
#attr_pairs ⇒ Object
readonly
The link’s attributes, an array of key-value pairs.
-
#href ⇒ Object
readonly
The link’s URI string.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access #attrs by key.
-
#attrs ⇒ Object
Lazily convert the attribute list to a Hash.
-
#initialize(href, attr_pairs) ⇒ Link
constructor
Initialize a Link from an href and attribute list.
-
#to_a ⇒ Object
Convert to a JSON-friendly Array.
-
#to_html ⇒ Object
Bonus! Render as an HTML link element.
-
#to_s ⇒ Object
Convert to string representation as per the link header spec.
Constructor Details
#initialize(href, attr_pairs) ⇒ Link
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"'
152 153 154 |
# File 'lib/link_header.rb', line 152 def initialize(href, attr_pairs) @href, @attr_pairs = href, attr_pairs end |
Instance Attribute Details
#attr_pairs ⇒ Object (readonly)
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"]]
144 145 146 |
# File 'lib/link_header.rb', line 144 def attr_pairs @attr_pairs end |
#href ⇒ Object (readonly)
The link’s URI string
LinkHeader::Link.new("http://example.com/foo", [["rel", "self"]]).href
=> 'http://example.com/foo>'
136 137 138 |
# File 'lib/link_header.rb', line 136 def href @href end |
Instance Method Details
#[](key) ⇒ Object
Access #attrs by key
171 172 173 |
# File 'lib/link_header.rb', line 171 def [](key) attrs[key] end |
#attrs ⇒ Object
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"}
164 165 166 |
# File 'lib/link_header.rb', line 164 def attrs @attrs ||= Hash[*attr_pairs.flatten] end |
#to_a ⇒ Object
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"]]]
181 182 183 |
# File 'lib/link_header.rb', line 181 def to_a [href, attr_pairs] end |
#to_html ⇒ Object
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">'
203 204 205 |
# 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 ⇒ Object
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"'
194 195 196 |
# File 'lib/link_header.rb', line 194 def to_s (["<#{href}>"] + attr_pairs.map{|k, v| "#{k}=\"#{v.gsub(/"/, '\"')}\""}).join('; ') end |