Class: Zenaton::Client
- Inherits:
-
Object
- Object
- Zenaton::Client
- 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
-
#api_token ⇒ Object
writeonly
Sets the attribute api_token.
-
#app_env ⇒ Object
writeonly
Sets the attribute app_env.
-
#app_id ⇒ Object
writeonly
Sets the attribute app_id.
Class Method Summary collapse
-
.init(app_id, api_token, app_env) ⇒ Zenaton::Client
Class method that sets the three tokens needed to interact with the API.
Instance Method Summary collapse
-
#find_workflow(workflow_name, custom_id) ⇒ Zenaton::Interfaces::Workflow?
Finds a workflow.
-
#kill_workflow(workflow_name, custom_id) ⇒ NilClass
Stops a workflow.
-
#pause_workflow(workflow_name, custom_id) ⇒ NilClass
Pauses a workflow.
-
#resume_workflow(workflow_name, custom_id) ⇒ NilClass
Resumes a workflow.
-
#send_event(workflow_name, custom_id, event) ⇒ NilClass
Sends an event to a workflow.
-
#start_task(task) ⇒ Object
Start a single task.
-
#start_workflow(flow) ⇒ Object
Start the specified workflow.
-
#website_url(resource = '', params = {}) ⇒ String
Gets the url for zenaton api.
-
#worker_url(resource = '', params = {}) ⇒ String
Gets the url for the workers.
Instance Attribute Details
#api_token=(value) ⇒ Object (writeonly)
Sets the attribute api_token
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
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
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
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
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. =~ /No workflow instance found/ raise exception end |
#kill_workflow(workflow_name, custom_id) ⇒ NilClass
Stops a workflow
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
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
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
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
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
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
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
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 |