class Linksta::Getter

Attributes

base[R]
concurrency[R]
headers[R]
hydra[R]
paths[R]
status[R]

Public Class Methods

new(paths, base, concurrency, status, headers) click to toggle source
# File lib/linksta.rb, line 80
def initialize(paths, base, concurrency, status, headers)
  @paths       = paths
  @base        = base
  @headers     = headers
  @status      = status

  @hydra = Typhoeus::Hydra.new(:max_concurrency => concurrency)
end

Public Instance Methods

check() click to toggle source
# File lib/linksta.rb, line 89
def check
  puts "Checking..."

  paths.each do |path|
    begin
      Typhoeus::Request.new(url(path), options).tap do |req|
        req.on_complete { |r| parse_response(r, status) }
        hydra.queue req
      end
    rescue URI::InvalidURIError
      puts "Error with URL #{path}, please check config."
    end
  end

  hydra.run
  check_for_broken
end

Private Instance Methods

check_for_broken() click to toggle source
# File lib/linksta.rb, line 111
def check_for_broken
  puts "Checking"
  if output.empty?
    puts 'URL\'s are good, All Done!'
    exit 0
  else
    puts "Buddy, you got a bad link"
    puts output
    exit 1
  end
end
make_request(url, status, status_code) click to toggle source
# File lib/linksta.rb, line 123
def make_request(url, status, status_code)
  if status != status_code
    puts "Status is NOT GOOD for #{url}, response is #{status}"
    output << url
  else
    puts "Status is #{status} for #{url}"
  end
end
options() click to toggle source
# File lib/linksta.rb, line 132
def options
  {
      :followlocation => true,
      :ssl_verifypeer => false,
      :headers        => headers[:headers]
  }
end
output() click to toggle source
# File lib/linksta.rb, line 140
def output
  @output ||= []
end
parse_response(resp, status) click to toggle source
# File lib/linksta.rb, line 144
def parse_response(resp, status)
  make_request(
      resp.options[:effective_url],
      resp.code,
      status
  )
rescue
  puts "Unable to get status code"
end
url(path) click to toggle source
# File lib/linksta.rb, line 154
def url(path)
  "#{base}#{path}"
end