Class: Schwab::Client

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

Overview

Main client for interacting with the Schwab API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, refresh_token: nil, auto_refresh: false, on_token_refresh: nil, config: nil) ⇒ Client

Initialize a new Schwab API client

Parameters:

  • access_token (String)

    OAuth access token

  • refresh_token (String, nil) (defaults to: nil)

    OAuth refresh token for auto-refresh

  • auto_refresh (Boolean) (defaults to: false)

    Whether to automatically refresh expired tokens

  • on_token_refresh (Proc, nil) (defaults to: nil)

    Callback when token is refreshed

  • config (Configuration, nil) (defaults to: nil)

    Custom configuration (uses global if not provided)



26
27
28
29
30
31
32
33
34
35
# File 'lib/schwab/client.rb', line 26

def initialize(access_token:, refresh_token: nil, auto_refresh: false, on_token_refresh: nil, config: nil)
  @access_token = access_token
  @refresh_token = refresh_token
  @auto_refresh = auto_refresh
  @on_token_refresh = on_token_refresh
  @config = config || Schwab.configuration || Configuration.new
  @connection = nil
  @account_resolver = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



17
18
19
# File 'lib/schwab/client.rb', line 17

def access_token
  @access_token
end

#auto_refreshObject (readonly)

Returns the value of attribute auto_refresh.



17
18
19
# File 'lib/schwab/client.rb', line 17

def auto_refresh
  @auto_refresh
end

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/schwab/client.rb', line 17

def config
  @config
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



17
18
19
# File 'lib/schwab/client.rb', line 17

def refresh_token
  @refresh_token
end

Instance Method Details

#account_resolverAccountNumberResolver

Get the account number resolver (lazily initialized)

Returns:



121
122
123
124
125
# File 'lib/schwab/client.rb', line 121

def 
  @mutex.synchronize do
    @account_resolver ||= AccountNumberResolver.new(self)
  end
end

#connectionFaraday::Connection

Get the Faraday connection (lazily initialized)

Returns:

  • (Faraday::Connection)

    The configured HTTP connection



40
41
42
43
44
# File 'lib/schwab/client.rb', line 40

def connection
  @mutex.synchronize do
    @connection ||= build_connection
  end
end

#delete(path, params = {}, resource_class = nil) ⇒ Hash, Resources::Base

Make a DELETE request to the API

Parameters:

  • path (String)

    The API endpoint path

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

    Query parameters

  • resource_class (Class, nil) (defaults to: nil)

    Optional resource class for response wrapping

Returns:

  • (Hash, Resources::Base)

    The response (hash or resource based on config)



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

def delete(path, params = {}, resource_class = nil)
  request(:delete, path, params, resource_class)
end

#get(path, params = {}, resource_class = nil) ⇒ Hash, Resources::Base

Make a GET request to the API

Parameters:

  • path (String)

    The API endpoint path

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

    Query parameters

  • resource_class (Class, nil) (defaults to: nil)

    Optional resource class for response wrapping

Returns:

  • (Hash, Resources::Base)

    The response (hash or resource based on config)



52
53
54
# File 'lib/schwab/client.rb', line 52

def get(path, params = {}, resource_class = nil)
  request(:get, path, params, resource_class)
end

#patch(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base

Make a PATCH request to the API

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

  • resource_class (Class, nil) (defaults to: nil)

    Optional resource class for response wrapping

Returns:

  • (Hash, Resources::Base)

    The response (hash or resource based on config)



92
93
94
# File 'lib/schwab/client.rb', line 92

def patch(path, body = {}, resource_class = nil)
  request(:patch, path, body, resource_class)
end

#post(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base

Make a POST request to the API

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

  • resource_class (Class, nil) (defaults to: nil)

    Optional resource class for response wrapping

Returns:

  • (Hash, Resources::Base)

    The response (hash or resource based on config)



62
63
64
# File 'lib/schwab/client.rb', line 62

def post(path, body = {}, resource_class = nil)
  request(:post, path, body, resource_class)
end

#put(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base

Make a PUT request to the API

Parameters:

  • path (String)

    The API endpoint path

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

    Request body

  • resource_class (Class, nil) (defaults to: nil)

    Optional resource class for response wrapping

Returns:

  • (Hash, Resources::Base)

    The response (hash or resource based on config)



72
73
74
# File 'lib/schwab/client.rb', line 72

def put(path, body = {}, resource_class = nil)
  request(:put, path, body, resource_class)
end

#refresh_account_mappings!void

This method returns an undefined value.

Refresh account number mappings

Examples:

Refresh account mappings

client.


142
143
144
# File 'lib/schwab/client.rb', line 142

def 
  .refresh!
end

#resolve_account_number(account_number) ⇒ String

Resolve an account number to its encrypted hash value

Examples:

Resolve account number

client.("123456789")  # => "ABC123XYZ"

Parameters:

  • account_number (String)

    Plain account number or encrypted hash

Returns:

  • (String)

    The encrypted hash value for API calls



133
134
135
# File 'lib/schwab/client.rb', line 133

def ()
  .resolve()
end

#update_access_token(new_token) ⇒ Object

Update the access token (useful after manual refresh)

Parameters:

  • new_token (String)

    The new access token



99
100
101
102
103
104
# File 'lib/schwab/client.rb', line 99

def update_access_token(new_token)
  @mutex.synchronize do
    @access_token = new_token
    @connection = nil # Force rebuild of connection with new token
  end
end

#update_tokens(access_token:, refresh_token: nil) ⇒ Object

Update both access and refresh tokens

Parameters:

  • access_token (String)

    The new access token

  • refresh_token (String, nil) (defaults to: nil)

    The new refresh token



110
111
112
113
114
115
116
# File 'lib/schwab/client.rb', line 110

def update_tokens(access_token:, refresh_token: nil)
  @mutex.synchronize do
    @access_token = access_token
    @refresh_token = refresh_token if refresh_token
    @connection = nil # Force rebuild of connection
  end
end