Skip to content

Commit ed05767

Browse files
committed
Fix wikipedia DNS.
1 parent 75981d9 commit ed05767

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

examples/Gemfile examples/gems.rb

File renamed without changes.

examples/wikipedia-dns.rb

+11-15
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,30 @@
2828

2929
require 'digest/md5'
3030

31-
require 'async/logger'
3231
require 'async/http/client'
3332
require 'async/dns/extensions/string'
34-
require 'async/http/url_endpoint'
33+
require 'async/http/endpoint'
3534

3635
# Encapsulates the logic for fetching information from Wikipedia.
3736
module Wikipedia
38-
ENDPOINT = Async::HTTP::URLEndpoint.parse("https://en.wikipedia.org")
37+
ENDPOINT = Async::HTTP::Endpoint.parse("https://en.wikipedia.org")
3938

4039
def self.lookup(title, logger: nil)
41-
client = Async::HTTP::Client.new([ENDPOINT])
40+
client = Async::HTTP::Client.new(ENDPOINT)
4241
url = self.summary_url(title)
4342

44-
logger&.info "Making request to #{ENDPOINT} for #{url}."
45-
response = client.get(url, {'Host' => ENDPOINT.hostname})
46-
logger&.info "Got response #{response.inspect}."
43+
logger&.debug "Making request to #{ENDPOINT} for #{url}."
44+
response = client.get(url, headers: {"user-agent" => "RubyDNS"})
45+
logger&.debug "Got response #{response.inspect}."
4746

4847
if response.status == 301
49-
return lookup(response.headers['HTTP_LOCATION'])
48+
return lookup(response.headers['location'], logger: logger)
5049
else
51-
return self.extract_summary(response.body).force_encoding('ASCII-8BIT')
50+
return self.extract_summary(response.body.read).force_encoding('ASCII-8BIT')
5251
end
52+
ensure
53+
response&.close
54+
client&.close
5355
end
5456

5557
def self.summary_url(title)
@@ -87,12 +89,6 @@ def startup
8789
on(:start) do
8890
# Process::Daemon::Privileges.change_user(RUN_AS)
8991

90-
if ARGV.include?('--debug')
91-
@logger.level = Logger::DEBUG
92-
else
93-
@logger.level = Logger::WARN
94-
end
95-
9692
@logger.info "Starting Wikipedia DNS..."
9793
end
9894

0 commit comments

Comments
 (0)