refactor HostMeta

This commit is contained in:
Benjamin Neff 2015-07-22 00:46:57 +02:00
parent 20d4646332
commit fe3c6b6161
2 changed files with 16 additions and 12 deletions

View file

@ -19,8 +19,13 @@ module DiasporaFederation
class HostMeta class HostMeta
private_class_method :new private_class_method :new
# @param [String] webfinger_url the webfinger-url
def initialize(webfinger_url)
@webfinger_url = webfinger_url
end
# URL fragment to append to the base URL # URL fragment to append to the base URL
WEBFINGER_SUFFIX = "webfinger?q={uri}" WEBFINGER_SUFFIX = "/webfinger?q={uri}"
# Returns the WebFinger URL that was used to build this instance (either from # Returns the WebFinger URL that was used to build this instance (either from
# xml or by giving a base URL). # xml or by giving a base URL).
@ -42,18 +47,14 @@ module DiasporaFederation
# Builds a new HostMeta instance and constructs the WebFinger URL from the # Builds a new HostMeta instance and constructs the WebFinger URL from the
# given base URL by appending HostMeta::WEBFINGER_SUFFIX. # given base URL by appending HostMeta::WEBFINGER_SUFFIX.
# @param [String, URL] base_url the base-url for the webfinger-url
# @return [HostMeta] # @return [HostMeta]
# @raise [InvalidData] if the webfinger url is malformed # @raise [InvalidData] if the webfinger url is malformed
def self.from_base_url(base_url) def self.from_base_url(base_url)
raise ArgumentError, "base_url is not a String" unless base_url.instance_of?(String) webfinger_url = "#{base_url.to_s.chomp('/')}#{WEBFINGER_SUFFIX}"
base_url += "/" unless base_url.end_with?("/")
webfinger_url = base_url + WEBFINGER_SUFFIX
raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url) raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url)
hm = allocate new(webfinger_url)
hm.instance_variable_set(:@webfinger_url, webfinger_url)
hm
end end
# Reads the given Host Meta XML document string and populates the # Reads the given Host Meta XML document string and populates the
@ -67,16 +68,14 @@ module DiasporaFederation
webfinger_url = webfinger_url_from_xrd(data) webfinger_url = webfinger_url_from_xrd(data)
raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url) raise InvalidData, "invalid webfinger url: #{webfinger_url}" unless webfinger_url_valid?(webfinger_url)
hm = allocate new(webfinger_url)
hm.instance_variable_set(:@webfinger_url, webfinger_url)
hm
end end
# Applies some basic sanity-checking to the given URL # Applies some basic sanity-checking to the given URL
# @param [String] url validation subject # @param [String] url validation subject
# @return [Boolean] validation result # @return [Boolean] validation result
def self.webfinger_url_valid?(url) def self.webfinger_url_valid?(url)
!url.nil? && url.instance_of?(String) && url =~ %r{^https?:\/\/.*\{uri\}}i !url.nil? && url.instance_of?(String) && url =~ %r{^https?:\/\/.*\/.*\{uri\}.*}i
end end
private_class_method :webfinger_url_valid? private_class_method :webfinger_url_valid?

View file

@ -20,6 +20,11 @@ XML
expect(hm.to_xml).to eq(xml) expect(hm.to_xml).to eq(xml)
end end
it "converts object to string" do
hm = Discovery::HostMeta.from_base_url(URI(base_url))
expect(hm.to_xml).to eq(xml)
end
it "appends a '/' if necessary" do it "appends a '/' if necessary" do
hm = Discovery::HostMeta.from_base_url("https://pod.example.tld") hm = Discovery::HostMeta.from_base_url("https://pod.example.tld")
expect(hm.to_xml).to eq(xml) expect(hm.to_xml).to eq(xml)