Class: LinkHeaderParser::LinkHeader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_value, base:) ⇒ LinkHeader

Create a new parsed Link header.

Parameters:

  • field_value (String, #to_str)
  • base (String, #to_str)

See Also:



23
24
25
26
# File 'lib/link_header_parser/link_header.rb', line 23

def initialize(field_value, base:)
  @field_value = field_value.to_str
  @base = base.to_str
end

Instance Attribute Details

#field_valueString (readonly)

The String value used to create this LinkHeaderParser::LinkHeader.

Returns:

  • (String)


14
15
16
# File 'lib/link_header_parser/link_header.rb', line 14

def field_value
  @field_value
end

Instance Method Details

#context_stringString

The context URL for this Link header extracted from field_value (or target URL if no context URL is present).



35
36
37
# File 'lib/link_header_parser/link_header.rb', line 35

def context_string
  @context_string ||= grouped_link_parameters[:anchor]&.first || target_string
end

#context_uriString

The resolved context URL for this Link header.



45
46
47
# File 'lib/link_header_parser/link_header.rb', line 45

def context_uri
  @context_uri ||= URI.join(target_uri, context_string).normalize.to_s
end

#inspectString

Returns:

  • (String)


50
51
52
53
54
# File 'lib/link_header_parser/link_header.rb', line 50

def inspect
  "#<#{self.class.name}:#{format("%#0x", object_id)} " \
    "target_uri: #{target_uri.inspect}, " \
    "relation_types: #{relation_types.inspect}>"
end

The parsed parameters for this Link header extracted from field_value.



62
63
64
65
66
# File 'lib/link_header_parser/link_header.rb', line 62

def link_parameters
  @link_parameters ||= field_value_match_data[:parameters]
                         .scan(PARAMETERS_REGEXP_PATTERN)
                         .map { |parameter| LinkHeaderParameter.new(parameter.strip) }
end

#relation_typesArray<String>

The relations_string value returned as an Array.



76
77
78
# File 'lib/link_header_parser/link_header.rb', line 76

def relation_types
  @relation_types ||= relations_string.split.map(&:downcase)
end

#relations_stringString

The relation types for this Link header extracted from field_value.



86
87
88
# File 'lib/link_header_parser/link_header.rb', line 86

def relations_string
  @relations_string ||= grouped_link_parameters[:rel]&.first.to_s
end

#target_stringString

The target URL for this Link header extracted from field_value



96
97
98
# File 'lib/link_header_parser/link_header.rb', line 96

def target_string
  @target_string ||= field_value_match_data[:target_string]
end

#target_uriString

The resolved target URL for this Link header.



106
107
108
# File 'lib/link_header_parser/link_header.rb', line 106

def target_uri
  @target_uri ||= URI.join(base, target_string).normalize.to_s
end

#to_hashHash{Symbol => String, Array, Hash{Symbol => Array}} Also known as: to_h

Return a Hash representation of this LinkHeaderParser::LinkHeader.

Returns:

  • (Hash{Symbol => String, Array, Hash{Symbol => Array}})


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/link_header_parser/link_header.rb', line 113

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