Class: Bridgetown::Webfinger::Parameters Private
- Inherits:
-
Object
- Object
- Bridgetown::Webfinger::Parameters
- Defined in:
- lib/bridgetown/webfinger/parameters.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Parses URI parameters in the Webfinger style
This is necessary because Rack handles parameters differently than the Webfinger specification. In Rack, repeated parameters compete in a last-one-wins scheme. For example:
https://example.com?foo=1&foo=2&bar=3
parses into:
{"foo" => 2, "bar" => 3}
whereas Webfinger’s processing [wants this]:
{"foo" => [1, 2], "bar" => 3}
per the phrasing “the “rel” parameter MAY be included multiple times in order to request multiple link relation types.”
Constant Summary collapse
- SPLITTER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Splits query parameters at the ampersand and optional spaces
This was borrowed from [Rack::QueryParser] and simplified.
%r{& *}n
Instance Attribute Summary collapse
-
#rel ⇒ Array<String>?
readonly
The list of link relations requested within the Parameters.
-
#resource ⇒ String?
The decoded resource requested within the Parameters.
Class Method Summary collapse
-
.from_query_string(query_string) ⇒ Parameters
private
Parses a query string for a webfinger request into a hash.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
private
Checks for value object equality.
-
#add_rel(rel) ⇒ void
Adds a link relation, or “rel,” to the Parameters.
-
#hash ⇒ Integer
private
Allows the Parameters to be used in a Hash key; part of value object semantics.
-
#initialize(resource: nil, rel: nil) ⇒ void
constructor
Creates a new Parameters from an optional resource and rel.
Constructor Details
#initialize(resource: nil, rel: nil) ⇒ void
Creates a new Bridgetown::Webfinger::Parameters from an optional resource and rel
85 86 87 88 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 85 def initialize(resource: nil, rel: nil) @resource = resource @rel = rel end |
Instance Attribute Details
#rel ⇒ Array<String>? (readonly)
The list of link relations requested within the Bridgetown::Webfinger::Parameters
105 106 107 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 105 def rel @rel end |
#resource ⇒ String?
The decoded resource requested within the Bridgetown::Webfinger::Parameters
120 121 122 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 120 def resource @resource end |
Class Method Details
.from_query_string(query_string) ⇒ Parameters
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses a query string for a webfinger request into a hash
This method cleans any unrelated parameters from the result and combines multiple requests for ‘rel`s into an array. It also handles unescaping terms.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 49 def self.from_query_string(query_string) query_string .split(SPLITTER) .map { |pair| pair.split("=", 2).map! { |component| decode(component) } } .each_with_object(Parameters.new) do |(param, value), result| case param when "resource" then result.resource = value when "rel" then result.add_rel(value) end end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks for value object equality
129 130 131 132 133 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 129 def ==(other) other.instance_of?(Parameters) && resource == other.resource && rel == other.rel end |
#add_rel(rel) ⇒ void
This method returns an undefined value.
Adds a link relation, or “rel,” to the Bridgetown::Webfinger::Parameters
149 150 151 152 153 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 149 def add_rel(rel) return unless rel (@rel ||= []).push(rel) end |
#hash ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Allows the Bridgetown::Webfinger::Parameters to be used in a Hash key; part of value object semantics
161 162 163 |
# File 'lib/bridgetown/webfinger/parameters.rb', line 161 def hash [resource, *rel].hash end |