Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vladimir Kovalev 2 #54

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions Vladimir_Kovalev/2/checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require "csv"
require "benchmark"
require "net/http"
require "digest"
require "yaml"
require "optparse"
require "singleton"
require "fileutils"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

require "domainatrix"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


Dir["./lib/*.rb"].each { |file| require_relative file }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


class Options
include Singleton
attr_accessor :verbose, :nosubdomains, :filter, :solutions, :parallel, :cache

def self.method_missing(*name)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MissingRespondToMissing: When using method_missing, define respond_to_missing?.

instance.send(*name)
end
end

parser = OptionParser.new do |opts|
opts.banner = "Usage: checker.rb [options] file.csv"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
Options.verbose = v
end
opts.on("--no-subdomains", "Exclude subdomains") do |v|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Options.nosubdomains = v
end
opts.on("--filter=sales", "Body filter") do |v|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Options.filter = v
end
opts.on("--exclude-solutions", "Exclude OpenSource projects") do |v|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Options.solutions = v
end
opts.on("--parallel=N", "Using threads") do |v|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Options.parallel = v
end
opts.on("--no-cache", "Without cache") do |v|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Options.cache = v
end
end

begin
parser.parse!
rescue OptionParser::InvalidOption => e
puts e
puts parser.help
exit
end

if Options.verbose
p Options.instance
p ARGV
end

if ARGV.count != 1
puts parser.help
exit
end

unless File.exist? ARGV[0]
puts "File not found"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

puts parser.help
exit
end

Db.load_file ARGV[0]
Db.subdomains_clear if Options.nosubdomains
Db.populate

def info(row)
print " #{row[:code]} (#{row[:time]}) "
end

def print_percent(step)
print "#{100 / Db.total * step}% - "
end

1.upto(Db.total) do |i|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockLength: Block has too many lines. [29/25]

print_percent(i) unless Options.filter
print "#{Db.result[i][:host]} - "

cached = UrlCache.fetch(Db.result[i][:host])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UselessAssignment: Useless assignment to variable - cached.

if cached = UrlCache.fetch(Db.result[i][:host])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/AssignmentInCondition: Use == if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.

Db.result[i] = cached
puts " cached #{info(Db.result[i])}"
next
end

uri = URI::HTTP.build(host: Db.result[i][:host])
res = nil

begin
time = Benchmark.measure do
res = Net::HTTP.get_response(uri)
end
time_string = if time.real.truncate.positive?
"#{time.real.truncate}s"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationWidth: Use 2 (not -10) spaces for indentation.

else
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/ElseAlignment: Align else with if.

"#{time.real.to_s.split(".")[1][0..2]}ms"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiteralsInInterpolation: Prefer single-quoted strings inside interpolations.

end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EndAlignment: end at 103, 6 is not aligned with if at 99, 18.

Db.result[i].merge!({ code: res.code,
time: time_string,
body: Body.new(res.body) })
rescue => e
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RescueStandardError: Avoid rescuing without specifying an error class.

Db.result[i].merge!({ code: e.message })
ensure
UrlCache.push(Db.result[i][:host], Db.result[i])
end

info(Db.result[i])
puts "done"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end
puts Db.out
11 changes: 11 additions & 0 deletions Vladimir_Kovalev/2/lib/body.class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Body
attr_reader :body

def initialize(body)
@body = body
end

def match_keyword?(keyword)
[email protected](/#{keyword}/).empty?
end
end
49 changes: 49 additions & 0 deletions Vladimir_Kovalev/2/lib/db.class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Db
class << self
def load_file(csv_file)
@@all_data = CSV.read(csv_file).uniq
end

def csv
@@all_data
end

def subdomains_clear
@@all_data.reject! do |e|
subdomain = Domainatrix.parse(e).subdomain
true if subdomain.length.positive? && subdomain != "www"
end
end

def total
@@all_data.count
end

def populate
@@result = {}
@@all_data.each.with_index(1) do |e, i|
@@result[i] = { host: e[0] }
end
end

def result
@@result
end

def out
out = { Total: 0, Success: 0, Failed: 0, Errored: 0 }
@@result.map { |e| e[1][:code] }.each do |e|
case e
when /^[23]/
out[:Success] += 1
when /^[45]/
out[:Failed] += 1
else
out[:Errored] += 1
end
out[:Total] += 1
end
out
end
end
end
33 changes: 33 additions & 0 deletions Vladimir_Kovalev/2/lib/urlcache.class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class UrlCache
@@cache_dir_name = ".cache"
@@cache_dir = File.join(Dir.pwd, @@cache_dir_name)
class << self
def fetch(url)
f = self.filename(url)
puts "#{Time.now.to_i - File.atime(f).to_i} < #{60 * 60}" if Options.verbose
if self.exist?(f) && (Time.now.to_i - File.atime(f).to_i) < 60 * 60
YAML.safe_load(IO.read(f))
else
false
end
end

def hash_of_file(url)
Digest::MD5.hexdigest url
end

def filename(url)
File.join(@@cache_dir, hash_of_file(url))
end

def push(url, data)
FileUtils.rm self.filename(url) if self.exist?(self.filename(url))
IO.write(self.filename(url), data.to_yaml)
end

def exist?(file)
Dir.mkdir @@cache_dir unless Dir.exist? @@cache_dir
File.exist? file
end
end
end