# frozen_string_literal: true
class WebfingerResource
attr_reader :resource
def initialize(resource)
@resource = resource
end
def username
case resource
when /\Ahttps?/i
username_from_url
when /\@/
username_from_acct
else
raise(ActiveRecord::RecordNotFound)
private
def username_from_url
if account_show_page?
path_params[:username]
raise ActiveRecord::RecordNotFound
def account_show_page?
path_params[:controller] == 'accounts' && path_params[:action] == 'show'
def path_params
Rails.application.routes.recognize_path(resource)
def username_from_acct
if domain_matches_local?
local_username
def split_acct
resource_without_acct_string.split('@')
def resource_without_acct_string
resource.gsub(/\Aacct:/, '')
def local_username
split_acct.first
def local_domain
split_acct.last
def domain_matches_local?
TagManager.instance.local_domain?(local_domain) || TagManager.instance.web_domain?(local_domain)