0

I am trying to exclude a range of ip addresses from being tracked with the Ahoy gem. The docs give this example:

Ahoy.exclude_method = lambda do |controller, request|
  request.ip == "192.168.1.1"
end

In the ahoy.rb initializer file I have:

 Ahoy.exclude_method = lambda do |controller, request|
    range = IPAddr.new("66.249.66.0/24")
    range.include?(request.ip) || request.url.split("/").include?("serviceworker.js")
  end

Rollbar is reporting constant errors from the ahoy.rb file:

NoMethodError: [safely] undefined method `ip' for nil:NilClass

Perhaps I don't understand what is a request, but if someone is on my site, I'm presuming there should be a valid non-nil request. Is that right?

Apparently the answer is no, that is not right since obviously a request is often nil, and I'm thinking the next step is to exclude this error from rollbar reporting and move on since the error does not impact user experience on the site. Am I thinking straight on this?

tomb
  • 1,374
  • 1
  • 13
  • 23
  • How is this lambda being called? Are you sure you're capturing the right arguments? – tadman May 30 '19 at 15:49
  • The short answer is I don't know, but I can make a guess that the Ahoy gem has included code somewhere that checks the exclude_method before recording a visit or event. – tomb May 30 '19 at 15:55
  • I am now wondering if controller is supposed to be a specific controller? I want to exclude visits and events on the entire site, maybe I should change 'controller' to application_controller.rb or simply application? – tomb May 30 '19 at 16:02
  • `binding.irb unless request.ip`, then re-run your request and inspect the local variables when the binding is triggered. – anothermh May 30 '19 at 19:17

0 Answers0