Class: Schwab::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab/configuration.rb

Overview

Configuration storage for Schwab SDK

Examples:

Configure the SDK

Schwab.configure do |config|
  config.client_id = "your_client_id"
  config.client_secret = "your_client_secret"
  config.redirect_uri = "http://localhost:3000/callback"
  config.response_format = :hash # or :resource
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/schwab/configuration.rb', line 54

def initialize
  @api_base_url = "https://api.schwabapi.com"
  @api_version = "v1"
  @timeout = 30
  @open_timeout = 30
  @faraday_adapter = Faraday.default_adapter
  @max_retries = 3
  @retry_delay = 1
  @logger = nil
  @response_format = :hash
end

Instance Attribute Details

#api_base_urlString

Returns Base URL for Schwab API (default: api.schwabapi.com).

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#api_versionString

Returns API version to use (default: v1).

Returns:

  • (String)

    API version to use (default: v1)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#client_idString

Returns OAuth client ID from Schwab developer portal.

Returns:

  • (String)

    OAuth client ID from Schwab developer portal



40
41
42
# File 'lib/schwab/configuration.rb', line 40

def client_id
  @client_id
end

#client_secretString

Returns OAuth client secret from Schwab developer portal.

Returns:

  • (String)

    OAuth client secret from Schwab developer portal



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#faraday_adapterSymbol

Returns Faraday adapter to use (default: Faraday.default_adapter).

Returns:

  • (Symbol)

    Faraday adapter to use (default: Faraday.default_adapter)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#loggerLogger?

Returns Logger instance for debugging.

Returns:

  • (Logger, nil)

    Logger instance for debugging



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#max_retriesInteger

Returns Maximum number of retries for failed requests (default: 3).

Returns:

  • (Integer)

    Maximum number of retries for failed requests (default: 3)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#open_timeoutInteger

Returns Connection open timeout in seconds (default: 30).

Returns:

  • (Integer)

    Connection open timeout in seconds (default: 30)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#redirect_uriString

Returns OAuth callback URL configured in Schwab developer portal.

Returns:

  • (String)

    OAuth callback URL configured in Schwab developer portal



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#response_formatSymbol

Returns Response format (:hash or :resource, default: :hash)

  • :hash returns plain Ruby hashes (default, backward compatible)

  • :resource returns Sawyer::Resource-like objects with method access.

Returns:

  • (Symbol)

    Response format (:hash or :resource, default: :hash)

    • :hash returns plain Ruby hashes (default, backward compatible)

    • :resource returns Sawyer::Resource-like objects with method access



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#retry_delayInteger

Returns Delay in seconds between retries (default: 1).

Returns:

  • (Integer)

    Delay in seconds between retries (default: 1)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

#timeoutInteger

Returns Request timeout in seconds (default: 30).

Returns:

  • (Integer)

    Request timeout in seconds (default: 30)



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schwab/configuration.rb', line 40

attr_accessor :client_id,
:client_secret,
:redirect_uri,
:api_base_url,
:api_version,
:logger,
:timeout,
:open_timeout,
:faraday_adapter,
:max_retries,
:retry_delay

Instance Method Details

#api_endpointObject

Get the full API endpoint URL with version



82
83
84
# File 'lib/schwab/configuration.rb', line 82

def api_endpoint
  "#{api_base_url}/#{api_version}"
end

#oauth_authorize_urlString

OAuth-specific endpoints

Returns:

  • (String)

    The OAuth authorization URL



88
89
90
# File 'lib/schwab/configuration.rb', line 88

def oauth_authorize_url
  "#{api_base_url}/v1/oauth/authorize"
end

#oauth_configured?Boolean

Check if OAuth credentials are configured

Returns:

  • (Boolean)


119
120
121
# File 'lib/schwab/configuration.rb', line 119

def oauth_configured?
  !client_id.nil? && !client_secret.nil? && !redirect_uri.nil?
end

#oauth_token_urlString

Get the OAuth token endpoint URL

Returns:

  • (String)

    The OAuth token URL



94
95
96
# File 'lib/schwab/configuration.rb', line 94

def oauth_token_url
  "#{api_base_url}/v1/oauth/token"
end

#to_hObject

Convert configuration to a hash



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/schwab/configuration.rb', line 124

def to_h
  {
    client_id: client_id,
    client_secret: client_secret,
    redirect_uri: redirect_uri,
    api_base_url: api_base_url,
    timeout: timeout,
    open_timeout: open_timeout,
    faraday_adapter: faraday_adapter,
    max_retries: max_retries,
    retry_delay: retry_delay,
    logger: logger,
    response_format: response_format,
  }
end

#validate!Object

Validate that required OAuth parameters are present



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/schwab/configuration.rb', line 99

def validate!
  missing = []
  missing << "client_id" if client_id.nil? || client_id.empty?
  missing << "client_secret" if client_secret.nil? || client_secret.empty?
  missing << "redirect_uri" if redirect_uri.nil? || redirect_uri.empty?

  unless missing.empty?
    raise Error, "Missing required configuration: #{missing.join(", ")}"
  end

  # Validate response_format
  valid_formats = [:hash, :resource]
  unless valid_formats.include?(response_format)
    raise Error, "Invalid response_format: #{response_format}. Must be :hash or :resource"
  end

  true
end