Class: Lucid::Shopify::ParseLinkHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid/shopify/parse_link_header.rb

Instance Method Summary collapse

Instance Method Details

#call(link_header) ⇒ Hash

Parse a Link header into query params for each pagination link (e.g. :next, :previous).

Parameters:

  • link_header (String, nil)

Returns:

  • (Hash)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lucid/shopify/parse_link_header.rb', line 14

def call(link_header)
  case link_header
  # NOTE: There is a strange behaviour where it seems that if the header
  # value exceeds a certain length, it is split into chunks. It seems that
  # if you use {HTTP::Headers#get}, you will always get {Array<String>},
  # and it is the special behaviour of {HTTP::Headers#[]} causing this.
  #
  # However, why is it split in the first place? Does Shopify send
  # multiple Link headers (as chunks) for longer values? There does not
  # seem to be any limit on the length of value in the HTTP specification.
  #
  # https://www.rubydoc.info/gems/http/HTTP/Headers#[]-instance_method
  # https://www.rubydoc.info/gems/http/HTTP/Headers#get-instance_method
  when Array
    link_header = link_header.join
  when String
  else
    return {}
  end

  link_header.split(',').map do |link|
    url, rel = link.split(';') # rel should be the first param
    url = url[/<(.*)>/, 1]
    rel = rel[/rel="?(\w+)"?/, 1]

    [rel.to_sym, query_params(url)]
  end.to_h
end