Class: Apertur::Resources::Polling
- Inherits:
-
Object
- Object
- Apertur::Resources::Polling
- Defined in:
- lib/apertur/resources/polling.rb
Overview
Poll upload sessions for new images and download them.
Provides a blocking polling loop (#poll_and_process) that fetches, downloads, and acknowledges new images automatically.
Instance Method Summary collapse
-
#ack(uuid, image_id) ⇒ Hash
Acknowledge (mark as processed) an image.
-
#download(uuid, image_id) ⇒ String
Download an image from a session.
-
#initialize(http) ⇒ Polling
constructor
A new instance of Polling.
-
#list(uuid) ⇒ Hash
List pending (un-acknowledged) images in a session.
-
#poll_and_process(uuid, interval: 3) {|image, data| ... } ⇒ void
Blocking polling loop that fetches, downloads, and acknowledges images.
Constructor Details
#initialize(http) ⇒ Polling
Returns a new instance of Polling.
11 12 13 |
# File 'lib/apertur/resources/polling.rb', line 11 def initialize(http) @http = http end |
Instance Method Details
#ack(uuid, image_id) ⇒ Hash
Acknowledge (mark as processed) an image.
37 38 39 |
# File 'lib/apertur/resources/polling.rb', line 37 def ack(uuid, image_id) @http.request(:post, "/api/v1/upload-sessions/#{uuid}/images/#{image_id}/ack") end |
#download(uuid, image_id) ⇒ String
Download an image from a session.
28 29 30 |
# File 'lib/apertur/resources/polling.rb', line 28 def download(uuid, image_id) @http.request_raw(:get, "/api/v1/upload-sessions/#{uuid}/images/#{image_id}") end |
#list(uuid) ⇒ Hash
List pending (un-acknowledged) images in a session.
19 20 21 |
# File 'lib/apertur/resources/polling.rb', line 19 def list(uuid) @http.request(:get, "/api/v1/upload-sessions/#{uuid}/poll") end |
#poll_and_process(uuid, interval: 3) {|image, data| ... } ⇒ void
This method returns an undefined value.
Blocking polling loop that fetches, downloads, and acknowledges images.
Calls the provided block for each new image. The loop runs until the calling thread is interrupted or the block raises an exception.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/apertur/resources/polling.rb', line 52 def poll_and_process(uuid, interval: 3, &handler) raise ArgumentError, "A block is required" unless block_given? loop do result = list(uuid) images = result["images"] || [] images.each do |image| data = download(uuid, image["id"]) handler.call(image, data) ack(uuid, image["id"]) end sleep(interval) end end |