Class: LinkHeader::Link
- Inherits:
-
Object
- Object
- LinkHeader::Link
- Defined in:
- lib/tms_client/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"'
168 169 170 |
# File 'lib/tms_client/link_header.rb', line 168 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"]]
160 161 162 |
# File 'lib/tms_client/link_header.rb', line 160 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>'
152 153 154 |
# File 'lib/tms_client/link_header.rb', line 152 def href @href end |
Instance Method Details
#[](key) ⇒ Object
Access #attrs by key
187 188 189 |
# File 'lib/tms_client/link_header.rb', line 187 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"}
180 181 182 |
# File 'lib/tms_client/link_header.rb', line 180 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"]]]
197 198 199 |
# File 'lib/tms_client/link_header.rb', line 197 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">'
219 220 221 |
# File 'lib/tms_client/link_header.rb', line 219 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"'
210 211 212 |
# File 'lib/tms_client/link_header.rb', line 210 def to_s (["<#{href}>"] + attr_pairs.map{|k, v| "#{k}=\"#{v.gsub(/"/, '\"')}\""}).join('; ') end |