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

"/webfinger?q={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



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

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:



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

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



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

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

#to_xmlString

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

Returns:

  • (String)

    XML string



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

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



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

def webfinger_template_url
  @webfinger_url
end