Class: HttpHeaderLink::LinkHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/http_header_link/link_header.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*links, **options) ⇒ LinkHeader

Returns a new instance of LinkHeader.

Parameters:

Options Hash (**options):

  • :base_url (String)


33
34
35
36
37
# File 'lib/http_header_link/link_header.rb', line 33

def initialize(*links, **options)
  _links = links.flatten
  @links = _links.empty? ? [] : _links
  @base_url = options.delete(:base_url)
end

Instance Attribute Details

Returns:



28
29
30
# File 'lib/http_header_link/link_header.rb', line 28

def links
  @links
end

Class Method Details

.parse(target) ⇒ HttpHeaderLink::ParseResult

Parameters:

  • target (String)

Returns:

  • (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]
    options = {}
    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?('"')
      options[name.to_sym] = val
    end
    Link.new(url, **options)
  end

  new(links)
end

Instance Method Details

Parameters:

  • url (String)
  • options (Hash)

Options Hash (**options):

  • :rel (String)
  • :title (String)
  • :hreflang (String)
  • :media (String)
  • :type (String)


51
52
53
# File 'lib/http_header_link/link_header.rb', line 51

def add_link(url, **options)
  links << Link.new(url, **options)
end

#find_by(attribute, value) ⇒ Object

Parameters:

  • attribute (Symbol)
  • value (String)


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

#generateString

Returns:

  • (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

Returns:

  • (Boolean)


56
57
58
# File 'lib/http_header_link/link_header.rb', line 56

def present?
  !links.empty?
end