-
Notifications
You must be signed in to change notification settings - Fork 44
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
Various improvements #41
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
indent_style = space | ||
indent_size = 4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Gemfile.lock | |
*.rbc | ||
coverage | ||
doc | ||
*.gem |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
AllCops: | ||
TargetRubyVersion: 2.4 | ||
NewCops: enable | ||
|
||
Layout/MultilineMethodCallIndentation: | ||
EnforcedStyle: indented | ||
|
||
Metrics/BlockLength: | ||
Exclude: | ||
- spec/**/* | ||
|
||
Metrics/LineLength: | ||
Exclude: | ||
- spec/support/* |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gemspec |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
require 'json' | ||
# frozen_string_literal: true | ||
|
||
require 'voight_kampff/test' | ||
require 'voight_kampff/methods' | ||
|
||
# Class helper methods | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try to run RuboCop without it? |
||
module VoightKampff | ||
class << self | ||
def root | ||
require 'pathname' | ||
Pathname.new File.expand_path '..', File.dirname(__FILE__) | ||
end | ||
ROOT = File.expand_path '..', __dir__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to use brackets for more readability There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're writing text in natural language (like English) mostly without parentheses, so "readability" is very subjective. |
||
|
||
class << self | ||
def human?(user_agent_string) | ||
test(user_agent_string).human? | ||
end | ||
|
||
def bot?(user_agent_string) | ||
test(user_agent_string).bot? | ||
end | ||
alias :replicant? :bot? | ||
alias replicant? bot? | ||
|
||
private | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module VoightKampff | ||
# Integration with Rails | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Necessary for RuboCop success. |
||
class Engine < Rails::Engine | ||
rake_tasks do | ||
load 'tasks/voight_kampff.rake' | ||
end | ||
|
||
initializer :add_voight_kampff_methods do |app| | ||
initializer :add_voight_kampff_methods do |_app| | ||
ActionDispatch::Request.class_eval do | ||
include VoightKampff::Methods | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
module VoightKampff::Methods | ||
def human? | ||
VoightKampff::Test.new(user_agent).human? | ||
end | ||
# frozen_string_literal: true | ||
|
||
module VoightKampff | ||
# Helper for Rack::Request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
module Methods | ||
extend Forwardable | ||
def_delegators :voight_kampff, :human?, :bot?, :replicant? | ||
|
||
private | ||
|
||
def bot? | ||
VoightKampff::Test.new(user_agent).bot? | ||
def voight_kampff | ||
VoightKampff::Test.new(user_agent) | ||
end | ||
end | ||
alias :replicant? :bot? | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'voight_kampff' | ||
require 'voight_kampff/rack_request' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
# Reopen the Rack::Request class to add bot detection methods | ||
Rack::Request.class_eval do | ||
include VoightKampff::Methods | ||
end | ||
Rack::Request.include VoightKampff::Methods if defined?(Rack::Request) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'voight_kampff' | ||
require 'voight_kampff/rack' | ||
require 'voight_kampff/engine' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module VoightKampff | ||
# Test User-Agent against Voight-Kampff | ||
class Test | ||
CRAWLERS_FILENAME = 'crawler-user-agents.json' | ||
|
||
class << self | ||
def crawlers | ||
@crawlers ||= JSON.parse File.read preferred_path | ||
end | ||
|
||
def crawler_regexp | ||
@crawler_regexp ||= begin | ||
# NOTE: This is admittedly a bit convoluted | ||
# but the performance gains make it worthwhile | ||
crawler_patterns = | ||
crawlers.map.with_index do |crawler, index| | ||
"(?<match#{index}>#{crawler['pattern']})" | ||
end.join('|') | ||
crawler_patterns = "(#{crawler_patterns})" | ||
Regexp.new(crawler_patterns, Regexp::IGNORECASE) | ||
end | ||
end | ||
|
||
private | ||
|
||
def lookup_paths | ||
AlexWayfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# These paths should be orderd by priority | ||
[ | ||
(Rails.root if defined? Rails), | ||
Dir.pwd, | ||
VoightKampff::ROOT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constant ROOT is needed only here, may be calculate root path of the gem here without unnecessary constants? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've made it similar to |
||
] | ||
end | ||
|
||
def preferred_path | ||
AlexWayfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lookup_paths.find do |base_path| | ||
next unless base_path | ||
|
||
path = File.join base_path, 'tmp', CRAWLERS_FILENAME | ||
|
||
break path if File.exist? path | ||
end | ||
end | ||
AlexWayfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
attr_accessor :user_agent_string | ||
|
||
def initialize(user_agent_string) | ||
|
@@ -19,42 +62,16 @@ def human? | |
def bot? | ||
!human? | ||
end | ||
alias :replicant? :bot? | ||
alias replicant? bot? | ||
|
||
private | ||
|
||
def lookup_paths | ||
# These paths should be orderd by priority | ||
base_paths = [] | ||
base_paths << Rails.root if defined? Rails | ||
base_paths << VoightKampff.root | ||
|
||
base_paths.map { |p| p.join('config', CRAWLERS_FILENAME) } | ||
end | ||
|
||
def preferred_path | ||
lookup_paths.find { |path| File.exist? path } | ||
end | ||
|
||
def matching_crawler | ||
if match = crawler_regexp.match(@user_agent_string) | ||
index = match.names.first.sub(/match/, '').to_i | ||
crawlers[index] | ||
end | ||
end | ||
|
||
def crawler_regexp | ||
@@crawler_regexp ||= begin | ||
# NOTE: This is admittedly a bit convoluted but the performance gains make it worthwhile | ||
index = -1 | ||
crawler_patterns = crawlers.map{|c| index += 1; "(?<match#{index}>#{c["pattern"]})" }.join("|") | ||
crawler_patterns = "(#{crawler_patterns})" | ||
Regexp.new(crawler_patterns, Regexp::IGNORECASE) | ||
end | ||
end | ||
match = self.class.crawler_regexp.match(@user_agent_string) | ||
return unless match | ||
|
||
def crawlers | ||
@@crawlers ||= JSON.load(File.open(preferred_path, 'r')) | ||
index = match.names.first.sub(/match/, '').to_i | ||
self.class.crawlers[index] | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not required, but it helps to configure an editor of any contributor to right (for project) settings and prevent too long lines, whitespaces at line ends, no EOL symbols in files, etc. Like this: https://github.com/biola/Voight-Kampff/pull/41/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L54
It's pretty common standard, without OS or editor lock, so I don't see a problem with it, only help.