Skip to content

Commit 7833390

Browse files
Primitive obsession to URI
1 parent 0e4a4c7 commit 7833390

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

lib/node_info/client.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'http'
22
require 'json'
3+
require 'uri'
34

45
module NodeInfo
56
# Client for discovering and fetching NodeInfo from Fediverse servers
@@ -44,7 +45,7 @@ def fetch domain
4445
def discover domain
4546
url = normalize_url domain, WELL_KNOWN_PATH
4647

47-
response = http_client.get(url)
48+
response = http_client.get url
4849

4950
raise DiscoveryError, "HTTP #{response.code}" unless response.status.success?
5051

@@ -77,22 +78,19 @@ def http_client
7778
end
7879

7980
def normalize_url domain, path
80-
# Remove protocol if present
81-
domain = domain.sub %r{^https?://}, ''
82-
83-
# Remove trailing slash
84-
domain = domain.chomp '/'
81+
url = URI.parse domain
82+
url = URI.parse("https://#{domain}") unless domain.start_with? 'http'
83+
host = url.host
8584

86-
# Add https protocol
87-
"https://#{domain}#{path}"
85+
URI::HTTPS.build(host: host, path: path).to_s
8886
end
8987

9088
def parse_well_known body
91-
data = JSON.parse(body)
89+
data = JSON.parse body
9290
links = data['links']
9391

9492
raise DiscoveryError, 'No links found in well-known document' unless links
95-
raise DiscoveryError, 'Links must be an array' unless links.is_a?(Array)
93+
raise DiscoveryError, 'Links must be an array' unless links.is_a? Array
9694

9795
links
9896
rescue JSON::ParserError => e

0 commit comments

Comments
 (0)