2017-07-19 23:36:11 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-17 15:13:19 +01:00
|
|
|
require 'addressable'
|
|
|
|
require 'nokogiri'
|
|
|
|
|
|
|
|
module Goldfinger
|
|
|
|
class Client
|
|
|
|
include Goldfinger::Utils
|
|
|
|
|
|
|
|
def initialize(uri)
|
|
|
|
@uri = uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def finger
|
2017-07-19 23:36:11 +02:00
|
|
|
response = perform_get(standard_url)
|
2016-03-07 12:23:48 +01:00
|
|
|
|
2017-07-19 23:36:11 +02:00
|
|
|
return finger_from_template if response.code != 200
|
2016-10-12 20:58:17 +02:00
|
|
|
|
2017-07-19 23:36:11 +02:00
|
|
|
Goldfinger::Result.new(response)
|
2017-04-27 16:25:20 +02:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
raise Goldfinger::NotFoundError, 'Invalid URI'
|
2016-02-17 15:13:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-27 16:25:20 +02:00
|
|
|
def finger_from_template
|
2017-07-19 23:36:11 +02:00
|
|
|
template = perform_get(url)
|
2017-04-27 16:25:20 +02:00
|
|
|
|
|
|
|
raise Goldfinger::NotFoundError, 'No host-meta on the server' if template.code != 200
|
|
|
|
|
|
|
|
response = perform_get(url_from_template(template.body))
|
|
|
|
|
|
|
|
raise Goldfinger::NotFoundError, 'No such user on the server' if response.code != 200
|
|
|
|
|
|
|
|
Goldfinger::Result.new(response)
|
|
|
|
end
|
|
|
|
|
2017-07-19 23:36:11 +02:00
|
|
|
def url
|
|
|
|
"https://#{domain}/.well-known/host-meta"
|
2016-02-17 15:13:19 +01:00
|
|
|
end
|
|
|
|
|
2017-07-19 23:36:11 +02:00
|
|
|
def standard_url
|
|
|
|
"https://#{domain}/.well-known/webfinger?resource=#{@uri}"
|
2017-04-27 16:25:20 +02:00
|
|
|
end
|
|
|
|
|
2016-02-17 15:13:19 +01:00
|
|
|
def url_from_template(template)
|
|
|
|
xml = Nokogiri::XML(template)
|
2016-02-17 23:16:08 +01:00
|
|
|
links = xml.xpath('//xmlns:Link[@rel="lrdd"]')
|
2016-02-17 15:13:19 +01:00
|
|
|
|
2016-02-17 16:58:02 +01:00
|
|
|
raise Goldfinger::NotFoundError if links.empty?
|
2016-02-17 15:13:19 +01:00
|
|
|
|
2016-03-07 12:23:48 +01:00
|
|
|
links.first.attribute('template').value.gsub('{uri}', @uri)
|
2016-09-30 23:06:11 +02:00
|
|
|
rescue Nokogiri::XML::XPath::SyntaxError
|
2016-10-12 20:58:17 +02:00
|
|
|
raise Goldfinger::Error, "Bad XML: #{template}"
|
2016-02-17 15:13:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def domain
|
|
|
|
@uri.split('@').last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|