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
|
2016-02-17 16:18:46 +01:00
|
|
|
ssl = true
|
|
|
|
|
|
|
|
begin
|
2016-03-07 12:23:48 +01:00
|
|
|
_, template = perform_get(url(ssl))
|
2016-02-17 16:18:46 +01:00
|
|
|
rescue HTTP::Error
|
|
|
|
if ssl
|
|
|
|
ssl = false
|
|
|
|
retry
|
|
|
|
else
|
2016-02-17 16:58:02 +01:00
|
|
|
raise Goldfinger::NotFoundError
|
2016-02-17 16:18:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-17 15:13:19 +01:00
|
|
|
headers, body = perform_get(url_from_template(template))
|
2016-03-07 12:23:48 +01:00
|
|
|
|
|
|
|
raise Goldfinger::Error, "Invalid response mime type: #{headers.get(HTTP::Headers::CONTENT_TYPE).first}" unless ['application/jrd+json', 'application/xrd+xml'].include?(headers.get(HTTP::Headers::CONTENT_TYPE).first)
|
|
|
|
|
2016-02-17 15:13:19 +01:00
|
|
|
Goldfinger::Result.new(headers, body)
|
2016-02-17 16:18:46 +01:00
|
|
|
rescue HTTP::Error
|
2016-02-17 16:58:02 +01:00
|
|
|
raise Goldfinger::NotFoundError
|
|
|
|
rescue OpenSSL::SSL::SSLError
|
|
|
|
raise Goldfinger::SSLError
|
2016-02-17 15:13:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def url(ssl = true)
|
|
|
|
"http#{'s' if ssl}://#{domain}/.well-known/host-meta"
|
|
|
|
end
|
|
|
|
|
|
|
|
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-02-17 15:13:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def domain
|
|
|
|
@uri.split('@').last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|