Class: DiasporaFederation::Discovery::XrdDocument
- Inherits:
-
Object
- Object
- DiasporaFederation::Discovery::XrdDocument
- Defined in:
- lib/diaspora_federation/discovery/xrd_document.rb
Overview
This class implements basic handling of XRD documents as far as it is necessary in the context of the protocols used with diaspora* federation.
It also implements handling of the JRD format, see RFC 6415, Appendix A for a description of the JSON format.
Constant Summary collapse
- XMLNS =
xml namespace url
"http://docs.oasis-open.org/ns/xri/xrd-1.0"
- LINK_ATTRS =
Link
element attributes %i[rel type href template].freeze
- DATETIME_FORMAT =
format string for datetime (
Expires
element) "%Y-%m-%dT%H:%M:%SZ"
Instance Attribute Summary collapse
-
#aliases ⇒ Array<String>
readonly
List of alias URIs.
-
#expires ⇒ Object
writeonly
The <Expires> element contains a time value which specifies the instant at and after which the document has expired and SHOULD NOT be used.
-
#links ⇒ Array<Hash<attr => val>>
readonly
List of
Link
element hashes. -
#properties ⇒ Hash<String => mixed>
readonly
List of properties.
-
#subject ⇒ Object
writeonly
The <Subject> element contains a URI value which identifies the resource described by this XRD.
Class Method Summary collapse
-
.json_data(jrd_doc) ⇒ Hash
Parse the JRD document from the given string and create a hash containing the extracted data with symbolized keys.
-
.xml_data(xrd_doc) ⇒ Hash
Parse the XRD document from the given string and create a hash containing the extracted data.
Instance Method Summary collapse
-
#initialize ⇒ XrdDocument
constructor
A new instance of XrdDocument.
- #to_json(*_args) ⇒ Object
-
#to_xml ⇒ String
Generates an XML document from the current instance and returns it as string.
Constructor Details
#initialize ⇒ XrdDocument
Returns a new instance of XrdDocument.
59 60 61 62 63 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 59 def initialize @aliases = [] @links = [] @properties = {} end |
Instance Attribute Details
#aliases ⇒ Array<String> (readonly)
Returns list of alias URIs.
48 49 50 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 48 def aliases @aliases end |
#expires=(value) ⇒ Object
The <Expires> element contains a time value which specifies the instant at and after which the document has expired and SHOULD NOT be used.
41 42 43 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 41 def expires=(value) @expires = value end |
#links ⇒ Array<Hash<attr => val>> (readonly)
Returns list of Link
element hashes. Each hash contains the attributesa and their associated values for the Link
element.
57 58 59 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 57 def links @links end |
#properties ⇒ Hash<String => mixed> (readonly)
Returns list of properties. Hash key represents the type
attribute, and the value is the element content.
52 53 54 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 52 def properties @properties end |
#subject=(value) ⇒ Object
The <Subject> element contains a URI value which identifies the resource described by this XRD.
45 46 47 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 45 def subject=(value) @subject = value end |
Class Method Details
.json_data(jrd_doc) ⇒ Hash
Parse the JRD document from the given string and create a hash containing the extracted data with symbolized keys.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 122 def self.json_data(jrd_doc) json_hash = JSON.parse(jrd_doc) { subject: json_hash["subject"], expires: (DateTime.strptime(json_hash["expires"], DATETIME_FORMAT) if json_hash.key?("expires")), aliases: json_hash["aliases"], properties: json_hash["properties"], links: symbolize_keys_for_links(json_hash["links"]) }.compact rescue JSON::JSONError => e raise InvalidDocument, "Not a JRD document: #{e.class}: #{e.[0..255].encode(Encoding.default_external, undef: :replace)}" end |
.xml_data(xrd_doc) ⇒ Hash
Parse the XRD document from the given string and create a hash containing the extracted data.
Small bonus: the hash structure that comes out of this method is the same as the one used to produce a JRD (JSON Resource Descriptor) or parsing it.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 100 def self.xml_data(xrd_doc) doc = parse_xrd_document(xrd_doc) {}.tap do |data| exp_elem = doc.at_xpath("xrd:XRD/xrd:Expires", NS) data[:expires] = DateTime.strptime(exp_elem.content, DATETIME_FORMAT) unless exp_elem.nil? subj_elem = doc.at_xpath("xrd:XRD/xrd:Subject", NS) data[:subject] = subj_elem.content unless subj_elem.nil? parse_aliases_from_xml_doc(doc, data) parse_properties_from_xml_doc(doc, data) parse_links_from_xml_doc(doc, data) end end |
Instance Method Details
#to_json(*_args) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 81 def to_json(*_args) { subject: subject, expires: (expires.strftime(DATETIME_FORMAT) if expires.instance_of?(DateTime)), aliases: (aliases if aliases.any?), properties: (properties if properties.any?), links: (links if links.any?) }.compact end |
#to_xml ⇒ String
Generates an XML document from the current instance and returns it as string
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/diaspora_federation/discovery/xrd_document.rb', line 67 def to_xml Nokogiri::XML::Builder.new(encoding: "UTF-8") {|xml| xml.XRD("xmlns" => XMLNS) { xml.Expires(expires.strftime(DATETIME_FORMAT)) if expires.instance_of?(DateTime) xml.Subject(subject) if !subject.nil? && !subject.empty? add_aliases_to(xml) add_properties_to(xml) add_links_to(xml) } }.to_xml end |