Class: CardDB::Connection
- Inherits:
-
Object
- Object
- CardDB::Connection
- Defined in:
- lib/carddb/connection.rb
Overview
HTTP connection wrapper using Faraday. Handles authentication, request/response formatting, and error handling.
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
The configuration for this connection.
Instance Method Summary collapse
-
#execute(query, variables = {}) ⇒ Hash
Executes a GraphQL query.
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
15 16 17 |
# File 'lib/carddb/connection.rb', line 15 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns The configuration for this connection.
12 13 14 |
# File 'lib/carddb/connection.rb', line 12 def config @config end |
Instance Method Details
#execute(query, variables = {}) ⇒ Hash
Executes a GraphQL query.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/carddb/connection.rb', line 25 def execute(query, variables = {}) operation_name = extract_operation_name(query) retries = 0 begin start_time = Time.now log_request(operation_name, variables) response = connection.post do |req| req.body = JSON.generate({ query: query, variables: variables }) end duration_ms = ((Time.now - start_time) * 1000).round result = handle_response(response) log_response(operation_name, duration_ms) result rescue RateLimitError => e if config.retry_on_rate_limit && retries < config.max_retries retries += 1 sleep_time = e.retry_after || 60 log_rate_limit_retry(operation_name, retries, sleep_time) sleep(sleep_time) retry end raise rescue Faraday::TimeoutError => e log_error("Request timed out: #{e.}") raise TimeoutError, "Request timed out: #{e.}" rescue Faraday::ConnectionFailed => e log_error("Connection failed: #{e.}") raise ConnectionError, "Connection failed: #{e.}" rescue Faraday::Error => e log_error("HTTP error: #{e.}") raise ConnectionError, "HTTP error: #{e.}" end end |