Skip to content

Commit c78f24d

Browse files
committed
Various improvements
Removed ------- * `rack` as dependency * Unused `config.ru` Changed ------- * `voight_kampff/rack_request` isn't required by default * Default path for cache is now in `./tmp` directory instead of `./config` Development ----------- * [EditorConfig](https://editorconfig.org/) added * [RuboCop](https://github.com/rubocop-hq/rubocop) added and offenses fixed * RuboCop task added for Travis CI * Drop support for Ruby <= 2.2 (they even will not get security patches) * Ignore built gems for git
1 parent 0590f17 commit c78f24d

26 files changed

Lines changed: 191 additions & 110 deletions

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Gemfile.lock
55
*.rbc
66
coverage
77
doc
8+
*.gem

.rubocop.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
AllCops:
2+
TargetRubyVersion: 2.4
3+
4+
Layout/MultilineMethodCallIndentation:
5+
EnforcedStyle: indented
6+
7+
Metrics/BlockLength:
8+
Exclude:
9+
- spec/**/*
10+
11+
Metrics/LineLength:
12+
Exclude:
13+
- spec/support/*

.ruby-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

.travis.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
language: ruby
2+
23
rvm:
3-
- 2.4.6
4-
- 2.5.5
5-
- 2.6.2
6-
script: bundle exec rspec
4+
- 2.4
5+
- 2.5
6+
- 2.6
7+
78
before_install:
9+
- gem update --system
810
# fixes Travis CI error: NoMethodError: undefined method `spec' for nil:NilClass
911
- gem install bundler
12+
13+
cache: bundler
14+
15+
script:
16+
- bundle exec rspec
17+
- bundle exec rubocop

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
gemspec

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,34 @@ Configuration
1515

1616
A JSON file is used to match [user agent strings](http://simplyfast.info/browser) to a list of known bots.
1717

18-
If you'd like to use an [updated list](https://github.com/monperrus/crawler-user-agents) or make your own customizations, run `rake voight_kampff:import_user_agents`. This will download a `crawler-user-agents.json` file into the `./config` directory.
18+
If you'd like to use an [updated list](https://github.com/monperrus/crawler-user-agents) or make your own customizations, run `rake voight_kampff:import_user_agents`. This will download a `crawler-user-agents.json` file into the `./tmp` directory.
1919

2020
__Note:__ The pattern entries in the JSON file are evaluated as [regular expressions](http://en.wikipedia.org/wiki/Regular_expression).
2121

2222
Usage
2323
-----
2424
There are three ways to use Voight-Kampff
2525

26-
1. Through Rack::Request such as in your [Ruby on Rails](http://rubyonrails.org) controllers:
27-
`request.bot?`
26+
1. Through `Rack::Request` in your app such as [Ruby on Rails](http://rubyonrails.org):
27+
```ruby
28+
require 'voight_kampff/rack_request'
2829

29-
2. Through the `VoightKampff` module:
30+
request.bot?
31+
```
32+
33+
2. Through the `VoightKampff` module:
3034
`VoightKampff.bot? 'your user agent string'`
3135

32-
3. Through a `VoightKampff::Test` instance:
36+
3. Through a `VoightKampff::Test` instance:
3337
`VoightKampff::Test.new('your user agent string').bot?`
3438

35-
All of the above examples accept `human?` and `bot?` methods. All of these methods will return `true` or `false`.
39+
All of the above examples accept `human?` and `bot?` methods.
40+
All of these methods will return `true` or `false`.
3641

3742
Upgrading to version 1.0
3843
------------------------
3944

40-
Version 1.0 uses a new source for a list of bot user agent strings since the old source was no longer maintained. This new source, unfortuately, does not include as much detail. Therefore the following methods have been deprecated:
45+
Version 1.0 uses a new source for a list of bot user agent strings since the old source was no longer maintained. This new source, unfortunately, does not include as much detail. Therefore the following methods have been deprecated:
4146
- `#browser?`
4247
- `#checker?`
4348
- `#downloader?`
@@ -51,12 +56,12 @@ Also, the gem no longer extends `ActionDispatch::Request` instead it extends `Ra
5156

5257
FAQ
5358
---
54-
__Q:__ __What's with the name?__
59+
__Q:__ __What's with the name?__
5560
__A:__ It's the [machine in Blade Runner](https://en.wikipedia.org/wiki/Blade_Runner#Voight-Kampff_machine) that is used to test whether someone is a human or a replicant.
5661

57-
__Q:__ __I've found a bot that isn't being matched__
62+
__Q:__ __I've found a bot that isn't being matched__
5863
__A:__ The list is being pulled from [github.com/monperrus/crawler-user-agents](https://github.com/monperrus/crawler-user-agents).
59-
If you'd like to have entries added to the list, please create a pull request with that project. Once that pull request is merged, feel free to create an issue here and I'll release a new gem version with the updated list. In the meantime you can always run `rake voight_kampff:import_user_agents` on your project to get that updated list.
64+
If you'd like to have entries added to the list, please create a pull request with that project. Once that pull request is merged, you'll be get the new version at the next cache update. In the meantime you can always manually flush the cache in your project to get that updated list.
6065

6166
__Q:__ __Why don't you use the user agent list from ______________
6267
If you know of a better source for a list of bot user agent strings, please create an issue and let me know. I'm open to switching to a better source or supporting multiple sources. There are others out there but I like the openness of monperrus' list.
@@ -67,7 +72,7 @@ Thanks to [github.com/monperrus/crawler-user-agents](https://github.com/monperru
6772

6873
Contributing
6974
------------
70-
PR without tests will not get merged, Make sure you write tests for api and rails app.
75+
PR without tests will not get merged, Make sure you write tests for API and Rails app.
7176
Feel free to ask for help, if you do not know how to write a determined test.
7277

7378
Running Tests?

config.ru

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/tasks/voight_kampff.rake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
namespace :voight_kampff do
24
desc 'Import a new crawler-user-agents.json file'
3-
task :import_user_agents, :url do |t, args|
5+
task :import_user_agents, :url do |_t, args|
46
args.with_defaults url: 'https://raw.githubusercontent.com/monperrus/crawler-user-agents/master/crawler-user-agents.json'
57

68
require 'net/http'
@@ -9,8 +11,10 @@ namespace :voight_kampff do
911
contents = Net::HTTP.get(uri)
1012

1113
if contents.present?
12-
file = File.open('./config/crawler-user-agents.json', 'w')
13-
file.write(contents.force_encoding(Encoding::UTF_8))
14+
File.write(
15+
'./tmp/crawler-user-agents.json',
16+
contents.force_encoding(Encoding::UTF_8)
17+
)
1418
else
1519
puts "voight_kampff:import_user_agents - empty file received from #{uri}"
1620
end

lib/voight_kampff.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
require 'json'
1+
# frozen_string_literal: true
22

33
require 'voight_kampff/test'
44
require 'voight_kampff/methods'
5-
require 'voight_kampff/rack_request' if defined?(Rack::Request)
65
require 'voight_kampff/engine' if defined?(Rails)
76

7+
# Class helper methods
88
module VoightKampff
9-
class << self
10-
def root
11-
require 'pathname'
12-
Pathname.new File.expand_path '..', File.dirname(__FILE__)
13-
end
9+
ROOT = File.expand_path '..', __dir__
1410

11+
class << self
1512
def human?(user_agent_string)
1613
test(user_agent_string).human?
1714
end
1815

1916
def bot?(user_agent_string)
2017
test(user_agent_string).bot?
2118
end
22-
alias :replicant? :bot?
19+
alias replicant? bot?
2320

2421
private
2522

0 commit comments

Comments
 (0)