### Download UAInspector Databases Source: https://github.com/elixir-inspector/ua_inspector/blob/master/README.md This command initiates the download of necessary database files for UAInspector. It can be run from the command line using Mix. Alternatively, `UAInspector.Downloader.download/0` can be called programmatically within your Elixir application. This step is crucial before parsing can occur effectively. ```bash mix ua_inspector.download ``` -------------------------------- ### Configure UAInspector Startup Behavior Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This configuration section details how to control database loading during application startup. Options include asynchronous loading (`startup_sync: false`), silent startup (`startup_silent: true`), and manual supervision by excluding UAInspector from automatic startup. ```elixir # config/config.exs # Asynchronous database loading (non-blocking startup) config :ua_inspector, startup_sync: false # Silent startup (suppress informational messages) config :ua_inspector, startup_silent: true # Combined configuration config :ua_inspector, startup_sync: false, startup_silent: true # Manual supervision (no automatic startup) def application do [ included_applications: [:ua_inspector] ] end # Add to your supervision tree def start(_type, _args) do children = [ UAInspector.Supervisor, MyApp.Web.Endpoint ] Supervisor.start_link(children, strategy: :one_for_one) end ``` -------------------------------- ### Download UAInspector Database Files Programmatically and via CLI Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This section covers downloading and updating UAInspector's device detector database files. Downloads can be performed programmatically using `UAInspector.Downloader.download/0` or `download/1` for specific components, or via the command line using `mix ua_inspector.download` with options like `--force` and `--quiet`. ```elixir # Download programmatically UAInspector.Downloader.download() # Downloads all database files # :ok # Download specific components UAInspector.Downloader.download(:databases) # Device/browser/OS databases UAInspector.Downloader.download(:client_hints) # Client hints data UAInspector.Downloader.download(:short_code_maps) # Short code mappings # Check if databases are ready UAInspector.ready?() # true if all databases loaded ``` ```bash # Download from command line mix ua_inspector.download # Force download without confirmation mix ua_inspector.download --force # Silent download mix ua_inspector.download --force --quiet ``` -------------------------------- ### Configure UAInspector Database Path and Remote Sources Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This snippet illustrates how to configure the database path, remote release version, and HTTP options for UAInspector. It also shows dynamic configuration via an initialization module and custom remote paths for different database components. ```elixir # config/config.exs config :ua_inspector, database_path: "/path/to/custom/databases", remote_release: "6.4.7", http_opts: [recv_timeout: 10_000] # Dynamic configuration with initialization config :ua_inspector, init: {MyApp.UAInspectorInit, :setup} # MyApp.UAInspectorInit module defmodule MyApp.UAInspectorInit do def setup do priv_dir = Application.app_dir(:my_app, "priv/ua_databases") Application.put_env(:ua_inspector, :database_path, priv_dir) :ok end end # Custom remote paths config :ua_inspector, remote_path: [ bot: "https://example.com/databases", browser_engine: "https://example.com/databases/client", client: "https://example.com/databases/client", client_hints: "https://example.com/databases/client/hints", device: "https://example.com/databases/device", os: "https://example.com/databases", short_code_map: "https://example.com", vendor_fragment: "https://example.com/databases" ] ``` -------------------------------- ### Safe User Agent Parsing with Elixir Pattern Matching Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt Demonstrates safe extraction of user agent information using Elixir's pattern matching on the result of UAInspector.parse. Handles different outcomes like known browsers, unknown clients, and bots. Ensures graceful handling of various user agent string formats. ```Elixir case UAInspector.parse(user_agent) do %{client: %{name: name}} when is_binary(name) -> "Browser: #{name}" %{client: :unknown} -> "Unknown browser" %UAInspector.Result.Bot{name: bot_name} -> "Bot: #{bot_name}" end ``` -------------------------------- ### Parse User Agent with Client Hints using UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt Parses user agents that are enhanced with modern client hints headers for improved detection accuracy. This method allows for more precise identification of browsers, devices, and operating systems by utilizing additional information provided in HTTP headers. ```elixir # Create client hints from HTTP headers client_hints = UAInspector.ClientHints.new([ {"sec-ch-ua", ~S(" Not A;Brand";v="99", "Chromium";v="95", "Microsoft Edge";v="95")}, {"sec-ch-ua-mobile", "?0"}, {"sec-ch-ua-platform", "Windows"}, {"sec-ch-ua-platform-version", "14.0.0"} ]) # Parse with enhanced client hints result = UAInspector.parse( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44", client_hints ) %UAInspector.Result{ browser_family: "Internet Explorer", client: %UAInspector.Result.Client{ engine: "Blink", engine_version: "95.0.4638.69", name: "Microsoft Edge", type: "browser", version: "95.0.1020.44" }, device: %UAInspector.Result.Device{ brand: :unknown, model: :unknown, type: "desktop" }, os: %UAInspector.Result.OS{ name: "Windows", platform: "x64", version: "10" }, os_family: "Windows", user_agent: "..." } # Detect mobile apps via x-requested-with header app_hints = UAInspector.ClientHints.new([{"x-requested-with", "org.telegram.messenger"}]) app_result = UAInspector.parse( "Mozilla/5.0 (Linux; Android 11; Pixel 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36", app_hints ) app_result.client.name # "Telegram" app_result.client.type # "mobile app" ``` -------------------------------- ### Add UAInspector Dependency to Mix Project Source: https://github.com/elixir-inspector/ua_inspector/blob/master/README.md This snippet shows how to add the UAInspector library as a dependency in your Elixir project's `mix.exs` file. Ensure you are using a compatible version. This is a standard way to manage project dependencies in Elixir. ```elixir defp deps do [ # ... {:ua_inspector, ">~= 3.0"}, # ... ] end ``` -------------------------------- ### Reload UAInspector Databases Asynchronously and Synchronously Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This snippet explains how to reload UAInspector's database contents without restarting the application. It covers asynchronous reloading (default) and synchronous reloading, along with checking database readiness after a reload and a typical reload pattern within a Phoenix controller. ```elixir # Asynchronous reload (default, non-blocking) UAInspector.reload() UAInspector.reload(async: true) # Synchronous reload (blocks until complete) UAInspector.reload(async: false) # Check readiness after reload if UAInspector.ready?() do # Databases are loaded and ready result = UAInspector.parse(user_agent) end # Typical reload pattern in a Phoenix controller def reload_databases(conn, _params) do Task.start(fn -> UAInspector.reload() end) json(conn, %{status: "reload_started"}) end ``` -------------------------------- ### Parse User Agents Without Bot Detection using UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This code demonstrates how to parse user agent strings while skipping bot detection for performance. It contrasts the standard `UAInspector.parse/1` with `UAInspector.parse_client/1`, which only performs client-side detection and always returns a `UAInspector.Result` object, not a `UAInspector.Result.Bot`. ```elixir # Standard parse checks bots first standard = UAInspector.parse(user_agent) # Skip bot detection client_only = UAInspector.parse_client(user_agent) # Always returns UAInspector.Result (never UAInspector.Result.Bot) %UAInspector.Result{ browser_family: "Chrome", client: %UAInspector.Result.Client{...}, device: %UAInspector.Result.Device{...}, os: %UAInspector.Result.OS{...}, os_family: "Android", user_agent: "..." } # With client hints client_only_with_hints = UAInspector.parse_client(user_agent, client_hints) ``` -------------------------------- ### Parse User Agent String with UAInspector Source: https://github.com/elixir-inspector/ua_inspector/blob/master/README.md Demonstrates how to use the `UAInspector.parse/1` function to parse a user agent string. The function returns a structured result object, which can be either a `UAInspector.Result` for browsers or a `UAInspector.Result.Bot` for bots. This is the core functionality for extracting user agent details. ```elixir iex> UAInspector.parse("Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53") %UAInspector.Result{ client: %UAInspector.Result.Client{ engine: "WebKit", engine_version: "537.51.1", name: "Mobile Safari", type: "browser", version: "7.0" }, device: %UAInspector.Result.Device{ brand: "Apple", model: "iPad", type: "tablet" }, os: %UAInspector.Result.OS{ name: "iOS", platform: :unknown, version: "7.0.4" }, user_agent: "Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53" } iex> UAInspector.parse("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36") %UAInspector.Result.Bot{ category: "Search bot", name: "Googlebot", producer: %UAInspector.Result.BotProducer{ name: "Google Inc.", url: "http://www.google.com" }, url: "http://www.google.com/bot.html", user_agent: "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36" } ``` -------------------------------- ### Device Type Detection with UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt Provides convenience functions to query specific device types like mobile, desktop, or bots. These functions can be used with both raw user agent strings and parsed result objects. ```elixir # Check device types user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0..." UAInspector.mobile?(user_agent) # true UAInspector.desktop?(user_agent) # false UAInspector.bot?(user_agent) # false # Works with parsed results too result = UAInspector.parse(user_agent) UAInspector.mobile?(result) # true # Check for HbbTV (Hybrid Broadcast Broadband TV) hbbtv_ua = "Opera/9.80 (Linux armv7l; HbbTV/1.2.1..." UAInspector.hbbtv?(hbbtv_ua) # Returns version string or false ``` -------------------------------- ### Handle Unknown or Empty User Agents with UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This snippet illustrates how UAInspector handles undetectable or invalid user agent strings. When an unknown user agent is parsed, it returns an empty `UAInspector.Result`. Nil or empty strings also result in a `UAInspector.Result` with the corresponding `user_agent` field set to nil or an empty string. ```elixir # Unknown user agent returns empty result unknown = UAInspector.parse("--- undetectable ---") %UAInspector.Result{ browser_family: :unknown, client: :unknown, device: :unknown, os: :unknown, os_family: :unknown, user_agent: "--- undetectable ---" } # Nil or empty user agents UAInspector.parse(nil) # %UAInspector.Result{user_agent: nil} UAInspector.parse("") # %UAInspector.Result{user_agent: ""} ``` -------------------------------- ### Parse User Agent String with UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt Parses a standard user agent string to extract browser, device, and OS information. It returns a structured result object that can be used to access specific fields like browser family, device brand, and OS name. ```elixir # Parse an iPad Safari user agent result = UAInspector.parse("Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53") # Result structure %UAInspector.Result{ browser_family: "Safari", client: %UAInspector.Result.Client{ engine: "WebKit", engine_version: "537.51.1", name: "Mobile Safari", type: "browser", version: "7.0" }, device: %UAInspector.Result.Device{ brand: "Apple", model: "iPad", type: "tablet" }, os: %UAInspector.Result.OS{ name: "iOS", platform: :unknown, version: "7.0.4" }, os_family: "iOS", user_agent: "Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53" } # Access specific fields result.device.brand # "Apple" result.client.name # "Mobile Safari" result.os.name # "iOS" ``` -------------------------------- ### Detect Bots with UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt Identifies and classifies web crawlers and bots, providing detailed producer information. It also includes a convenience function to check if a user agent string belongs to a bot. ```elixir # Parse a Googlebot user agent bot_result = UAInspector.parse("Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Safari/537.36") %UAInspector.Result.Bot{ category: "Search bot", name: "Googlebot", producer: %UAInspector.Result.BotProducer{ name: "Google Inc.", url: "http://www.google.com" }, url: "http://www.google.com/bot.html", user_agent: "Mozilla/5.0 AppleWebKit/537.36..." } # Check if a user agent is a bot UAInspector.bot?("Mozilla/5.0... Googlebot/2.1...") # true UAInspector.bot?("Mozilla/5.0 (iPad; CPU OS 7_0_4...") # false # Generic bot detection for unknown crawlers generic_bot = UAInspector.parse("generic crawler agent") %UAInspector.Result.Bot{ category: :unknown, name: "Generic Bot", producer: %UAInspector.Result.BotProducer{ name: :unknown, url: :unknown }, url: :unknown, user_agent: "generic crawler agent" } ``` -------------------------------- ### Check for ShellTV Devices with UAInspector Source: https://context7.com/elixir-inspector/ua_inspector/llms.txt This snippet demonstrates how to check if a given user agent string belongs to a ShellTV device using the `UAInspector.shelltv?` function. It takes a user agent string as input and returns a boolean. ```elixir shelltv_ua = "Mozilla/5.0 (Linux; Android 9; SHIELD..." UAInspector.shelltv?(shelltv_ua) # true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.