Skip to content

Commit 993e95e

Browse files
committed
Various improvements
Removed ------- * `rack` as dependency * Unused `config.ru` Changed ------- * Default path for cache is now in `./tmp` directory instead of `./config` Added ----- * Support of Rails 6 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 93fe240 commit 993e95e

25 files changed

Lines changed: 197 additions & 111 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AllCops:
2+
TargetRubyVersion: 2.4
3+
NewCops: enable
4+
5+
Layout/MultilineMethodCallIndentation:
6+
EnforcedStyle: indented
7+
8+
Metrics/BlockLength:
9+
Exclude:
10+
- spec/**/*
11+
12+
Metrics/LineLength:
13+
Exclude:
14+
- 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.9
4-
- 2.5.7
5-
- 2.6.5
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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,32 @@ 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+
request.bot?
29+
```
2830

29-
2. Through the `VoightKampff` module:
31+
2. Through the `VoightKampff` module:
3032
`VoightKampff.bot? 'your user agent string'`
3133

32-
3. Through a `VoightKampff::Test` instance:
34+
3. Through a `VoightKampff::Test` instance:
3335
`VoightKampff::Test.new('your user agent string').bot?`
3436

35-
All of the above examples accept `human?` and `bot?` methods. All of these methods will return `true` or `false`.
37+
All of the above examples accept `human?` and `bot?` methods.
38+
All of these methods will return `true` or `false`.
3639

3740
Upgrading to version 1.0
3841
------------------------
3942

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:
43+
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:
4144
- `#browser?`
4245
- `#checker?`
4346
- `#downloader?`
@@ -51,10 +54,10 @@ Also, the gem no longer extends `ActionDispatch::Request` instead it extends `Ra
5154

5255
FAQ
5356
---
54-
__Q:__ __What's with the name?__
57+
__Q:__ __What's with the name?__
5558
__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.
5659

57-
__Q:__ __I've found a bot that isn't being matched__
60+
__Q:__ __I've found a bot that isn't being matched__
5861
__A:__ The list is being pulled from [github.com/monperrus/crawler-user-agents](https://github.com/monperrus/crawler-user-agents).
5962
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.
6063

@@ -67,7 +70,7 @@ Thanks to [github.com/monperrus/crawler-user-agents](https://github.com/monperru
6770

6871
Contributing
6972
------------
70-
PR without tests will not get merged, Make sure you write tests for api and rails app.
73+
PR without tests will not get merged, Make sure you write tests for API and Rails app.
7174
Feel free to ask for help, if you do not know how to write a determined test.
7275

7376
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
require 'json'
1+
# frozen_string_literal: true
22

33
require 'voight_kampff/test'
44
require 'voight_kampff/methods'
55
require 'voight_kampff/rack_request' if defined?(Rack::Request)
66
require 'voight_kampff/engine' if defined?(Rails::Engine)
77

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

12+
class << self
1513
def human?(user_agent_string)
1614
test(user_agent_string).human?
1715
end
1816

1917
def bot?(user_agent_string)
2018
test(user_agent_string).bot?
2119
end
22-
alias :replicant? :bot?
20+
alias replicant? bot?
2321

2422
private
2523

0 commit comments

Comments
 (0)