diff --git a/lib/voight_kampff.rb b/lib/voight_kampff.rb index 317cf1d..0eb44fb 100644 --- a/lib/voight_kampff.rb +++ b/lib/voight_kampff.rb @@ -1,7 +1,8 @@ require 'json' require 'voight_kampff/test' -require 'voight_kampff/rack_request' if defined?(Rack) +require 'voight_kampff/methods' +require 'voight_kampff/rack_request' if defined?(Rack::Request) require 'voight_kampff/engine' if defined?(Rails) module VoightKampff diff --git a/lib/voight_kampff/engine.rb b/lib/voight_kampff/engine.rb index aeb6a4a..6f8f96f 100644 --- a/lib/voight_kampff/engine.rb +++ b/lib/voight_kampff/engine.rb @@ -3,5 +3,11 @@ class Engine < Rails::Engine rake_tasks do load 'tasks/voight_kampff.rake' end + + initializer :add_voight_kampff_methods do |app| + ActionDispatch::Request.class_eval do + include VoightKampff::Methods + end + end end end diff --git a/lib/voight_kampff/methods.rb b/lib/voight_kampff/methods.rb new file mode 100644 index 0000000..67e4e0e --- /dev/null +++ b/lib/voight_kampff/methods.rb @@ -0,0 +1,10 @@ +module VoightKampff::Methods + def human? + VoightKampff::Test.new(user_agent).human? + end + + def bot? + VoightKampff::Test.new(user_agent).bot? + end + alias :replicant? :bot? +end diff --git a/lib/voight_kampff/rack_request.rb b/lib/voight_kampff/rack_request.rb index 7a78ec8..a7e1e00 100644 --- a/lib/voight_kampff/rack_request.rb +++ b/lib/voight_kampff/rack_request.rb @@ -1,11 +1,4 @@ # Reopen the Rack::Request class to add bot detection methods -class Rack::Request - def human? - VoightKampff::Test.new(user_agent).human? - end - - def bot? - VoightKampff::Test.new(user_agent).bot? - end - alias :replicant? :bot? +Rack::Request.class_eval do + include VoightKampff::Methods end diff --git a/spec/controllers/replicants_controller_spec.rb b/spec/controllers/replicants_controller_spec.rb index a12ce81..502df1d 100644 --- a/spec/controllers/replicants_controller_spec.rb +++ b/spec/controllers/replicants_controller_spec.rb @@ -3,7 +3,7 @@ describe ReplicantsController, type: :controller do let(:user_agent_string) { '' } before do - expect(request).to receive(:user_agent).and_return user_agent_string + expect_any_instance_of(ActionController::TestRequest).to receive(:user_agent).and_return user_agent_string get :index end diff --git a/voight_kampff.gemspec b/voight_kampff.gemspec index a9dbcf6..3d8246a 100644 --- a/voight_kampff.gemspec +++ b/voight_kampff.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.add_dependency 'rack', ['>= 1.4', '< 3.0'] - s.add_development_dependency 'rails', '~> 4.2' + s.add_development_dependency 'rails', '~> 5.0' s.add_development_dependency 'rspec-rails', '~> 3.3' s.add_development_dependency 'combustion', '~> 0.5' end