class Nitlink::Parser

Constants

RWS

Attributes

options[R]

Public Instance Methods

parse(response, http_method = 'GET') click to toggle source
# File lib/nitlink/parser.rb, line 16
def parse(response, http_method = 'GET')
  @http_method = http_method
  @request_uri, @status, link_header, @content_location_header = ResponseNormalizer.new.metadata(response)

  links = LinkCollection.new
  return links unless link_header

  unfolded_header = link_header.gsub(/\r?\n[\x20\x09]+/, '')
  link_strings = Splitter.new(unfolded_header).split_on_unquoted(',')

  parse_links(link_strings, links)
end

Private Instance Methods

decode(param_value) click to toggle source
# File lib/nitlink/parser.rb, line 89
def decode(param_value)
  ParamDecoder.new.decode(param_value)
end
extract_target_attributes(link_parameters) click to toggle source
# File lib/nitlink/parser.rb, line 67
def extract_target_attributes(link_parameters)
  target_attributes = []
  link_parameters.each do |param_name, param_value|
    next if %(rel anchor).include?(param_name)
    next if %(media title title* type).include?(param_name) && first_match(target_attributes, param_name)

    begin
      param_value = decode(param_value) if param_name.end_with?('*')
    rescue EncodedParamSyntaxError, UnsupportedCharsetError
      next
    end

    target_attributes.push [param_name, param_value]
  end

  Hash[target_attributes]
end
first_match(link_parameters, param_name) click to toggle source
# File lib/nitlink/parser.rb, line 63
def first_match(link_parameters, param_name)
  (link_parameters.find { |name, _value| name == param_name } || []).last
end
identity() click to toggle source
# File lib/nitlink/parser.rb, line 93
def identity
  if %w(GET HEAD).include?(@http_method.upcase) && [200, 203, 204, 206, 304].include?(@status)
    @request_uri
  else
    @content_location_header
  end
end
malformed(link_string) click to toggle source
# File lib/nitlink/parser.rb, line 85
def malformed(link_string)
  MalformedLinkHeaderError.new("Malformed link header (#{ link_string })")
end