class EasySitemap::Runner

Attributes

result[R]

Public Class Methods

new(urls, api_key: nil) click to toggle source
# File lib/easy_sitemap/runner.rb, line 6
def initialize(urls, api_key: nil)
  @urls = urls
  @api_key = api_key
  @result = {}
end
perform(filename: 'sitemap.xml', thread_count: 5, api_key: nil) click to toggle source
# File lib/easy_sitemap/runner.rb, line 12
def self.perform(filename: 'sitemap.xml', thread_count: 5, api_key: nil)
  doc = File.open(filename) { |f| Nokogiri::XML(f) }

  runners = doc.xpath("//loc").
      map(&:text).
      in_groups(thread_count, false).
      map { |group| Runner.new(group, api_key: api_key) }

  threads = runners.map { |runner| Thread.new { runner.perform } }
  threads.each { |thr| thr.join }

  runners.map(&:result).inject(:merge)
end

Public Instance Methods

perform() click to toggle source
# File lib/easy_sitemap/runner.rb, line 26
def perform
  @urls.each do |url|
    ping_url(url)
  end
end

Private Instance Methods

ping_url(url) click to toggle source
# File lib/easy_sitemap/runner.rb, line 34
def ping_url(url)
  r = {}

  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    h = {}
    h['X-Redmine-API-Key'] = @api_key if @api_key.present?

    response = HTTParty.get(url, follow_redirects: false, headers: h)

    r['status'] = response.code
  rescue Errno::ECONNREFUSED
    r['status'] = 410
  end

  ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  r['time'] = (ending - starting).round(2)

  @result[url] = r
end