Class: Ghostcrawl::SchedulesClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ghostcrawl/client.rb

Overview

Manage schedules — /v1/schedules.

Instance Method Summary collapse

Constructor Details

#initialize(v1, adapter = nil) ⇒ SchedulesClient

Returns a new instance of SchedulesClient.



631
632
633
634
# File 'lib/ghostcrawl/client.rb', line 631

def initialize(v1, adapter = nil)
  @v1 = v1
  @adapter = adapter
end

Instance Method Details

#create(cron:, name: nil, job_type: nil, job_params: nil, task: nil, **opts) ⇒ Object

Create a new schedule. Delegates to POST /v1/schedules via the generated SchedulesRequestBuilder.

The API's ScheduleCreateRequest requires name, job_type ("scrape" | "crawl" | "change_monitor"), cron_expr, and job_params (the full scrape/crawl request body). It rejects any other top-level field with a 422 — so a legacy task key must be TRANSLATED, never forwarded.

Parameters:

  • cron (String)

    cron expression (sent as cron_expr)

  • name (String) (defaults to: nil)

    schedule name (required by the API)

  • job_type (String, nil) (defaults to: nil)

    "scrape" | "crawl" | "change_monitor"

  • job_params (Hash, nil) (defaults to: nil)

    full job request body (e.g. { url: ... })

  • task (Hash, nil) (defaults to: nil)

    DEPRECATED legacy shape { "action" => ..., ...rest }; when given (and job_type/job_params are absent) it is split into job_type (from task["action"]) and job_params (the remaining keys).



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/ghostcrawl/client.rb', line 663

def create(cron:, name: nil, job_type: nil, job_params: nil, task: nil, **opts)
  # Back-compat: derive job_type/job_params from a legacy `task` hash.
  if task.is_a?(Hash)
    t = task.transform_keys(&:to_s)
    job_type ||= t["action"] || t["job_type"] || t["type"]
    job_params ||= t.reject { |k, _| %w[action job_type type].include?(k) }
  end

  data = { "cron_expr" => cron }
  data["name"]       = name       unless name.nil?
  data["job_type"]   = job_type   unless job_type.nil?
  data["job_params"] = job_params unless job_params.nil?
  # opts may override/supply name/job_type/job_params/notify_webhook/monitor_mode.
  # Never forward a raw `task` field — the API 422s on it.
  data.merge!(opts.transform_keys(&:to_s).reject { |k, _| k == "task" })

  ResponseHelper.to_hash(@v1.schedules.post(AdditionalDataBody.new(data)))
end

#delete(schedule_id) ⇒ Object

Delete a schedule. Delegates to DELETE /v1/schedules/id. The generated builder returns a Fiber that must be resumed to actually send the request (the old ; {} discarded it, so the DELETE never fired). ResponseHelper.void_request! runs it and tolerates the empty 204 body.



687
688
689
# File 'lib/ghostcrawl/client.rb', line 687

def delete(schedule_id)
  ResponseHelper.void_request!(@adapter, @v1.schedules.by_schedule_id(schedule_id).to_delete_request_information(nil))
end

#get(schedule_id) ⇒ Object

Get a schedule by ID. Delegates to GET /v1/schedules/id via the generated builder.



644
645
646
# File 'lib/ghostcrawl/client.rb', line 644

def get(schedule_id)
  ResponseHelper.to_hash(@v1.schedules.by_schedule_id(schedule_id).get)
end

#listObject

List all schedules. Delegates to GET /v1/schedules via the generated SchedulesRequestBuilder.



638
639
640
# File 'lib/ghostcrawl/client.rb', line 638

def list
  ResponseHelper.to_hash(@v1.schedules.get)
end