Class: Schwab::AccountNumberResolver

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

Overview

Resolves plain text account numbers to their encrypted hash values required by the Schwab API for URL path parameters

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ AccountNumberResolver

Initialize a new resolver for the given client

Parameters:

  • client (Schwab::Client)

    The client instance to use for API calls



10
11
12
13
14
15
# File 'lib/schwab/account_number_resolver.rb', line 10

def initialize(client)
  @client = client
  @mappings = {}
  @loaded = false
  @mutex = Mutex.new
end

Instance Method Details

#loaded?Boolean

Check if the account number mappings are loaded

Returns:

  • (Boolean)

    True if mappings are loaded



66
67
68
# File 'lib/schwab/account_number_resolver.rb', line 66

def loaded?
  @loaded
end

#mappingsHash<String, String>

Get all account numbers and their hash values

Examples:

Get mappings

resolver.mappings  # => {"123456789" => "ABC123XYZ"}

Returns:

  • (Hash<String, String>)

    Plain account number => hash value mapping



56
57
58
59
60
61
# File 'lib/schwab/account_number_resolver.rb', line 56

def mappings
  @mutex.synchronize do
    load_mappings unless @loaded
    @mappings.dup
  end
end

#refresh!void

This method returns an undefined value.

Refresh the account number mappings from the API

Examples:

Refresh mappings

resolver.refresh!


45
46
47
48
49
# File 'lib/schwab/account_number_resolver.rb', line 45

def refresh!
  @mutex.synchronize do
    refresh_mappings
  end
end

#resolve(account_number) ⇒ String

Resolve an account number to its encrypted hash value

Examples:

Resolve account number

resolver.resolve("123456789")  # => "ABC123XYZ"
resolver.resolve("ABC123XYZ")  # => "ABC123XYZ" (already encrypted)

Parameters:

  • account_number (String)

    Plain account number or encrypted hash value

Returns:

  • (String)

    The encrypted hash value to use in API calls

Raises:

  • (Error)

    If the account number is not found



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/schwab/account_number_resolver.rb', line 25

def resolve()
  return  if looks_like_hash?()

  @mutex.synchronize do
    load_mappings unless @loaded

    hash_value = @mappings[.to_s]
    return hash_value if hash_value

    # Try refreshing mappings in case this is a new account
    refresh_mappings
    @mappings[.to_s] || ()
  end
end