Class: GptTranslate::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-chatgpt-translate/ping.rb

Overview

Ping one page of a site.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2023-2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(site, path) ⇒ Ping

Ctor.



43
44
45
46
47
# File 'lib/jekyll-chatgpt-translate/ping.rb', line 43

def initialize(site, path)
  @site = site
  raise 'Permalink must start with a slash' unless path.start_with?('/')
  @path = path
end

Instance Method Details

#downloadObject

Downloads the page from the Internet and returns HTML or NIL, if the page is absent



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-chatgpt-translate/ping.rb', line 50

def download
  home = @site.config['url']
  return nil if home.nil?
  uri = Iri.new(home).path(@path).to_s
  html = nil
  begin
    response = Net::HTTP.get_response(URI(uri))
    html = response.body if response.is_a?(Net::HTTPSuccess)
    Jekyll.logger.debug("GET #{uri.inspect}: #{response.code}")
  rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL => e
    Jekyll.logger.debug("Failed to ping #{uri.inspect} (#{e.class.name}): #{e.message}")
  end
  html
end