Class: SchwabRb::AccountHashManager
- Inherits:
-
Object
- Object
- SchwabRb::AccountHashManager
- Defined in:
- lib/schwab_rb/account_hash_manager.rb
Defined Under Namespace
Classes: AccountNamesFileNotFoundError, InvalidAccountNamesFileError
Instance Attribute Summary collapse
-
#account_hashes_path ⇒ Object
readonly
Returns the value of attribute account_hashes_path.
-
#account_names ⇒ Object
readonly
Returns the value of attribute account_names.
-
#account_names_path ⇒ Object
readonly
Returns the value of attribute account_names_path.
Instance Method Summary collapse
- #available_account_names ⇒ Object
- #get_all_hashes ⇒ Object
- #get_hash_by_name(account_name) ⇒ Object
-
#initialize(account_names_path = nil, account_hashes_path = nil) ⇒ AccountHashManager
constructor
A new instance of AccountHashManager.
- #update_hashes_from_api_response(account_numbers_response) ⇒ Object
Constructor Details
#initialize(account_names_path = nil, account_hashes_path = nil) ⇒ AccountHashManager
Returns a new instance of AccountHashManager.
13 14 15 16 17 18 19 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 13 def initialize(account_names_path = nil, account_hashes_path = nil) @account_names_path = account_names_path || SchwabRb.configuration.account_names_path @account_hashes_path = account_hashes_path || SchwabRb.configuration.account_hashes_path @account_names_path = File.(@account_names_path) @account_hashes_path = File.(@account_hashes_path) @account_names = [] end |
Instance Attribute Details
#account_hashes_path ⇒ Object (readonly)
Returns the value of attribute account_hashes_path.
11 12 13 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 11 def account_hashes_path @account_hashes_path end |
#account_names ⇒ Object (readonly)
Returns the value of attribute account_names.
11 12 13 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 11 def account_names @account_names end |
#account_names_path ⇒ Object (readonly)
Returns the value of attribute account_names_path.
11 12 13 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 11 def account_names_path @account_names_path end |
Instance Method Details
#available_account_names ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 71 def available_account_names begin load_account_names.keys rescue AccountNamesFileNotFoundError [] end end |
#get_all_hashes ⇒ Object
67 68 69 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 67 def get_all_hashes load_account_hashes end |
#get_hash_by_name(account_name) ⇒ Object
62 63 64 65 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 62 def get_hash_by_name(account_name) hashes = load_account_hashes hashes[account_name] end |
#update_hashes_from_api_response(account_numbers_response) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/schwab_rb/account_hash_manager.rb', line 21 def update_hashes_from_api_response(account_numbers_response) account_names = load_account_names current_hashes = load_account_hashes number_to_hash = {} account_numbers_response.each do |account_data| account_number = account_data[:accountNumber] hash_value = account_data[:hashValue] number_to_hash[account_number] = hash_value end updated_hashes = {} missing_accounts = [] account_names.each do |name, account_number| if number_to_hash.key?(account_number) updated_hashes[name] = number_to_hash[account_number] elsif current_hashes.key?(name) # Keep existing hash but warn that account wasn't in API response updated_hashes[name] = current_hashes[name] missing_accounts << { name: name, number: account_number } else # Account name exists but no hash found (new or invalid account) missing_accounts << { name: name, number: account_number } end end # Log warnings for accounts that weren't found in API response if missing_accounts.any? missing_accounts.each do |account| SchwabRb::Logger.logger.warn( "Account '#{account[:name]}' (#{account[:number]}) not found in API response. " \ "This may indicate a closed account or incorrect account number in account_names.json" ) end end save_account_hashes(updated_hashes) updated_hashes end |