class LinkHeaderParser::LinkHeader
Constants
- FIELD_VALUE_REGEXP_PATTERN
- PARAMETERS_REGEXP_PATTERN
Attributes
Public Class Methods
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
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
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
# 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
The parsed parameters for this Link header extracted from field_value
@see tools.ietf.org/html/rfc8288#appendix-B.3
@return [Array<LinkHeaderParser::LinkHeaderParameter>]
# File lib/link_header_parser/link_header.rb, line 42 def link_parameters @link_parameters ||= field_value_match_data[:parameters].scan(PARAMETERS_REGEXP_PATTERN).flatten.map { |parameter| LinkHeaderParameter.new(parameter) } end
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
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
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
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
@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
Private Instance Methods
# 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
# File lib/link_header_parser/link_header.rb, line 101 def grouped_link_parameters @grouped_link_parameters ||= link_parameters.map(&:to_a).group_by(&:shift).transform_keys(&:to_sym).transform_values(&:flatten) end