browser icon indicating copy to clipboard operation
browser copied to clipboard

Add support for custom browsers

Open what-is-a-crow opened this issue 10 years ago • 2 comments

We use custom user agents for our native mobile clients that we rely on for API versioning and so have historically just monkey-patched Browser. Version 2.0 nearly supports this scenario with its tidy, class-by-class approach to each browser type; however, there is still no way to actually register a custom browser and so we're back to monkey-patching (albeit in a much less invasive manner).

Our custom browser looks like this:

module Browser
  class MyCustomBrowser < Base
    def id
      :my_custom_browser
    end

    def name
      "My Custom Browser"
    end

    def full_version
      ua[%r[CustomBrowser/([\S]+)], 1]
    end

    def match?
      ua =~ /CustomBrowser/
    end
  end
end

And the API looks like this:

Browser.register Browser::MyCustomBrowser

The Browser.register function just adds the class reference to an array of custom browsers, which are prepended to the hard-coded list that is currently in place.

I'm happy to open a pull request for these changes but I wanted to make sure that this makes sense first.

Cheers!

what-is-a-crow avatar Mar 03 '16 20:03 what-is-a-crow

Thanks for the suggestion. It was an easy change, so I just did it. I took the opportunity and did the same thing for platform, device and meta collections.

fnando avatar Mar 04 '16 03:03 fnando

This is fantastic. Thanks!

what-is-a-crow avatar Mar 04 '16 03:03 what-is-a-crow