class Zenaton::Client

Zenaton Client

Constants

APP_ENV
APP_ID
DEFAULT_WORKER_PORT
WORKER_API_VERSION
ZENATON_WORKER_URL

Attributes

api_token[W]
app_env[W]
app_id[W]

Public Class Methods

init(app_id, api_token, app_env) click to toggle source

Class method that sets the three tokens needed to interact with the API @param app_id [String] the ID of your Zenaton application @param api_token [String] your Zenaton account API token @param app_env [String] the environment (dev, staging, prod) to run under @return [Zenaton::Client] the instance of the client.

# File lib/zenaton/client.rb, line 28
def self.init(app_id, api_token, app_env)
  instance.tap do |client|
    client.app_id = app_id
    client.api_token = api_token
    client.app_env = app_env
  end
end
new() click to toggle source

@private

# File lib/zenaton/client.rb, line 37
def initialize
  @http = Services::Http.new
  @graphql = Services::GraphQL::Client.new(http: @http)
  @serializer = Services::Serializer.new
  @properties = Services::Properties.new
end

Public Instance Methods

find_workflow(workflow_name, custom_id) click to toggle source

Finds a workflow @param workflow_name [String] the class name of the workflow @param custom_id [String] the custom ID of the workflow @return [Zenaton::Interfaces::Workflow, nil]

# File lib/zenaton/client.rb, line 112
def find_workflow(workflow_name, custom_id)
  @graphql.find_workflow(workflow_name, custom_id, credentials)
end
kill_workflow(name, custom_id) click to toggle source

Stops a workflow @param name [String] the class name of the workflow @param custom_id [String] the custom ID of the workflow @return [NilClass]

# File lib/zenaton/client.rb, line 88
def kill_workflow(name, custom_id)
  @graphql.kill_workflow(name, custom_id, credentials)
end
pause_workflow(name, custom_id) click to toggle source

Pauses a workflow @param name [String] the class name of the workflow @param custom_id [String] the custom ID of the workflow @return [NilClass]

# File lib/zenaton/client.rb, line 96
def pause_workflow(name, custom_id)
  @graphql.pause_workflow(name, custom_id, credentials)
end
resume_workflow(name, custom_id) click to toggle source

Resumes a workflow @param name [String] the class name of the workflow @param custom_id [String] the custom ID of the workflow @return [NilClass]

# File lib/zenaton/client.rb, line 104
def resume_workflow(name, custom_id)
  @graphql.resume_workflow(name, custom_id, credentials)
end
send_event(workflow_name, custom_id, event) click to toggle source

Sends an event to a workflow @param workflow_name [String] the class name of the workflow @param custom_id [String] the custom ID of the workflow @param event [Zenaton::Interfaces::Event] the event to send @return [NilClass]

# File lib/zenaton/client.rb, line 121
def send_event(workflow_name, custom_id, event)
  @graphql.send_event(workflow_name, custom_id, event, credentials)
end
start_scheduled_task(task, cron) click to toggle source

Schedule a task for repeated execution

# File lib/zenaton/client.rb, line 73
def start_scheduled_task(task, cron)
  res = @graphql.schedule_task(task, cron, credentials)
  res && res['createTaskSchedule']
end
start_scheduled_workflow(flow, cron) click to toggle source

Schedule a workflow for repeated execution

# File lib/zenaton/client.rb, line 79
def start_scheduled_workflow(flow, cron)
  res = @graphql.schedule_workflow(flow, cron, credentials)
  res && res['createWorkflowSchedule']
end
start_task(task) click to toggle source

Start a single task @param task [Zenaton::Interfaces::Task]

# File lib/zenaton/client.rb, line 62
def start_task(task)
  @graphql.start_task(task, credentials)
end
start_workflow(flow) click to toggle source

Start the specified workflow @param flow [Zenaton::Interfaces::Workflow]

# File lib/zenaton/client.rb, line 68
def start_workflow(flow)
  @graphql.start_workflow(flow, credentials)
end
worker_url(resource = '', params = {}) click to toggle source

Gets the url for the workers @param resource [String] the endpoint for the worker @param params [Hash|String] query params to be url encoded @return [String] the workers url with parameters

# File lib/zenaton/client.rb, line 48
def worker_url(resource = '', params = {})
  base_url = ENV['ZENATON_WORKER_URL'] || ZENATON_WORKER_URL
  port = ENV['ZENATON_WORKER_PORT'] || DEFAULT_WORKER_PORT
  url = "#{base_url}:#{port}/api/#{WORKER_API_VERSION}/#{resource}"

  if params.is_a?(Hash)
    append_params_to_url(url, params)
  else
    add_app_env("#{url}?", params)
  end
end

Private Instance Methods

add_app_env(url, params) click to toggle source

DEPRECATED: This implementation does not safely encode the parameters to be passed as query params in a get request. This method gets called by agents up to version 0.4.5

# File lib/zenaton/client.rb, line 138
    def add_app_env(url, params)
      deprecation_warning = <<~WARN
        [WARNING] You are running a Zenaton agent with a version <= 0.4.5
                  Please consider upgrading to a more recent version.
      WARN
      warn(deprecation_warning)

      app_env = @app_env ? "#{APP_ENV}=#{@app_env}&" : ''
      app_id = @app_id ? "#{APP_ID}=#{@app_id}&" : ''

      "#{url}#{app_env}#{app_id}#{params}"
    end
append_params_to_url(url, params) click to toggle source
# File lib/zenaton/client.rb, line 151
def append_params_to_url(url, params)
  params[APP_ENV] = @app_env if @app_env
  params[APP_ID] = @app_id if @app_id

  "#{url}?#{URI.encode_www_form(params)}"
end
credentials() click to toggle source
# File lib/zenaton/client.rb, line 127
def credentials
  {
    'app_id' => @app_id,
    'api_token' => @api_token,
    'app_env' => @app_env
  }
end