Class: LinkHeader
- Inherits:
-
Object
- Object
- LinkHeader
- Defined in:
- lib/link_header.rb,
lib/link_header/version.rb
Overview
Represents an HTTP link header of the form described in the draft spec tools.ietf.org/id/draft-nottingham-http-link-header-06.txt. It is simply a list of LinkHeader::Link objects and some conversion functions.
Defined Under Namespace
Classes: Link
Constant Summary collapse
- HREF =
Regexes for link header parsing. TOKEN and QUOTED in particular should conform to RFC2616.
Acknowledgement: The QUOTED regexp is based on stackoverflow.com/questions/249791/regexp-for-quoted-string-with-escaping-quotes/249937#249937
/ *< *([^>]*) *> *;? */- TOKEN =
:nodoc: non-empty sequence of non-separator characters
/([^()<>@,;:\"\[\]?={}\s]+)/- QUOTED =
:nodoc: double-quoted strings with backslash-escaped double quotes
/"((?:[^"\\]|\\.)*)"/- ATTR =
:nodoc:
/#{TOKEN} *= *(#{TOKEN}|#{QUOTED}) */- SEMI =
:nodoc:
/; */- COMMA =
:nodoc:
/, */- VERSION =
"0.0.8"
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
An array of Link objects.
Class Method Summary collapse
-
.parse(link_header) ⇒ Object
Parse a link header, returning a new LinkHeader object.
Instance Method Summary collapse
- #<<(link) ⇒ Object
-
#find_link(*attr_pairs) ⇒ Object
Find a member link that has the given attributes.
-
#initialize(links = []) ⇒ LinkHeader
constructor
Initialize from a collection of either LinkHeader::Link objects or data from which Link objects can be created.
-
#to_a ⇒ Object
Convert to a JSON-friendly array.
-
#to_html(separator = "\n") ⇒ Object
Render as a list of HTML link elements.
-
#to_s ⇒ Object
Convert to string representation as per the link header spec.
Constructor Details
#initialize(links = []) ⇒ LinkHeader
Initialize from a collection of either LinkHeader::Link objects or data from which Link objects can be created.
From a list of LinkHeader::Link objects:
LinkHeader.new([
LinkHeader::Link.new("http://example.com/foo", [["rel", "self"]]),
LinkHeader::Link.new("http://example.com/", [["rel", "up"]])])
From the equivalent JSON-friendly raw data:
LinkHeader.new([
["http://example.com/foo", [["rel", "self"]]],
["http://example.com/", [["rel", "up"]]]]).to_s
See also LinkHeader.parse
29 30 31 32 33 34 35 |
# File 'lib/link_header.rb', line 29 def initialize(links=[]) if links @links = links.map{|l| l.kind_of?(Link) ? l : Link.new(*l)} else @links = [] end end |
Instance Attribute Details
#links ⇒ Object (readonly)
An array of Link objects
10 11 12 |
# File 'lib/link_header.rb', line 10 def links @links end |
Class Method Details
.parse(link_header) ⇒ Object
Parse a link header, returning a new LinkHeader object
LinkHeader.parse('<http://example.com/foo>; rel="self", <http://example.com/>; rel = "up"').to_a
#=> [["http://example.com/foo", [["rel", "self"]]],
["http://example.com/", [["rel", "up"]]]]
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/link_header.rb', line 85 def self.parse(link_header) return new unless link_header scanner = StringScanner.new(link_header) links = [] while scanner.scan(HREF) href = scanner[1] attrs = [] while scanner.scan(ATTR) attr_name, token, quoted = scanner[1], scanner[3], scanner[4] attrs.push([attr_name, token || quoted.gsub(/\\"/, '"')]) break unless scanner.scan(SEMI) end links.push(Link.new(href, attrs)) break unless scanner.scan(COMMA) end new(links) end |
Instance Method Details
#<<(link) ⇒ Object
37 38 39 40 |
# File 'lib/link_header.rb', line 37 def <<(link) link = link.kind_of?(Link) ? link : Link.new(*link) @links << link end |
#find_link(*attr_pairs) ⇒ Object
Find a member link that has the given attributes
108 109 110 111 112 113 114 |
# File 'lib/link_header.rb', line 108 def find_link(*attr_pairs) links.detect do |link| !attr_pairs.detect do |pair| !link.attr_pairs.include?(pair) end end end |
#to_a ⇒ Object
Convert to a JSON-friendly array
LinkHeader.parse('<http://example.com/foo>; rel="self", <http://example.com/>; rel = "up"').to_a
#=> [["http://example.com/foo", [["rel", "self"]]],
["http://example.com/", [["rel", "up"]]]]
49 50 51 |
# File 'lib/link_header.rb', line 49 def to_a links.map{|l| l.to_a} end |
#to_html(separator = "\n") ⇒ Object
Render as a list of HTML link elements
119 120 121 |
# File 'lib/link_header.rb', line 119 def to_html(separator="\n") links.map{|link| link.to_html}.join(separator) end |
#to_s ⇒ Object
Convert to string representation as per the link header spec
LinkHeader.new([
["http://example.com/foo", [["rel", "self"]]],
["http://example.com/", [["rel", "up"]]]]).to_s
#=> '<http://example.com/foo>; rel="self", <http://example.com/>; rel = "up"'
61 62 63 |
# File 'lib/link_header.rb', line 61 def to_s links.join(', ') end |