Class: Bridgetown::Webfinger::Parameters Private

Inherits:
Object
  • Object
show all
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.”

[1]: datatracker.ietf.org/doc/html/rfc7033#section-4.3

Since:

  • 0.1.0

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.

[2]: rubydoc.info/gems/rack/Rack/QueryParser

Since:

  • 0.1.0

%r{& *}n

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource: nil, rel: nil) ⇒ void

Creates a new Bridgetown::Webfinger::Parameters from an optional resource and rel

Examples:

Create an empty


Since:

  • 0.1.0



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

#relArray<String>? (readonly)

The list of link relations requested within the Bridgetown::Webfinger::Parameters

Examples:

Reading the link relations requested as a URL parameter


params = Bridgetown::Webfinger::Parameters.from_query_string(
  "resource=acct%3Abilbo%40bagend.com&" \
  "rel=http%3A%2F%2Fwebfinger.net%2Frel%2Favatar&" \
  "rel=http%3A%2F%2Fwebfinger.net%2Frel%2Fprofile-page"
)
params.rel
#=> ["https://webfinger.net/rel/avatar", "https://webfinger.net/rel/profile-page"]

Since:

  • 0.1.0



105
106
107
# File 'lib/bridgetown/webfinger/parameters.rb', line 105

def rel
  @rel
end

#resourceString?

The decoded resource requested within the Bridgetown::Webfinger::Parameters

Examples:

Reading the resource requested as a URL parameter


params = Bridgetown::Webfinger::Parameters.from_query_string(
  "resource=acct%3Abilbo%40bagend.com"
)
params.resource #=> "acct:[email protected]"

Since:

  • 0.1.0



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.

Since:

  • 0.1.0



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

Since:

  • 0.1.0



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

Examples:

Adding a link relation to a new Bridgetown::Webfinger::Parameters instance


params = Bridgetown::Webfinger::Parameters.new
params.add_rel("http://webfinger.net/rel/avatar")
params.rel  #=> ["http://webfinger.net/rel/avatar"]

Since:

  • 0.1.0



149
150
151
152
153
# File 'lib/bridgetown/webfinger/parameters.rb', line 149

def add_rel(rel)
  return unless rel

  (@rel ||= []).push(rel)
end

#hashInteger

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

Since:

  • 0.1.0



161
162
163
# File 'lib/bridgetown/webfinger/parameters.rb', line 161

def hash
  [resource, *rel].hash
end