Class: Strava::Web::Client

Inherits:
Object
  • Object
show all
Includes:
Connection, Request
Defined in:
lib/strava/web/client.rb

Overview

Base web client class for making HTTP requests to Strava APIs.

This class provides the foundation for all Strava client implementations (API, OAuth, and Webhooks clients). It handles HTTP connection management, request execution, and configuration management.

The client includes mixins for connection handling and HTTP request methods, and manages configuration attributes like endpoint URLs, user agents, and authentication tokens.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize a new client instance.

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options for the client

Options Hash (options):

  • :endpoint (String)

    The API endpoint URL

  • :user_agent (String)

    The user agent string

  • :token (String)

    The access token for authentication

  • :logger (Logger)

    Custom logger instance



34
35
36
37
38
39
# File 'lib/strava/web/client.rb', line 34

def initialize(options = {})
  Strava::Web::Config::ATTRIBUTES.each do |key|
    send("#{key}=", options[key] || Strava::Web.config.send(key))
  end
  @logger ||= Strava::Logger.logger
end

Class Method Details

.configModule

Returns the current configuration.

Returns:

  • (Module)

    The Config module



73
74
75
# File 'lib/strava/web/client.rb', line 73

def config
  Config
end

.configure {|Config| ... } ⇒ Module

Configure the client with a block or return the configuration.

Examples:

Configure the client

Strava::Web::Client.configure do |config|
  config.endpoint = 'https://www.strava.com/api/v3'
end

Yields:

  • (Config)

    Yields the configuration module for setup

Returns:

  • (Module)

    The Config module



64
65
66
# File 'lib/strava/web/client.rb', line 64

def configure
  block_given? ? yield(Config) : Config
end

Instance Method Details

#endpointString

Note:

This method must be implemented by subclasses

Returns the API endpoint URL for this client.

Returns:

  • (String)

    The API endpoint URL

Raises:

  • (NotImplementedError)

    if not implemented by subclass



48
49
50
# File 'lib/strava/web/client.rb', line 48

def endpoint
  raise NotImplementedError
end

#parse_args(id_or_options, options = {}) ⇒ Array<(String, Hash)>

Parse method arguments that can be either (id, options) or (options).

This helper method standardizes the common pattern where Strava API methods accept either a resource ID as the first parameter followed by an options hash, or just an options hash containing an :id key.

Examples:

With separate ID and options

parse_args('12345', {per_page: 10})
# => ['12345', {per_page: 10}]

With ID in options hash

parse_args({id: '12345', per_page: 10})
# => ['12345', {per_page: 10}]

Parameters:

  • id_or_options (String, Integer, Hash)

    Either a resource ID or options hash

  • options (Hash) (defaults to: {})

    Options hash (when first param is an ID)

Returns:

  • (Array<(String, Hash)>)

    Tuple of [id, options]

Raises:

  • (ArgumentError)

    if the :id is missing from the options hash



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/strava/web/client.rb', line 100

def parse_args(id_or_options, options = {})
  if id_or_options.is_a?(Hash)
    raise ArgumentError, 'Required argument :id missing' if id_or_options[:id].nil?

    [id_or_options[:id], id_or_options.except(:id)]
  else
    raise ArgumentError, 'Required argument :id missing' if id_or_options.nil?

    [id_or_options, options]
  end
end