Class: Schwab::Client
- Inherits:
-
Object
- Object
- Schwab::Client
- Defined in:
- lib/schwab/client.rb
Overview
Main client for interacting with the Schwab API
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#auto_refresh ⇒ Object
readonly
Returns the value of attribute auto_refresh.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
-
#account_resolver ⇒ AccountNumberResolver
Get the account number resolver (lazily initialized).
-
#connection ⇒ Faraday::Connection
Get the Faraday connection (lazily initialized).
-
#delete(path, params = {}, resource_class = nil) ⇒ Hash, Resources::Base
Make a DELETE request to the API.
-
#get(path, params = {}, resource_class = nil) ⇒ Hash, Resources::Base
Make a GET request to the API.
-
#initialize(access_token:, refresh_token: nil, auto_refresh: false, on_token_refresh: nil, config: nil) ⇒ Client
constructor
Initialize a new Schwab API client.
-
#patch(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base
Make a PATCH request to the API.
-
#post(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base
Make a POST request to the API.
-
#put(path, body = {}, resource_class = nil) ⇒ Hash, Resources::Base
Make a PUT request to the API.
-
#refresh_account_mappings! ⇒ void
Refresh account number mappings.
-
#resolve_account_number(account_number) ⇒ String
Resolve an account number to its encrypted hash value.
-
#update_access_token(new_token) ⇒ Object
Update the access token (useful after manual refresh).
-
#update_tokens(access_token:, refresh_token: nil) ⇒ Object
Update both access and refresh tokens.
Constructor Details
#initialize(access_token:, refresh_token: nil, auto_refresh: false, on_token_refresh: nil, config: nil) ⇒ Client
Initialize a new Schwab API client
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_token ⇒ Object (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_refresh ⇒ Object (readonly)
Returns the value of attribute auto_refresh.
17 18 19 |
# File 'lib/schwab/client.rb', line 17 def auto_refresh @auto_refresh end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
17 18 19 |
# File 'lib/schwab/client.rb', line 17 def config @config end |
#refresh_token ⇒ Object (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_resolver ⇒ AccountNumberResolver
Get the account number resolver (lazily initialized)
121 122 123 124 125 |
# File 'lib/schwab/client.rb', line 121 def account_resolver @mutex.synchronize do @account_resolver ||= AccountNumberResolver.new(self) end end |
#connection ⇒ Faraday::Connection
Get the Faraday connection (lazily initialized)
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
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
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
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
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
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
142 143 144 |
# File 'lib/schwab/client.rb', line 142 def refresh_account_mappings! account_resolver.refresh! end |
#resolve_account_number(account_number) ⇒ String
Resolve an account number to its encrypted hash value
133 134 135 |
# File 'lib/schwab/client.rb', line 133 def resolve_account_number(account_number) account_resolver.resolve(account_number) end |
#update_access_token(new_token) ⇒ Object
Update the access token (useful after manual refresh)
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
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 |