Class: HttpHeaderLink::LinkHeader
- Inherits:
-
Object
- Object
- HttpHeaderLink::LinkHeader
- Defined in:
- lib/http_header_link/link_header.rb
Instance Attribute Summary collapse
- #links ⇒ Array<HttpHeaderLink::Link> readonly
Class Method Summary collapse
Instance Method Summary collapse
- #add_link(url, **options) ⇒ Object
- #find_by(attribute, value) ⇒ Object
- #generate ⇒ String
-
#initialize(*links, **options) ⇒ LinkHeader
constructor
A new instance of LinkHeader.
- #present? ⇒ Boolean
Constructor Details
#initialize(*links, **options) ⇒ LinkHeader
Returns a new instance of LinkHeader.
33 34 35 36 37 |
# File 'lib/http_header_link/link_header.rb', line 33 def initialize(*links, **) _links = links.flatten @links = _links.empty? ? [] : _links @base_url = .delete(:base_url) end |
Instance Attribute Details
#links ⇒ Array<HttpHeaderLink::Link> (readonly)
28 29 30 |
# File 'lib/http_header_link/link_header.rb', line 28 def links @links end |
Class Method Details
.parse(target) ⇒ HttpHeaderLink::ParseResult
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/http_header_link/link_header.rb', line 8 def parse(target) parts = target.split(',') links = parts.map do |part| sections = part.split(';') url = sections.shift[/<(.*)>/, 1] = {} sections.each do |section| name, val = section.split('=').map(&:strip) val.slice!(0, 1) if val.start_with?('"') val.slice!(-1, 1) if val.end_with?('"') [name.to_sym] = val end Link.new(url, **) end new(links) end |
Instance Method Details
#add_link(url, **options) ⇒ Object
51 52 53 |
# File 'lib/http_header_link/link_header.rb', line 51 def add_link(url, **) links << Link.new(url, **) end |
#find_by(attribute, value) ⇒ Object
62 63 64 |
# File 'lib/http_header_link/link_header.rb', line 62 def find_by(attribute, value) links.find { |link| link.public_send(attribute) == value } end |
#generate ⇒ String
40 41 42 |
# File 'lib/http_header_link/link_header.rb', line 40 def generate links.flatten.compact.map { |l| l.generate(base_url: base_url) }.join(', ') end |
#present? ⇒ Boolean
56 57 58 |
# File 'lib/http_header_link/link_header.rb', line 56 def present? !links.empty? end |