Class: DiasporaFederation::Discovery::HostMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/discovery/host_meta.rb

Overview

Generates and parses Host Meta documents.

This is a minimal implementation of the standard, only to the degree of what is used for the purposes of the diaspora* protocol. (e.g. WebFinger)

Examples:

Creating a Host Meta document

doc = HostMeta.from_base_url("https://pod.example.tld/")
doc.to_xml

Parsing a Host Meta document

doc = HostMeta.from_xml(xml_string)
webfinger_tpl = doc.webfinger_template_url

See Also:

Constant Summary collapse

WEBFINGER_SUFFIX =

URL fragment to append to the base URL

"/.well-known/webfinger.xml?resource={uri}".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webfinger_url) ⇒ HostMeta

Creates a new host-meta instance

Parameters:

  • webfinger_url (String)

    the webfinger-url



23
24
25
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 23

def initialize(webfinger_url)
  @webfinger_url = webfinger_url
end

Class Method Details

.from_base_url(base_url) ⇒ HostMeta

Builds a new HostMeta instance and constructs the WebFinger URL from the given base URL by appending HostMeta::WEBFINGER_SUFFIX.

Parameters:

  • base_url (String, URL)

    the base-url for the webfinger-url

Returns:

Raises:



53
54
55
56
57
58
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 53

def self.from_base_url(base_url)
  webfinger_url = "#{base_url.to_s.chomp('/')}#{WEBFINGER_SUFFIX}"
  raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url)

  new(webfinger_url)
end

.from_xml(hostmeta_xml) ⇒ Object

Reads the given Host Meta XML document string and populates the webfinger_url.

Parameters:

  • hostmeta_xml (String)

    Host Meta XML string

Raises:

  • (InvalidData)

    if the xml or the webfinger url is malformed



64
65
66
67
68
69
70
71
72
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 64

def self.from_xml(hostmeta_xml)
  data = XrdDocument.xml_data(hostmeta_xml)
  raise InvalidData, "received an invalid xml" unless data.key?(:links)

  webfinger_url = webfinger_url_from_xrd(data)
  raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url)

  new(webfinger_url)
end

Instance Method Details

#selfString

Gets the webfinger url from an XRD data structure

Parameters:

  • data (Hash)

    extracted data

Returns:

  • (String)

    webfinger url



77
78
79
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 77

private_class_method def self.webfinger_url_valid?(url)
  !url.nil? && url.instance_of?(String) && url =~ %r{\Ahttps?:\/\/.*\/.*\{uri\}.*}i
end

#to_xmlString

Produces the XML string for the Host Meta instance with a Link element containing the webfinger_url.

Returns:

  • (String)

    XML string



40
41
42
43
44
45
46
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 40

def to_xml
  doc = XrdDocument.new
  doc.links << {rel:      "lrdd",
                type:     "application/xrd+xml",
                template: @webfinger_url}
  doc.to_xml
end

#webfinger_template_urlString

Returns the WebFinger URL that was used to build this instance (either from xml or by giving a base URL).

Returns:

  • (String)

    WebFinger template URL



33
34
35
# File 'lib/diaspora_federation/discovery/host_meta.rb', line 33

def webfinger_template_url
  @webfinger_url
end