Git Product home page Git Product logo

device_detector's Introduction

Device Detector

Build Status

The library for parsing User Agent and browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), vendor and model detection.

  • Support latest Crystal version and update script for private use or immediately updates.
  • Currently it is production version and works fine more that 2 years.
  • The Library uses regexes from matomo-org/device-detector.

Installation

Add this to your application's shard.yml:

dependencies:
  device_detector:
    github: creadone/device_detector

Then run shards install

Usage

require "device_detector"

user_agent = "Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 Edge/12.0"
response = DeviceDetector::Detector.new(user_agent).call  # All parsers
response = DeviceDetector::Detector.new(user_agent).lite  # Only for bot and mobile

# Check if browser detected
response.browser? #=> true

# browser name
response.browser_name #=> Microsoft Edge

# browser version
response.browser_version #=> 12.0

# get raw response with
pp response.raw

[{
    "bot" => {
      "name" => ""
    }
  },
  {
    "browser" => {
      "name" => "", "version" => ""
    }
  },

  {...},

  {
    "vendorfragment" => {
      "vendor" => ""
    }
  }
]

Available methods:

bot?
bot_name
browser_engine?
browser_engine_name
browser?
browser_name
browser_version
camera?
camera_vendor
camera_model
car_browser?
car_browser_vendor
car_browser_model
console?
console_vendor
console_model
feed_reader?
feed_reader_name
feed_reader_version
library?
library_name
library_version
mediaplayer?
mediaplayer_name
mediaplayer_version
mobile_app?
mobile_app_name
mobile_app_version
mobile_device?
mobile_device_vendor
mobile_device_type
mobile_device_model
os?
os_name
os_version
pim?
pim_name
pim_version
portable_media_player?
portable_media_player_vendor
portable_media_player_model
tv?
tv_vendor
tv_model
vendorfragment?
vendorfragment_vendor

Benchmarks

Recent benchmarking of parsing 1000 user-agent strings on a MacBook Air with Intel Core i5 dual core (0.8 Ghz per core):

Crystal 0.30.1 (2019-08-13) LLVM: 8.0.1 Default target: x86_64-apple-macosx

bench/raw_response.cr --release
            user     system      total        real
full:   5.880000   0.060000   5.940000 (  5.940340)
lite:   3.880000   0.040000   3.920000 (  3.953958)

It's mean that device_detector can work with 1000 / 5.9 ~ 169 QPS.

Testing

crystal spec

Update regexes

crystal scripts/update_regexes.cr

ToDo

  • Support overloading of base rules
  • CLI & HTTP version
  • More lighter and faster the lite version
  • Reload regexes on the fly (may be)

Contributing

  1. Fork it ( https://github.com/creadone/device_detector/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • @creadone Sergey Fedorov - creator, maintainer
  • @delef Ivan Palamarchuk - new api, code optimization
  • @zaycker Yuriy Zaitsev - fix check order

device_detector's People

Contributors

creadone avatar delef avatar zaycker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

device_detector's Issues

Logo Design

Hey, I am graphic designer and I can design a logo for Device Detector. What do you think about it?

I am waiting your comment.

Have a nice day!

Best Regards
Baran Pirinçal

Add device precheck before model checks

There is a false positive triggered television "Smart" for general test with Edge browser. It happens cause of device regexp is not always included into model regexp.

# Smart
Smart:
  regex: 'Smart[^a-z]'
  device: 'tv'
  models:
    - regex: 'Smart; ([^);/]+)'
      model: '$1'
    - regex: '([A-Z]{2}[0-9]{2}|ZAPPIX)'
      model: '$1'

Look on second model's regexp. There is a huge probability to find UA with two chars and two digits. It is necessary to make a precheck by device regexp for MultiModel yaml records I guess. Also such statements for camera, console, mobile, portable_media_player and television reduced crystal spec time from 210 ms to 130 ms cause it prevents excess iterations by model's regexps.

@creadone

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.