Created
June 5, 2011 00:15
-
-
Save maxwell/1008521 to your computer and use it in GitHub Desktop.
using faraday magic for webfinger parsing. modified code from sporkmonger webfinger repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#tons take from sporkmonger's webfinger github | |
require File.join(Rails.root, 'lib/webfinger/hcard') | |
require File.join(Rails.root, 'lib/webfinger/webfinger_profile') | |
require 'addressable/uri' | |
require 'addressable/template' | |
require 'xrd' | |
class Webfinger | |
def self.lookup(email) | |
setup_faraday | |
email = self.normalize_acct_uri(email) | |
hostname = email.path.split('@')[-1] | |
hostmeta_xrd = self.fetch_hostmeta(hostname) | |
template = self.lrdd_template(hostmeta_xrd) | |
webfinger_uri = template.expand('uri' => email) | |
return XRD.parse(Faraday.get(webfinger_uri).body) | |
end | |
def self.fetch_hostmeta(hostname) | |
hostmeta_uri = Addressable::Template.new( | |
'https://{hostname}/.well-known/host-meta' | |
).expand('hostname' => hostname) | |
begin | |
xrd = XRD.parse(Faraday.get(hostmeta_uri).body) | |
rescue OpenSSL::SSL::SSLError => e | |
# Needs to handle 404 case | |
warn(e) | |
if hostmeta_uri.scheme == 'https' | |
hostmeta_uri.scheme = 'http' | |
retry | |
else | |
raise e | |
end | |
end | |
end | |
def self.setup_faraday | |
Faraday.default_connection = Faraday::Connection.new '', :ssl => {:ca_path =>'/System/Library/OpenSSL/certs'} do |builder| | |
if RUBY_VERSION.include?('1.9.2') && EM.reactor_running? | |
puts "using async hottness" | |
builder.use Faraday::Adapter::EMSynchrony# make http requests with Net::HTTP | |
else | |
puts "using good ole nethttp" | |
builder.use Faraday::Adapter::NetHttp # make http requests with Net::HTTP | |
end | |
end | |
end | |
def self.normalize_acct_uri(acct_uri) | |
acct_uri = Addressable::URI.parse(acct_uri).omit(:query, :fragment) | |
acct_uri.scheme = 'acct' | |
return acct_uri.normalize | |
end | |
def self.lrdd_template(xrd) | |
return Addressable::Template.new(xrd.links(:rel => 'lrdd')[0].template) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment