### Configuration Object Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Shows how to instantiate Algolia::Configuration with app_id, api_key, hosts, and timeouts. ```ruby # Configuration object pattern config = Algolia::Configuration.new( app_id: 'APP_ID', api_key: 'API_KEY', hosts: [...], read_timeout: 5, write_timeout: 30 ) ``` -------------------------------- ### Transport Class Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Shows the Transport class structure with initialization and request method. Demonstrates the retry strategy with host failover. ```ruby # lib/algolia/transport/ class Transport def initialize(config) @http_requester = HttpRequester.new(config.app_id, config.api_key) @retry_strategy = RetryStrategy.new(config.hosts) end def request(method, path, body, opts) # Retry with host failover end end ``` -------------------------------- ### Install Algolia Ruby gem Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/README.md Install the Algolia Ruby client via RubyGems using the gem install command. ```bash gem install algolia ``` -------------------------------- ### Block Syntax Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Shows two ways to pass a block to client.search: single-line with braces and multi-line with do..end. Use do..end for multi-line blocks. ```ruby # Use do..end for multi-line, braces for single-line client.search { |r| r.query = 'test' } client.search do |params| params.query = 'test' params.hits_per_page = 10 end ``` -------------------------------- ### Retry Strategy Class Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Outlines the RetryStrategy class with host states and retry behavior. Notes that 4xx errors are not retried. ```ruby # lib/algolia/transport/retry_strategy.rb class RetryStrategy # Host states: :up, :down, :timed_out # Retries on network errors # 4xx errors not retried end ``` -------------------------------- ### Build and Test Commands Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Commands for building the Ruby client, generating/running CTS tests, using the playground, formatting, installing dependencies, running tests, and linting from the repo root or client directory. ```bash # From repo root (api-clients-automation) yarn cli build clients ruby # Build Ruby client yarn cli cts generate ruby # Generate CTS tests yarn cli cts run ruby # Run CTS tests yarn cli playground ruby search # Interactive playground yarn cli format ruby clients/algoliasearch-client-ruby # From client directory cd clients/algoliasearch-client-ruby bundle install # Install dependencies bundle exec rake test # Run tests bundle exec rubocop # Run linter ``` -------------------------------- ### Nil Safety Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Shows safe navigation and explicit checks to avoid nil errors when accessing nested response fields. ```ruby # Use safe navigation result&.hits&.first&.object_id # Or explicit checks if result && result.hits && result.hits.any? result.hits.first.object_id end ``` -------------------------------- ### Symbol vs String Keys Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Demonstrates how to access response fields. API returns string keys, so use string access or model objects; symbol access may fail. ```ruby # API returns string keys, use with_indifferent_access or symbols response[:hits] # May not work response['hits'] # Works response.hits # Works if using model objects ``` -------------------------------- ### Error Handling Example Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Shows rescue clauses for Algolia::AlgoliaError (base) and Algolia::ApiError (API errors). Access status_code and message on the error object. ```ruby begin response = client.search(params) rescue Algolia::AlgoliaError => e # Base error class rescue Algolia::ApiError => e # API returned error puts e.status_code puts e.message end ``` -------------------------------- ### Create client and save object Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/README.md Create an Algolia search client with your application ID and API key, then save an object to an index. ```ruby client = Algolia::SearchClient.create('YourApplicationID', 'YourAPIKey') client.save_object('your_index_name', {objectID: 1, name: 'Foo'}) ``` -------------------------------- ### Check Generated Files in generation.config.mjs Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Inspect config/generation.config.mjs to identify generated patterns (without '!') vs hand-written ones (with '!'). Generated files must not be edited. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-ruby/lib/algolia/**', // Generated '!clients/algoliasearch-client-ruby/lib/algolia/transport/**', // Hand-written ✓ '!clients/algoliasearch-client-ruby/lib/algolia/error.rb', // Hand-written ✓ ``` -------------------------------- ### Check Git Remote Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/AGENTS.md Verify the current repository before making changes. The correct origin should be api-clients-automation; if it's algoliasearch-client-ruby, stop and route changes through the automation repo. ```bash git remote -v ``` -------------------------------- ### Search single index Source: https://github.com/algolia/algoliasearch-client-ruby/blob/main/README.md Perform a search on a single index using the search_single_index method. ```ruby objects = client.search_single_index('your_index_name', 'Foo') ```