Class: MaxApiClient::Polling
- Inherits:
-
Object
- Object
- MaxApiClient::Polling
- Defined in:
- lib/max_api_client/polling.rb,
sig/max_api_client.rbs
Overview
Long-polling helper over GET /updates with marker tracking and retries.
Constant Summary collapse
- DEFAULT_TIMEOUT =
20- DEFAULT_RETRY_INTERVAL =
5- READ_TIMEOUT_PADDING =
5- RETRYABLE_ERRORS =
[ Net::OpenTimeout, Net::ReadTimeout, EOFError, IOError, SocketError, Errno::ECONNRESET ].freeze
Instance Method Summary collapse
-
#each {|update| ... } ⇒ self
rubocop:enable Metrics/ParameterLists.
-
#initialize(api, types: [], marker: nil, timeout: DEFAULT_TIMEOUT, retry_interval: DEFAULT_RETRY_INTERVAL, read_timeout: nil) ⇒ Polling
constructor
rubocop:disable Metrics/ParameterLists.
- #stop ⇒ Boolean
- #stopped? ⇒ Boolean
Constructor Details
#initialize(api, types: [], marker: nil, timeout: DEFAULT_TIMEOUT, retry_interval: DEFAULT_RETRY_INTERVAL, read_timeout: nil) ⇒ Polling
rubocop:disable Metrics/ParameterLists
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/max_api_client/polling.rb', line 19 def initialize(api, types: [], marker: nil, timeout: DEFAULT_TIMEOUT, retry_interval: DEFAULT_RETRY_INTERVAL, read_timeout: nil) @api = api @types = types @marker = marker @timeout = timeout @retry_interval = retry_interval @read_timeout = read_timeout || (timeout.to_i + READ_TIMEOUT_PADDING) @stopped = false end |
Instance Method Details
#each {|update| ... } ⇒ self
rubocop:enable Metrics/ParameterLists
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/max_api_client/polling.rb', line 31 def each return enum_for(:each) unless block_given? until stopped? begin response = fetch_updates @marker = fetch_value(response, :marker) Array(fetch_value(response, :updates)).each do |update| break if stopped? yield update end rescue *RETRYABLE_ERRORS retry_later rescue ApiError => e raise unless retryable_status?(e.status) retry_later end end self end |
#stop ⇒ Boolean
56 57 58 |
# File 'lib/max_api_client/polling.rb', line 56 def stop @stopped = true end |
#stopped? ⇒ Boolean
60 61 62 |
# File 'lib/max_api_client/polling.rb', line 60 def stopped? @stopped end |