Class: Zenaton::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/zenaton/client.rb

Overview

Zenaton Client

Constant Summary collapse

ZENATON_API_URL =

Zenaton api url

'https://api.zenaton.com/v1'
ZENATON_WORKER_URL =

Default worker url

'http://localhost'
DEFAULT_WORKER_PORT =

Default worker port

4001
WORKER_API_VERSION =

Default worker api version

'v_newton'
MAX_ID_SIZE =

Limit on length of custom ids

256
APP_ENV =

Parameter name for the application environment

'app_env'
APP_ID =

Parameter name for the application ID

'app_id'
API_TOKEN =

Parameter name for the API token

'api_token'
ATTR_ID =

Parameter name for custom ids

'custom_id'
ATTR_NAME =

Parameter name for workflow names

'name'
ATTR_CANONICAL =

Parameter name for version name

'canonical_name'
ATTR_DATA =

Parameter name for json payload

'data'
ATTR_PROG =

Parameter name for the language

'programming_language'
ATTR_MODE =

Parameter name for the worker update mode

'mode'
ATTR_MAX_PROCESSING_TIME =

Parameter name for task maximum processing time

'maxProcessingTime'
PROG =

The current programming language

'Ruby'
EVENT_INPUT =

Parameter name for event input

'event_input'
EVENT_NAME =

Parameter name for event name

'event_name'
EVENT_DATA =

Parameter name for event data

'event_data'
WORKFLOW_KILL =

Worker update mode to stop a worker

'kill'
WORKFLOW_PAUSE =

Worker udpate mode to pause a worker

'pause'
WORKFLOW_RUN =

Worker update mode to resume a worker

'run'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_token=(value) ⇒ Object (writeonly)

Sets the attribute api_token

Parameters:

  • value

    the value to set the attribute api_token to.



44
45
46
# File 'lib/zenaton/client.rb', line 44

def api_token=(value)
  @api_token = value
end

#app_env=(value) ⇒ Object (writeonly)

Sets the attribute app_env

Parameters:

  • value

    the value to set the attribute app_env to.



44
45
46
# File 'lib/zenaton/client.rb', line 44

def app_env=(value)
  @app_env = value
end

#app_id=(value) ⇒ Object (writeonly)

Sets the attribute app_id

Parameters:

  • value

    the value to set the attribute app_id to.



44
45
46
# File 'lib/zenaton/client.rb', line 44

def app_id=(value)
  @app_id = value
end

Class Method Details

.init(app_id, api_token, app_env) ⇒ Zenaton::Client

Class method that sets the three tokens needed to interact with the API

Parameters:

  • app_id (String)

    the ID of your Zenaton application

  • api_token (String)

    your Zenaton account API token

  • app_env (String)

    the environment (dev, staging, prod) to run under

Returns:



51
52
53
54
55
56
57
# File 'lib/zenaton/client.rb', line 51

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

Instance Method Details

#find_workflow(workflow_name, custom_id) ⇒ Zenaton::Interfaces::Workflow?

Finds a workflow

Parameters:

  • workflow_name (String)

    the class name of the workflow

  • custom_id (String)

    the custom ID of the workflow

Returns:



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/zenaton/client.rb', line 154

def find_workflow(workflow_name, custom_id)
  params = { ATTR_ID => custom_id, ATTR_NAME => workflow_name }
  params[ATTR_PROG] = PROG
  data = @http.get(instance_website_url(params))['data']
  data && @properties.object_from(
    data['name'],
    @serializer.decode(data['properties'])
  )
rescue Zenaton::InternalError => exception
  return nil if exception.message =~ /No workflow instance found/
  raise exception
end

#kill_workflow(workflow_name, custom_id) ⇒ NilClass

Stops a workflow

Parameters:

  • workflow_name (String)

    the class name of the workflow

  • custom_id (String)

    the custom ID of the workflow (if any)

Returns:

  • (NilClass)


130
131
132
# File 'lib/zenaton/client.rb', line 130

def kill_workflow(workflow_name, custom_id)
  update_instance(workflow_name, custom_id, WORKFLOW_KILL)
end

#pause_workflow(workflow_name, custom_id) ⇒ NilClass

Pauses a workflow

Parameters:

  • workflow_name (String)

    the class name of the workflow

  • custom_id (String)

    the custom ID of the workflow (if any)

Returns:

  • (NilClass)


138
139
140
# File 'lib/zenaton/client.rb', line 138

def pause_workflow(workflow_name, custom_id)
  update_instance(workflow_name, custom_id, WORKFLOW_PAUSE)
end

#resume_workflow(workflow_name, custom_id) ⇒ NilClass

Resumes a workflow

Parameters:

  • workflow_name (String)

    the class name of the workflow

  • custom_id (String)

    the custom ID of the workflow (if any)

Returns:

  • (NilClass)


146
147
148
# File 'lib/zenaton/client.rb', line 146

def resume_workflow(workflow_name, custom_id)
  update_instance(workflow_name, custom_id, WORKFLOW_RUN)
end

#send_event(workflow_name, custom_id, event) ⇒ NilClass

Sends an event to a workflow

Parameters:

  • workflow_name (String)

    the class name of the workflow

  • custom_id (String)

    the custom ID of the workflow (if any)

  • event (Zenaton::Interfaces::Event)

    the event to send

Returns:

  • (NilClass)


172
173
174
175
176
177
178
179
180
181
182
# File 'lib/zenaton/client.rb', line 172

def send_event(workflow_name, custom_id, event)
  body = {
    ATTR_PROG => PROG,
    ATTR_NAME => workflow_name,
    ATTR_ID => custom_id,
    EVENT_NAME => event.class.name,
    EVENT_INPUT => @serializer.encode(@properties.from(event)),
    EVENT_DATA => @serializer.encode(event)
  }
  @http.post(send_event_url, body)
end

#start_task(task) ⇒ Object

Start a single task

Parameters:



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/zenaton/client.rb', line 100

def start_task(task)
  max_processing_time = if task.respond_to?(:max_processing_time)
                          task.max_processing_time
                        end
  @http.post(
    worker_url('tasks'),
    ATTR_PROG => PROG,
    ATTR_NAME => class_name(task),
    ATTR_DATA => @serializer.encode(@properties.from(task)),
    ATTR_MAX_PROCESSING_TIME => max_processing_time
  )
end

#start_workflow(flow) ⇒ Object

Start the specified workflow

Parameters:



115
116
117
118
119
120
121
122
123
124
# File 'lib/zenaton/client.rb', line 115

def start_workflow(flow)
  @http.post(
    instance_worker_url,
    ATTR_PROG => PROG,
    ATTR_CANONICAL => canonical_name(flow),
    ATTR_NAME => class_name(flow),
    ATTR_DATA => @serializer.encode(@properties.from(flow)),
    ATTR_ID => parse_custom_id_from(flow)
  )
end

#website_url(resource = '', params = {}) ⇒ String

Gets the url for zenaton api

Parameters:

  • resource (String) (defaults to: '')

    the endpoint for the api

  • params (Hash|String) (defaults to: {})

    query params to be url encoded

Returns:

  • (String)

    the api url with parameters



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zenaton/client.rb', line 86

def website_url(resource = '', params = {})
  api_url = ENV['ZENATON_API_URL'] || ZENATON_API_URL
  url = "#{api_url}/#{resource}"

  if params.is_a?(Hash)
    params[API_TOKEN] = @api_token
    append_params_to_url(url, params)
  else
    add_app_env("#{url}?#{API_TOKEN}=#{@api_token}&", params)
  end
end

#worker_url(resource = '', params = {}) ⇒ String

Gets the url for the workers

Parameters:

  • resource (String) (defaults to: '')

    the endpoint for the worker

  • params (Hash|String) (defaults to: {})

    query params to be url encoded

Returns:

  • (String)

    the workers url with parameters



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zenaton/client.rb', line 70

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