### Install HowLongToBeat Gem Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Add the gem to your application's Gemfile and run bundle install, or install it directly using gem install. ```ruby gem 'howlongtobeat' ``` ```bash $ bundle install ``` ```bash $ gem install howlongtobeat ``` -------------------------------- ### Install the howlongtobeat Gem Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Add the gem to your Gemfile and run bundle install, or install it directly using the gem command. ```ruby # Gemfile gem 'howlongtobeat' # Or install directly # gem install howlongtobeat ``` -------------------------------- ### Initialize the HowLongToBeat Client Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Create a new client instance with optional similarity threshold and auto-filter settings. The default similarity threshold is 0.4. ```ruby require 'howlongtobeat' # Default client: filters out results with similarity < 0.4 hltb = HowLongToBeat::HowLongToBeat.new # No similarity filtering — return every result the API sends back hltb_no_filter = HowLongToBeat::HowLongToBeat.new(0.0) # Strict matching — only results with similarity >= 0.7 hltb_strict = HowLongToBeat::HowLongToBeat.new(0.7) # Auto-filter times: nil out times that don't apply to the game's complexity type hltb_auto = HowLongToBeat::HowLongToBeat.new(0.4, true) ``` -------------------------------- ### Initialize the client Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Creates a new client instance for the HowLongToBeat API. You can specify a minimum similarity threshold and whether to automatically filter irrelevant time fields. ```APIDOC ## HowLongToBeat::HowLongToBeat.new ### Description Creates a new client instance. Accepts an optional minimum similarity threshold (default `0.4`, range `0.0–1.0`) and an optional `auto_filter_times` flag that suppresses irrelevant time fields based on complexity flags. ### Method `HowLongToBeat::HowLongToBeat.new(minimum_similarity_threshold = 0.4, auto_filter_times = false)` ### Parameters - **minimum_similarity_threshold** (Float) - Optional - The minimum similarity score (0.0-1.0) for search results. Defaults to 0.4. - **auto_filter_times** (Boolean) - Optional - If true, automatically filters out time fields that do not apply to the game's complexity type. Defaults to false. ### Request Example ```ruby require 'howlongtobeat' # Default client: filters out results with similarity < 0.4 hltb = HowLongToBeat::HowLongToBeat.new # No similarity filtering — return every result the API sends back hltb_no_filter = HowLongToBeat::HowLongToBeat.new(0.0) # Strict matching — only results with similarity >= 0.7 hltb_strict = HowLongToBeat::HowLongToBeat.new(0.7) # Auto-filter times: nil out times that don't apply to the game's complexity type hltb_auto = HowLongToBeat::HowLongToBeat.new(0.4, true) ``` ``` -------------------------------- ### Initialize API with Custom Similarity Threshold Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Create a new API instance with a custom similarity threshold to filter search results. A threshold of 0.0 returns all results, while a higher value enforces stricter matching. ```ruby # Return all results without filtering hltb = HowLongToBeat::HowLongToBeat.new(0.0) results = hltb.search("The Witcher 3") # Use a higher threshold for stricter matching hltb = HowLongToBeat::HowLongToBeat.new(0.7) results = hltb.search("The Witcher 3") ``` -------------------------------- ### Understanding Game Time Data Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt This snippet shows how to access game time data, demonstrating that solo times are nilled when complexity_lvl_sp is false. It illustrates accessing main story, multiplayer time, and complexity levels. ```ruby puts clean&.main_story # => nil (pure multiplayer game) puts clean&.mp_time # => Float (only meaningful times remain) puts clean&.complexity_lvl_sp # => false puts clean&.complexity_lvl_mp # => true ``` -------------------------------- ### Basic Search Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Searches for games by name and returns an array of matching games or nil if no results are found. Each result is an HowLongToBeatEntry object. ```APIDOC ## Basic Search ### Description Searches for games by name and returns an array of matching games or nil if no results are found. Each result is an HowLongToBeatEntry object. ### Method `search(game_name, search_modifiers = HowLongToBeat::HTMLRequests::SearchModifiers::NONE)` ### Parameters #### Path Parameters None #### Query Parameters - **game_name** (String) - Required - The name of the game to search for. - **search_modifiers** (Enum) - Optional - Modifiers to filter search results. Defaults to `NONE`. ### Request Example ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new results = hltb.search("The Witcher 3") ``` ### Response #### Success Response - Returns an Array of `HowLongToBeatEntry` objects or `nil` if no results are found. #### Response Example ```ruby # Example of an HowLongToBeatEntry object structure: { "game_id": 10270, "game_name": "The Witcher 3: Wild Hunt", "game_alias": "The Witcher 3", "game_type": "game", "game_image_url": "https://howlongtobeat.com/images/games/witcher_3_wild_hunt.jpg", "game_web_link": "https://howlongtobeat.com/game/10270", "review_score": 90, "profile_dev": "CD Projekt Red", "profile_platforms": ["PC", "PS4", "Xbox One", "Switch", "PS5", "Xbox Series X/S"], "release_world": 2015, "main_story": 51.5, "main_extra": 80.0, "completionist": 175.0, "all_styles": 80.0, "coop_time": null, "mp_time": null, "complexity_lvl_combine": true, "complexity_lvl_sp": true, "complexity_lvl_co": false, "complexity_lvl_mp": false, "similarity": 0.95, "json_content": { ... } } ``` ``` -------------------------------- ### Basic Game Search Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Initialize the API and search for a game by its title. The search method returns an array of possible games or nil if no results are found. ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new results = hltb.search("The Witcher 3") ``` -------------------------------- ### Access Game Details with HowLongToBeatEntry Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt The `HowLongToBeatEntry` object provides detailed information about a game, including its ID, name, type, image URL, web link, review score, developer, platforms, release year, and various completion times. It also includes complexity flags and matching metadata. ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new entry = hltb.search("Hollow Knight")&.first # --- Identity --- entry.game_id # => Integer (HowLongToBeat internal ID) entry.game_name # => String e.g. "Hollow Knight" entry.game_alias # => String | nil entry.game_type # => String e.g. "game", "dlc" entry.game_image_url # => String full URL to cover art entry.game_web_link # => String full URL to HLTB page entry.review_score # => Integer 0–100 entry.profile_dev # => String developer name entry.profile_platforms # => Array e.g. ["PC", "Nintendo Switch"] entry.release_world # => Integer release year # --- Completion times (hours, Float or nil) --- entry.main_story # => Float e.g. 26.5 entry.main_extra # => Float e.g. 40.5 entry.completionist # => Float e.g. 56.0 entry.all_styles # => Float e.g. 38.0 entry.coop_time # => Float | nil entry.mp_time # => Float | nil # --- Complexity flags --- entry.complexity_lvl_combine # => Boolean entry.complexity_lvl_sp # => Boolean true when solo times are meaningful entry.complexity_lvl_co # => Boolean true when co-op time is meaningful entry.complexity_lvl_mp # => Boolean true when mp time is meaningful # --- Matching metadata --- entry.similarity # => Float 0.0–1.0 how closely the name matched the query entry.json_content # => Hash raw API payload for this game # --- String representation --- puts entry.to_s # => "Hollow Knight (ID: 38383) - Main Story: 26.5h, Main + Extra: 40.5h, Completionist: 56.0h, All Styles: 38.0h" # Build a formatted completion summary def format_entry(entry) lines = ["Game: #{entry.game_name} (#{entry.release_world})"] lines << " Developer : #{entry.profile_dev}" lines << " Platforms : #{entry.profile_platforms&.join(', ')}" lines << " Score : #{entry.review_score}/100" lines << " Main Story: #{entry.main_story}h" if entry.main_story lines << " +Extras : #{entry.main_extra}h" if entry.main_extra lines << " 100% : #{entry.completionist}h" if entry.completionist lines << " All Styles: #{entry.all_styles}h" if entry.all_styles lines << " Co-op : #{entry.coop_time}h" if entry.coop_time lines << " Multi : #{entry.mp_time}h" if entry.mp_time lines.join("\n") end puts format_entry(entry) ``` -------------------------------- ### Controlling Search Precision with Similarity Threshold Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Demonstrates how to control the precision of search results by setting a similarity threshold when initializing the HowLongToBeat client. A threshold of 0.0 performs a broad search, while higher values (e.g., 0.8) enforce stricter name matching. The default threshold is 0.4. ```ruby require 'howlongtobeat' # Broad search: all results regardless of name match broad = HowLongToBeat::HowLongToBeat.new(0.0) results = broad.search("Zelda") puts results.map { |r| "#{r.game_name} (#{r.similarity.round(2)})" } # => many results including loosely related titles # Default: similarity >= 0.4 default = HowLongToBeat::HowLongToBeat.new # same as new(0.4) results = default.search("Zelda") # Strict: only near-exact name matches strict = HowLongToBeat::HowLongToBeat.new(0.8) results = strict.search("The Legend of Zelda: Breath of the Wild") results&.each do |r| puts "#{r.game_name}: #{r.similarity}" # Only entries with similarity >= 0.8 are present end # Compare counts puts broad.search("Witcher")&.length # e.g. 15 puts default.search("Witcher")&.length # e.g. 8 puts strict.search("Witcher")&.length # e.g. 2 ``` -------------------------------- ### Search for Games by Title Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Searches HowLongToBeat.com by game name and returns an array of HowLongToBeatEntry objects. Handles network or input failures by returning nil. Supports case-insensitive similarity matching. ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new # Basic search results = hltb.search("The Witcher 3") if results.nil? puts "Request failed or no network" elsif results.empty? puts "No matching games found" else results.each do |entry| puts entry.to_s # => "The Witcher 3: Wild Hunt (ID: 10270) - Main Story: 51.5h, Main + Extra: 103.0h, Completionist: 173.0h, All Styles: 91.0h" end # Access individual fields on the best match best = results.first puts best.game_name # => "The Witcher 3: Wild Hunt" puts best.game_id # => 10270 puts best.game_type # => "game" puts best.release_world # => 2015 puts best.review_score # => 91 puts best.profile_dev # => "CD Projekt RED" puts best.profile_platforms # => ["PC", "PlayStation 4", "Xbox One", ...] puts best.game_image_url # => "https://howlongtobeat.com/games/10270_The_Witcher_3_Wild_Hunt.jpg" puts best.game_web_link # => "https://howlongtobeat.com/game/10270" puts best.similarity # => 0.93 (Float 0.0–1.0) puts best.main_story # => 51.5 (hours, Float) puts best.main_extra # => 103.0 puts best.completionist # => 173.0 puts best.all_styles # => 91.0 puts best.coop_time # => nil (no co-op data) puts best.mp_time # => nil puts best.json_content # => raw Hash from the API end # Case-insensitive similarity matching results = hltb.search("the witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::NONE, false) ``` -------------------------------- ### Search by title Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Searches for games on HowLongToBeat.com using a game title. Returns an array of `HowLongToBeatEntry` objects ordered by relevance, or `nil` if the request fails. ```APIDOC ## HowLongToBeat#search ### Description Searches HowLongToBeat.com by game name and returns an array of `HowLongToBeatEntry` objects ordered by relevance, or `nil` on network/input failure. Accepts an optional `SearchModifiers` constant and a `similarity_case_sensitive` boolean (default `true`). ### Method `search(title, search_modifiers = HowLongToBeat::HTMLRequests::SearchModifiers::NONE, similarity_case_sensitive = true)` ### Parameters - **title** (String) - Required - The title of the game to search for. - **search_modifiers** (HowLongToBeat::HTMLRequests::SearchModifiers) - Optional - Modifiers to refine the search (e.g., filtering by game type). Defaults to `HowLongToBeat::HTMLRequests::SearchModifiers::NONE`. - **similarity_case_sensitive** (Boolean) - Optional - Whether the similarity matching should be case-sensitive. Defaults to `true`. ### Response - **HowLongToBeatEntry** (Array) - An array of `HowLongToBeatEntry` objects representing the search results, ordered by relevance. Each object contains game metadata, completion times, and other details. - **nil** - Returned if the request fails due to network issues or invalid input. ### Request Example ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new # Basic search results = hltb.search("The Witcher 3") if results.nil? puts "Request failed or no network" elsif results.empty? puts "No matching games found" else results.each do |entry| puts entry.to_s # => "The Witcher 3: Wild Hunt (ID: 10270) - Main Story: 51.5h, Main + Extra: 103.0h, Completionist: 173.0h, All Styles: 91.0h" end # Access individual fields on the best match best = results.first puts best.game_name # => "The Witcher 3: Wild Hunt" puts best.game_id # => 10270 puts best.game_type # => "game" puts best.release_world # => 2015 puts best.review_score # => 91 puts best.profile_dev # => "CD Projekt RED" puts best.profile_platforms # => ["PC", "PlayStation 4", "Xbox One", ...] puts best.game_image_url # => "https://howlongtobeat.com/games/10270_The_Witcher_3_Wild_Hunt.jpg" puts best.game_web_link # => "https://howlongtobeat.com/game/10270" puts best.similarity # => 0.93 (Float 0.0–1.0) puts best.main_story # => 51.5 (hours, Float) puts best.main_extra # => 103.0 puts best.completionist # => 173.0 puts best.all_styles # => 91.0 puts best.coop_time # => nil (no co-op data) puts best.mp_time # => nil puts best.json_content # => raw Hash from the API end # Case-insensitive similarity matching results = hltb.search("the witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::NONE, false) ``` ``` -------------------------------- ### Search by ID Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Searches for a game using its HowLongToBeat ID and returns a single HowLongToBeatEntry object or nil if not found. ```APIDOC ## Search by ID ### Description Searches for a game using its HowLongToBeat ID and returns a single HowLongToBeatEntry object or `nil` if not found. ### Method `search_from_id(game_id)` ### Parameters #### Path Parameters None #### Query Parameters - **game_id** (Integer) - Required - The unique ID of the game on HowLongToBeat. ### Request Example ```ruby result = hltb.search_from_id(10270) # The Witcher 3: Wild Hunt ``` ### Response #### Success Response - Returns a single `HowLongToBeatEntry` object or `nil` if the game ID is not found. #### Response Example ```ruby # Example of an HowLongToBeatEntry object structure (same as Basic Search response) { "game_id": 10270, "game_name": "The Witcher 3: Wild Hunt", "game_alias": "The Witcher 3", "game_type": "game", "game_image_url": "https://howlongtobeat.com/images/games/witcher_3_wild_hunt.jpg", "game_web_link": "https://howlongtobeat.com/game/10270", "review_score": 90, "profile_dev": "CD Projekt Red", "profile_platforms": ["PC", "PS4", "Xbox One", "Switch", "PS5", "Xbox Series X/S"], "release_world": 2015, "main_story": 51.5, "main_extra": 80.0, "completionist": 175.0, "all_styles": 80.0, "coop_time": null, "mp_time": null, "complexity_lvl_combine": true, "complexity_lvl_sp": true, "complexity_lvl_co": false, "complexity_lvl_mp": false, "similarity": 1.0, "json_content": { ... } } ``` ``` -------------------------------- ### Search Game by ID Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Retrieve a specific game's details using its unique HowLongToBeat ID. This method returns a single game entry or nil if not found. ```ruby result = hltb.search_from_id(10270) # The Witcher 3: Wild Hunt ``` -------------------------------- ### Search by game ID Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Retrieves detailed information for a specific game using its unique HowLongToBeat ID. Returns a single `HowLongToBeatEntry` object or `nil` if the game is not found or the request fails. ```APIDOC ## HowLongToBeat#search_from_id ### Description Looks up a single game by its HowLongToBeat numeric ID. Internally fetches the game's title from its page, runs a search, and filters the results to the exact ID match. Returns a single `HowLongToBeatEntry` or `nil`. ### Method `search_from_id(game_id)` ### Parameters - **game_id** (Integer) - Required - The numeric ID of the game on HowLongToBeat.com. ### Response - **HowLongToBeatEntry** (Object) - A `HowLongToBeatEntry` object containing detailed information about the specified game. - **nil** - Returned if the game ID is invalid, not found, or if the request fails. ### Request Example ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new result = hltb.search_from_id(10270) # The Witcher 3: Wild Hunt if result puts result.game_name # => "The Witcher 3: Wild Hunt" puts result.game_id # => 10270 puts result.main_story # => 51.5 puts result.completionist # => 173.0 puts result.to_s # => "The Witcher 3: Wild Hunt (ID: 10270) - Main Story: 51.5h, Main + Extra: 103.0h, Completionist: 173.0h, All Styles: 91.0h" else puts "Game not found or request failed" end # Edge cases — both return nil hltb.search_from_id(nil) # => nil hltb.search_from_id(0) # => nil hltb.search_from_id(999999999) # => nil ``` ``` -------------------------------- ### Search for Games by ID Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Looks up a single game by its HowLongToBeat numeric ID. Returns a single HowLongToBeatEntry or nil if the game is not found or the request fails. Handles edge cases like nil, 0, or invalid IDs by returning nil. ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new result = hltb.search_from_id(10270) # The Witcher 3: Wild Hunt if result puts result.game_name # => "The Witcher 3: Wild Hunt" puts result.game_id # => 10270 puts result.main_story # => 51.5 puts result.completionist # => 173.0 puts result.to_s # => "The Witcher 3: Wild Hunt (ID: 10270) - Main Story: 51.5h, Main + Extra: 103.0h, Completionist: 173.0h, All Styles: 91.0h" else puts "Game not found or request failed" end # Edge cases — both return nil hltb.search_from_id(nil) # => nil hltb.search_from_id(0) # => nil hltb.search_from_id(999999999) # => nil ``` -------------------------------- ### Automatically Filter Irrelevant Completion Times Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Initialize the `HowLongToBeat` client with `auto_filter_times: true` to automatically set irrelevant time fields to `nil` based on a game's complexity profile. This prevents misleading data, such as solo times for multiplayer-only games. ```ruby require 'howlongtobeat' # Without auto_filter_times (default) hltb_raw = HowLongToBeat::HowLongToBeat.new(0.4, false) raw = hltb_raw.search("Rocket League")&.first puts raw&.main_story # may return a misleading value # With auto_filter_times hltb_clean = HowLongToBeat::HowLongToBeat.new(0.4, true) clean = hltb_clean.search("Rocket League")&.first ``` -------------------------------- ### Search with DLC Exclusion Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Perform a search while excluding DLCs by using the HIDE_DLC modifier. Other available modifiers include ISOLATE_DLC, ISOLATE_MODS, and ISOLATE_HACKS. ```ruby hltb = HowLongToBeat::HowLongToBeat.new results = hltb.search("The Witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::HIDE_DLC) ``` -------------------------------- ### Similarity Filtering Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Adjusts the threshold for filtering search results based on name similarity. ```APIDOC ## Similarity Filtering ### Description By default, the search filters results with a similarity score greater than 0.4. This threshold can be adjusted when initializing the `HowLongToBeat` object. ### Usage Example ```ruby require 'howlongtobeat' # Return all results without filtering (similarity threshold of 0.0) hltb_all = HowLongToBeat::HowLongToBeat.new(0.0) results_all = hltb_all.search("The Witcher 3") # Use a higher threshold for stricter matching (e.g., 0.7) hltb_strict = HowLongToBeat::HowLongToBeat.new(0.7) results_strict = hltb_strict.search("The Witcher 3") ``` ### Parameters When initializing `HowLongToBeat`: - **similarity_threshold** (Float) - Optional - The minimum similarity score (0.0 to 1.0) required for a search result to be included. Defaults to 0.4. ``` -------------------------------- ### Filter Search Results with SearchModifiers Source: https://context7.com/dpashutskii/howlongtobeat/llms.txt Use constants from `HowLongToBeat::HTMLRequests::SearchModifiers` to control the types of content returned in search results. These modifiers can include all content, exclude DLCs, isolate DLCs, mods, or ROM hacks. ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new # NONE (default) — return all content including DLCs results = hltb.search("The Witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::NONE) # HIDE_DLC — games only, no DLC entries games_only = hltb.search("The Witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::HIDE_DLC) # ISOLATE_DLC — only DLC entries dlcs = hltb.search("The Witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_DLC) dlcs&.each { |d| puts "#{d.game_name} (#{d.game_type})" } # ISOLATE_MODS — only mods mods = hltb.search("Skyrim", HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_MODS) # ISOLATE_HACKS — only ROM hacks hacks = hltb.search("Zelda", HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_HACKS) # Inspect the modifier string values puts HowLongToBeat::HTMLRequests::SearchModifiers::NONE # => "" puts HowLongToBeat::HTMLRequests::SearchModifiers::HIDE_DLC # => "hide_dlc" puts HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_DLC # => "only_dlc" puts HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_MODS # => "only_mods" puts HowLongToBeat::HTMLRequests::SearchModifiers::ISOLATE_HACKS # => "only_hacks" ``` -------------------------------- ### Search Modifiers Source: https://github.com/dpashutskii/howlongtobeat/blob/main/README.md Allows filtering search results using predefined modifiers. ```APIDOC ## Search Modifiers ### Description Allows filtering search results using predefined modifiers. These can be passed as the second argument to the `search` method. ### Available Modifiers - **NONE**: Default search (includes DLCs). - **ISOLATE_DLC**: Show only DLCs. - **ISOLATE_MODS**: Show only mods. - **ISOLATE_HACKS**: Show only hacks. - **HIDE_DLC**: Hide DLCs and show only games. ### Usage Example ```ruby require 'howlongtobeat' hltb = HowLongToBeat::HowLongToBeat.new results = hltb.search("The Witcher 3", HowLongToBeat::HTMLRequests::SearchModifiers::HIDE_DLC) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.