Class: Bridgetown::Webfinger::JRD

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/bridgetown/webfinger/jrd.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #warn

Constructor Details

#initialize(aliases: nil, links: nil, properties: nil, subject: nil) ⇒ JRD

Examples:

Creating a new Bridgetown::Webfinger::JRD without parsing a Hash


Bridgetown::Webfinger::JRD.new(
  subject: "acct:[email protected]",
  aliases: [Bridgetown::Webfinger::Alias.new("https://bilbobaggins.com/")]
)

Parameters:

  • aliases (Array<Alias>, nil) (defaults to: nil)

    the optional Aliases for the resource

  • links (Array<Link>, nil) (defaults to: nil)

    the optional Links related to the resource

  • properties (Properties) (defaults to: nil)

    the optional Properties describing the resource

  • subject (String, nil) (defaults to: nil)

    the (nominally) optional subject URI for the resource

Since:

  • 0.1.0



81
82
83
84
85
86
# File 'lib/bridgetown/webfinger/jrd.rb', line 81

def initialize(aliases: nil, links: nil, properties: nil, subject: nil)
  @aliases = aliases
  @links = links
  @properties = properties
  @subject = subject
end

Instance Attribute Details

#aliasesArray<Alias>? (readonly)

The list of Aliases for the resource

Examples:


jrd = Bridgetown::Webfinger::JRD.parse(
  "acct:[email protected]",
  {aliases: ["https://bilbobaggins.com"]}
)
jrd.aliases

Returns:

  • (Array<Alias>, nil)

    the optional Aliases for the resource

Since:

  • 0.1.0



102
103
104
# File 'lib/bridgetown/webfinger/jrd.rb', line 102

def aliases
  @aliases
end

The list of Links for the resource

Examples:


jrd = Bridgetown::Webfinger::JRD.parse(
  "acct:[email protected]",
  {links: [{href: "https://bilbobaggins.com", rel: "https://webfinger.net/rel/avatar"}]}
)
jrd.links

Returns:

  • (Array<Link>, nil)

    the optional Links related to the resource

Since:

  • 0.1.0



118
119
120
# File 'lib/bridgetown/webfinger/jrd.rb', line 118

def links
  @links
end

#propertiesProperties? (readonly)

The list of Properties for the resource

Examples:


jrd = Bridgetown::Webfinger::JRD.parse(
  "acct:[email protected]",
  {properties: {"http://packetizer.com/ns/name" => "Bilbo Baggins"}}
)
jrd.properties

Returns:

Since:

  • 0.1.0



134
135
136
# File 'lib/bridgetown/webfinger/jrd.rb', line 134

def properties
  @properties
end

#subjectString? (readonly)

The subject of the Bridgetown::Webfinger::JRD

Examples:

Read the subject for a Bridgetown::Webfinger::JRD


jrd = Bridgetown::Webfinger::JRD.parse("acct:[email protected]")
jrd.subject  #=> "acct:[email protected]"

Returns:

  • (String, nil)

    the (nominally) optional subject URI for the resource

Since:

  • 0.1.0



147
148
149
# File 'lib/bridgetown/webfinger/jrd.rb', line 147

def subject
  @subject
end

Class Method Details

.parse(subject, data) ⇒ JRD?

Parses and maybe-returns a Bridgetown::Webfinger::JRD when the subject and data form one

Bridgetown::Webfinger::JRDs can describe any resource identifiable via a URI, whether you want to give public directory information about people via ‘acct:` URIs, copyright information about an article, or perhaps license information about a library.

Examples:

Convert a Hash into a sanitized Bridgetown::Webfinger::JRD


Bridgetown::Webfinger::JRD.parse(
  "acct:[email protected]",
  {
    aliases: ["https://bilbobaggins.com/"],
    links: [
      {
        href: "https://bagend.com/bilbo",
        rel: "http://webfinger.net/rel/profile-page",
        type: "text/html",
        properties: {
          "http://packetizer.com/ns/name" => "Bilbo @ Bag End"
        },
        titles: {
          "en-us" => "Bilbo Baggins's blog"
        }
      }
    ],
    properties: {
      "http://packetizer.com/ns/name" => "Bilbo Baggins"
    }
  }
)

Parameters:

  • subject (String)

    the (nominally) optional subject identified by the JRD; should be present but is not required

  • data (Hash)

    the data hash containing information to add to the JRD

Returns:

Since:

  • 0.1.0



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bridgetown/webfinger/jrd.rb', line 51

def self.parse(subject, data)
  aliases = Array(data[:aliases]).filter_map { |moniker| Alias.parse(moniker) }
  links = Array(data[:links]).filter_map { |link| Link.parse(link) }
  properties = Properties.parse(data[:properties])
  subject = Webfinger.uri?(subject) ? subject : nil

  new(
    aliases: (!aliases.empty?) ? aliases : nil,
    links: (!links.empty?) ? links : nil,
    properties: properties,
    subject: subject
  )
end

Instance Method Details

#to_hHash

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.

Converts the Bridgetown::Webfinger::JRD to a JSON-serializable Hash

Returns:

  • (Hash)

    the JRD as a JSON-compatible Hash

Since:

  • 0.1.0



155
156
157
158
159
160
161
# File 'lib/bridgetown/webfinger/jrd.rb', line 155

def to_h
  result = {subject: subject}
  result[:aliases] = aliases if aliases
  result[:links] = links.map(&:to_h) if links
  result[:properties] = properties.to_h if properties
  result
end