Class: LinkHeaderParser::LinkHeader
- Inherits:
-
Object
- Object
- LinkHeaderParser::LinkHeader
- Defined in:
- lib/link_header_parser/link_header.rb
Instance Attribute Summary collapse
-
#field_value ⇒ String
readonly
The
String
value used to create this LinkHeader.
Instance Method Summary collapse
-
#context_string ⇒ String
The context URL for this Link header extracted from
field_value
(or target URL if no context URL is present). -
#context_uri ⇒ String
The resolved context URL for this Link header.
-
#initialize(field_value, base:) ⇒ LinkHeader
constructor
Create a new parsed Link header.
- #inspect ⇒ String
-
#link_parameters ⇒ Array<LinkHeaderParser::LinkHeaderParameter>
The parsed parameters for this Link header extracted from
field_value
. -
#relation_types ⇒ Array<String>
The
relations_string
value returned as anArray
. -
#relations_string ⇒ String
The relation types for this Link header extracted from
field_value
. -
#target_string ⇒ String
The target URL for this Link header extracted from
field_value
. -
#target_uri ⇒ String
The resolved target URL for this Link header.
-
#to_hash ⇒ Hash{Symbol => String, Array, Hash{Symbol => Array}}
(also: #to_h)
Return a
Hash
representation of this LinkHeader.
Constructor Details
#initialize(field_value, base:) ⇒ LinkHeader
Create a new parsed Link header.
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_value ⇒ String (readonly)
The String
value used to create this LinkHeaderParser::LinkHeader.
14 15 16 |
# File 'lib/link_header_parser/link_header.rb', line 14 def field_value @field_value end |
Instance Method Details
#context_string ⇒ String
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_uri ⇒ String
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 |
#inspect ⇒ 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 |
#link_parameters ⇒ Array<LinkHeaderParser::LinkHeaderParameter>
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_types ⇒ Array<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_string ⇒ String
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_string ⇒ String
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_uri ⇒ String
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_hash ⇒ Hash{Symbol => String, Array, Hash{Symbol => Array}} Also known as: to_h
Return a Hash
representation of this LinkHeaderParser::LinkHeader.
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 |