crawler_detect icon indicating copy to clipboard operation
crawler_detect copied to clipboard

allow is_crawler? & crawler_name to be called outside of rack

Open rgraff opened this issue 5 years ago • 6 comments

When is_crawler? is called outside the context of a rack request--for example in specs--it will raise an error. This prevents that.

     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /usr/local/bundle/gems/crawler_detect-1.0.1/lib/rack/crawler_detect.rb:11:in `is_crawler?'

rgraff avatar Aug 03 '20 18:08 rgraff

hello Robert,

in this case is_crawler? will return nil that can be misinterpreted

loadkpi avatar Nov 04 '20 21:11 loadkpi

@loadkpi good catch. updated.

rgraff avatar Nov 05 '20 23:11 rgraff

Is this going to be taken into account? I am facing the error in specs @rgraff @loadkpi

Jandrov avatar Dec 23 '21 17:12 Jandrov

@Jandrov I don't know the status of getting this merged. In the meantime, you can fork my branch if you want to get your specs passing.

rgraff avatar Dec 23 '21 17:12 rgraff

hello guys!

could you provide the example of how it can be used outside of rack? I don't really understand

this PR makes possible to use these methods when they can't be used. In this case the result of them doesn't make sense. And it's probably better to raise exception instead of return incorrect values.

loadkpi avatar Dec 23 '21 19:12 loadkpi

Here are two examples:

When writing unit test or specs, you'll often call methods outside the context of a rack request. Without this change, you need to mock the helper methods or setup the env variables. It's a little tedious.

When the helpers are used in views and those the views are used in out-of-band jobs too. For example, you may have a footer partial that calls is_crawler?. If that footer is then used in an email template asynchronously, it will raise an error. The work around is then write your own helpers which call is_crawler? and then rescue the errors.

rgraff avatar Dec 23 '21 20:12 rgraff