class LinkHeaderParser::LinkHeader

Constants

FIELD_VALUE_REGEXP_PATTERN
PARAMETERS_REGEXP_PATTERN

Attributes

base[R]
field_value[R]

Public Class Methods

new(field_value, base:) click to toggle source

Create a new parsed Link header @see tools.ietf.org/html/rfc8288#appendix-B.2

@param field_value [String] @param base [String]

# File lib/link_header_parser/link_header.rb, line 13
def initialize(field_value, base:)
  @field_value = field_value.to_str
  @base = base.to_str
end

Public Instance Methods

context_string() click to toggle source

The context URL for this Link header extracted from field_value (or target URL if no context URL is present) @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.11)

@return [String]

# File lib/link_header_parser/link_header.rb, line 22
def context_string
  @context_string ||= grouped_link_parameters[:anchor]&.first || target_string
end
context_uri() click to toggle source

The resolved context URL for this Link header @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.12)

@return [String]

# File lib/link_header_parser/link_header.rb, line 30
def context_uri
  @context_uri ||= Addressable::URI.join(target_uri, context_string).normalize.to_s
end
inspect() click to toggle source
# File lib/link_header_parser/link_header.rb, line 34
def inspect
  format(%(#<#{self.class.name}:%#0x target_uri: #{target_uri.inspect}, relation_types: #{relation_types.inspect}>), object_id)
end
relation_types() click to toggle source

The relations_string value returned as an Array @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.10 and Appendix B.2.2.17.1)

@return [Array<String>]

# File lib/link_header_parser/link_header.rb, line 50
def relation_types
  @relation_types ||= relations_string.split.map(&:downcase)
end
relations_string() click to toggle source

The relation types for this Link header extracted from field_value @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.9)

@return [String]

# File lib/link_header_parser/link_header.rb, line 58
def relations_string
  @relations_string ||= grouped_link_parameters[:rel]&.first.to_s
end
target_string() click to toggle source

The target URL for this Link header extracted from field_value @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.4)

@return [String]

# File lib/link_header_parser/link_header.rb, line 66
def target_string
  @target_string ||= field_value_match_data[:target_string]
end
target_uri() click to toggle source

The resolved target URL for this Link header @see tools.ietf.org/html/rfc8288#appendix-B.2 (Appendix B.2.2.8)

@return [String]

# File lib/link_header_parser/link_header.rb, line 74
def target_uri
  @target_uri ||= Addressable::URI.join(base, target_string).normalize.to_s
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [Hash{Symbol => String, Array, Hash{Symbol => Array}}]

# File lib/link_header_parser/link_header.rb, line 79
def to_hash
  {
    target_string: target_string,
    target_uri: target_uri,
    context_string: context_string,
    context_uri: context_uri,
    relations_string: relations_string,
    relation_types: relation_types,
    link_parameters: grouped_link_parameters
  }
end
Also aliased as: to_h

Private Instance Methods

field_value_match_data() click to toggle source
# File lib/link_header_parser/link_header.rb, line 97
def field_value_match_data
  @field_value_match_data ||= field_value.match(FIELD_VALUE_REGEXP_PATTERN)
end