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://zenaton.com/api/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'
PROG =

The current programming language

'Ruby'
EVENT_INPUT =

Parameter name for event data

'event_input'
EVENT_NAME =

Parameter name for event name

'event_name'
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.



41
42
43
# File 'lib/zenaton/client.rb', line 41

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.



41
42
43
# File 'lib/zenaton/client.rb', line 41

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.



41
42
43
# File 'lib/zenaton/client.rb', line 41

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:



48
49
50
51
52
53
54
# File 'lib/zenaton/client.rb', line 48

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:



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/zenaton/client.rb', line 125

def find_workflow(workflow_name, custom_id)
  params = "#{ATTR_ID}=#{custom_id}&#{ATTR_NAME}=#{workflow_name}&#{ATTR_PROG}=#{PROG}" # rubocop:disable Metrics/LineLength
  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)


101
102
103
# File 'lib/zenaton/client.rb', line 101

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)


109
110
111
# File 'lib/zenaton/client.rb', line 109

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)


117
118
119
# File 'lib/zenaton/client.rb', line 117

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)


142
143
144
145
146
147
148
149
150
151
# File 'lib/zenaton/client.rb', line 142

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))
  }
  @http.post(send_event_url, body)
end

#start_workflow(flow) ⇒ Object

Start the specified workflow

Parameters:



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

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 (String) (defaults to: '')

    url encoded parameters to include in request

Returns:

  • (String)

    the api url with parameters



78
79
80
81
82
# File 'lib/zenaton/client.rb', line 78

def website_url(resource = '', params = '')
  api_url = ENV['ZENATON_API_URL'] || ZENATON_API_URL
  url = "#{api_url}/#{resource}?#{API_TOKEN}=#{@api_token}&"
  add_app_env(url, params)
end

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

Gets the url for the workers

Parameters:

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

    the endpoint for the worker

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

    url encoded parameters to include in request

Returns:

  • (String)

    the workers url with parameters



67
68
69
70
71
72
# File 'lib/zenaton/client.rb', line 67

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}?"
  add_app_env(url, params)
end