Skip to content

Commit

Permalink
rubocop -a
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Feb 7, 2020
1 parent 25efd91 commit 7d78984
Show file tree
Hide file tree
Showing 40 changed files with 237 additions and 161 deletions.
13 changes: 4 additions & 9 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
Metrics/LineLength:
Enabled: false

Style/AlignHash:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table

Lint/EndAlignment:
AlignWith: variable
AutoCorrect: true

Metrics/MethodLength:
Enabled: false

Expand All @@ -29,6 +21,9 @@ Style/DoubleNegation:

Metrics/ClassLength:
Enabled: false

Style/ClassVars:
Enabled: false

Metrics/BlockLength:
Enabled: false
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0
2.5.3
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
2 changes: 2 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require './lib/site-inspector'
require 'rspec/core/rake_task'

Expand Down
1 change: 1 addition & 0 deletions bin/site-inspector
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'mercenary'
require 'oj'
Expand Down
5 changes: 4 additions & 1 deletion lib/cliver/dependency_ext.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Cliver
class Dependency
# Memoized shortcut for detect
Expand All @@ -10,12 +12,13 @@ def path
# Returns the version of the resolved dependency
def version
return @detected_version if defined? @detected_version

version = installed_versions.find { |p, _v| p == path }
@detected_version = version.nil? ? nil : version[1]
end

def major_version
version.split('.').first if version
version&.split('.')&.first
end
end
end
20 changes: 11 additions & 9 deletions lib/site-inspector.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'open-uri'
require 'addressable/uri'
require 'public_suffix'
Expand Down Expand Up @@ -29,11 +31,11 @@ class << self

def cache
@cache ||= if ENV['CACHE']
SiteInspector::DiskCache.new
elsif Object.const_defined?('Rails')
SiteInspector::RailsCache.new
else
SiteInspector::Cache.new
SiteInspector::DiskCache.new
elsif Object.const_defined?('Rails')
SiteInspector::RailsCache.new
else
SiteInspector::Cache.new
end
end

Expand All @@ -47,11 +49,11 @@ def inspect(domain)

def typhoeus_defaults
defaults = {
followlocation: false,
timeout: SiteInspector.timeout,
followlocation: false,
timeout: SiteInspector.timeout,
accept_encoding: 'gzip',
method: :head,
headers: {
method: :head,
headers: {
'User-Agent' => "Mozilla/5.0 (compatible; SiteInspector/#{SiteInspector::VERSION}; +https://github.com/benbalter/site-inspector)"
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/site-inspector/cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Cache
def memory
Expand Down
27 changes: 16 additions & 11 deletions lib/site-inspector/checks/accessibility.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'json'
require 'open3'

Expand All @@ -8,10 +10,10 @@ class Pa11yError < RuntimeError; end

STANDARDS = {
section508: 'Section508', # Default standard
wcag2a: 'WCAG2A',
wcag2aa: 'WCAG2AA',
wcag2aaa: 'WCAG2AAA'
}
wcag2a: 'WCAG2A',
wcag2aa: 'WCAG2AA',
wcag2aaa: 'WCAG2AAA'
}.freeze

DEFAULT_LEVEL = :error

Expand All @@ -24,7 +26,8 @@ def pa11y_version

def pa11y?
return @pa11y_detected if defined? @pa11y_detected
@pa11y_detected = !!(pa11y.detect)

@pa11y_detected = !!pa11y.detect
end

def enabled?
Expand All @@ -45,7 +48,8 @@ def level
end

def level=(level)
fail ArgumentError, "Invalid level '#{level}'" unless [:error, :warning, :notice].include?(level)
raise ArgumentError, "Invalid level '#{level}'" unless %i[error warning notice].include?(level)

@level = level
end

Expand All @@ -58,7 +62,8 @@ def standard
end

def standard=(standard)
fail ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)
raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)

@standard = standard
end

Expand All @@ -75,7 +80,7 @@ def check
rescue Pa11yError
nil
end
alias_method :to_h, :check
alias to_h check

def method_missing(method_sym, *arguments, &block)
if standard?(method_sym)
Expand All @@ -97,7 +102,7 @@ def respond_to?(method_sym, include_private = false)

def run_pa11y(standard)
self.class.pa11y.detect! unless ENV['SKIP_PA11Y_CHECK']
fail ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)
raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)

args = [
'--standard', STANDARDS[standard],
Expand All @@ -109,10 +114,10 @@ def run_pa11y(standard)

# Pa11y exit codes: https://github.com/nature/pa11y#exit-codes
# 0: No errors, 1: Technical error within pa11y, 2: accessibility error (configurable via --level)
fail Pa11yError if status == 1
raise Pa11yError if status == 1

{
valid: status == 0,
valid: status == 0,
results: JSON.parse(output)
}
rescue Pa11yError, JSON::ParserError
Expand Down
6 changes: 4 additions & 2 deletions lib/site-inspector/checks/check.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Check
Expand Down Expand Up @@ -40,11 +42,11 @@ def name
end

def enabled?
!!(@@enabled)
!!@@enabled
end

def enabled=(value)
@@enabled = !!(value)
@@enabled = !!value
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/site-inspector/checks/content.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Content < Check
Expand All @@ -16,7 +18,7 @@ def document
require 'nokogiri'
@doc ||= Nokogiri::HTML response.body if response
end
alias_method :doc, :document
alias doc document

def body
@body ||= document.to_s.force_encoding('UTF-8').encode('UTF-8', invalid: :replace, replace: '')
Expand All @@ -40,6 +42,7 @@ def doctype

def prefetch
return unless endpoint.up?

options = SiteInspector.typhoeus_defaults.merge(followlocation: true)
['robots.txt', 'sitemap.xml', 'humans.txt', random_path].each do |path|
request = Typhoeus::Request.new(URI.join(endpoint.uri, path), options)
Expand All @@ -55,10 +58,10 @@ def proper_404s?
def to_h
prefetch
{
doctype: doctype,
doctype: doctype,
sitemap_xml: sitemap_xml?,
robots_txt: robots_txt?,
humans_txt: humans_txt?,
robots_txt: robots_txt?,
humans_txt: humans_txt?,
proper_404s: proper_404s?
}
end
Expand Down
4 changes: 3 additions & 1 deletion lib/site-inspector/checks/cookies.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Cookies < Check
Expand All @@ -10,7 +12,7 @@ def any?(&block)
true
end
end
alias_method :cookies?, :any?
alias cookies? any?

def all
@cookies ||= cookie_header.map { |c| CGI::Cookie.parse(c) } if cookies?
Expand Down
17 changes: 10 additions & 7 deletions lib/site-inspector/checks/dns.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Dns < Check
Expand Down Expand Up @@ -25,7 +27,7 @@ def records
def record?(type)
records.any? { |record| record.type == type } || query(type).count != 0
end
alias_method :has_record?, :record?
alias has_record? record?

def dnssec?
@dnssec ||= has_record? 'DNSKEY'
Expand Down Expand Up @@ -87,14 +89,15 @@ def inspect

def to_h
return { error: LocalhostError } if localhost?

{
dnssec: dnssec?,
ipv6: ipv6?,
cdn: cdn,
dnssec: dnssec?,
ipv6: ipv6?,
cdn: cdn,
cloud_provider: cloud_provider,
google_apps: google_apps?,
hostname: hostname,
ip: ip
google_apps: google_apps?,
hostname: hostname,
ip: ip
}
end

Expand Down
14 changes: 8 additions & 6 deletions lib/site-inspector/checks/headers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Headers < Check
Expand Down Expand Up @@ -44,9 +46,9 @@ def xss_protection?

# Returns an array of hashes of downcased key/value header pairs (or an empty hash)
def all
@all ||= (response && response.headers) ? Hash[response.headers.map { |k, v| [k.downcase, v] }] : {}
@all ||= response&.headers ? Hash[response.headers.map { |k, v| [k.downcase, v] }] : {}
end
alias_method :headers, :all
alias headers all

def [](header)
headers[header]
Expand All @@ -55,10 +57,10 @@ def [](header)
def to_h
{
strict_transport_security: strict_transport_security || false,
content_security_policy: content_security_policy || false,
click_jacking_protection: click_jacking_protection || false,
server: server,
xss_protection: xss_protection || false
content_security_policy: content_security_policy || false,
click_jacking_protection: click_jacking_protection || false,
server: server,
xss_protection: xss_protection || false
}
end
end
Expand Down
14 changes: 9 additions & 5 deletions lib/site-inspector/checks/hsts.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
# Utility parser for HSTS headers.
# RFC: http://tools.ietf.org/html/rfc6797
class Hsts < Check
def valid?
return false unless header

pairs.none? { |key, value| "#{key}#{value}" =~ /[\s\'\"]/ }
end

Expand All @@ -22,6 +25,7 @@ def preload?

def enabled?
return false unless max_age

max_age > 0
end

Expand All @@ -32,12 +36,12 @@ def preload_ready?

def to_h
{
valid: valid?,
max_age: max_age,
valid: valid?,
max_age: max_age,
include_subdomains: include_subdomains?,
preload: preload?,
enabled: enabled?,
preload_ready: preload_ready?
preload: preload?,
enabled: enabled?,
preload_ready: preload_ready?
}
end

Expand Down
4 changes: 3 additions & 1 deletion lib/site-inspector/checks/https.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SiteInspector
class Endpoint
class Https < Check
Expand All @@ -23,7 +25,7 @@ def inspect

def to_h
{
valid: valid?,
valid: valid?,
return_code: response.return_code
}
end
Expand Down
Loading

0 comments on commit 7d78984

Please sign in to comment.