Class: Scrapio::Resources::Jobs
- Inherits:
-
Object
- Object
- Scrapio::Resources::Jobs
- Defined in:
- lib/scrapio/resources/jobs.rb
Constant Summary collapse
- TERMINAL =
%w[completed partial failed cancelled].freeze
Instance Method Summary collapse
- #create(kind:, input:, webhook_url: nil) ⇒ Object
- #get(job_id) ⇒ Object
- #get_result(job_id) ⇒ Object
-
#initialize(http) ⇒ Jobs
constructor
A new instance of Jobs.
- #wait_for_completion(job_id, poll_interval: 2.0, timeout: 300.0) ⇒ Object
Constructor Details
#initialize(http) ⇒ Jobs
Returns a new instance of Jobs.
6 |
# File 'lib/scrapio/resources/jobs.rb', line 6 def initialize(http) = @http = http |
Instance Method Details
#create(kind:, input:, webhook_url: nil) ⇒ Object
8 9 10 11 |
# File 'lib/scrapio/resources/jobs.rb', line 8 def create(kind:, input:, webhook_url: nil) webhook = webhook_url.nil? ? nil : { url: webhook_url } @http.post("/v1/jobs", { kind: kind, input: input, webhook: webhook }) end |
#get(job_id) ⇒ Object
13 14 15 |
# File 'lib/scrapio/resources/jobs.rb', line 13 def get(job_id) @http.get("/v1/jobs/#{job_id}") end |
#get_result(job_id) ⇒ Object
17 18 19 |
# File 'lib/scrapio/resources/jobs.rb', line 17 def get_result(job_id) @http.get("/v1/jobs/#{job_id}/result") end |
#wait_for_completion(job_id, poll_interval: 2.0, timeout: 300.0) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/scrapio/resources/jobs.rb', line 21 def wait_for_completion(job_id, poll_interval: 2.0, timeout: 300.0) deadline = Time.now + timeout loop do job = get(job_id) return get_result(job_id) if TERMINAL.include?(job["status"]) raise ScrapioError, "Job #{job_id} did not complete within #{timeout}s" if Time.now >= deadline sleep(poll_interval) end end |