You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# app/lib/market_data_pull/tdameritrade/api_operation.rb# Include this module inside of a class and then to use it, call# # perform_request { |client| client.<operation method> }#moduleMarketDataPull;moduleTDAmeritrademoduleAPIOperationmodule_function@@next_operation_time=Time.currentdefperform_requestlast_creds=TDAmeritradeToken.last_credsclient=iflast_creds.access_token_expired?TDAmeritradeToken.build_client_with_new_access_tokenelseTDAmeritradeToken.build_clientendwait_for_next_operation_timefailed_attempts=0beginclient_result=yield(client)set_next_operation_timereturnclient_result# otherwise the next operation timestamp will be returnedrescue ::TDAmeritrade::Error::RateLimitError, ::TDAmeritrade::Error::TDAmeritradeError,Timeout::Error=>eputs"#{e.class} - #{e.message}"sleep61failed_attempts += 1failed_attempts >= 3 ? raise('Error performing TDAmeritrade API request') : retryendenddefset_next_operation_time@@next_operation_time=Time.current + 0.5.secondsend# There is a limit on the number of operations you can do in a second, as well as a limit of 120 per minute. This# setting of a time to perform the next operation will help stagger our API calls to stay within those limits.defwait_for_next_operation_timeuntilTime.current >= @@next_operation_timesleep0.1endendendend;end