Class: VcoWorkflows::WorkflowService

Inherits:
Object
  • Object
show all
Defined in:
lib/vcoworkflows/workflowservice.rb

Overview

WorkflowService is the object which acts as the interface to the vCO API, and is loosely modeled from the vCO API documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ VcoWorkflows::WorkflowService

Create a new WorkflowService

Parameters:



23
24
25
# File 'lib/vcoworkflows/workflowservice.rb', line 23

def initialize(session)
  @session = session
end

Instance Attribute Details

#sessionVcoWorkflows::VcoSession (readonly)

The VcoSession used by this service



16
17
18
# File 'lib/vcoworkflows/workflowservice.rb', line 16

def session
  @session
end

Instance Method Details

#execute_workflow(id, parameter_json) ⇒ String

Submit the given workflow for execution

Parameters:

  • id (String)

    Workflow GUID for the workflow we want to execute

  • parameter_json (String)

    JSON document of input parameters

Returns:

  • (String)

    Execution ID



103
104
105
106
107
108
109
110
111
# File 'lib/vcoworkflows/workflowservice.rb', line 103

def execute_workflow(id, parameter_json)
  path = "/workflows/#{id}/executions/"
  response = @session.post(path, parameter_json)
  # Execution ID is the final component in the Location header URL, so
  # chop off the front, then pull off any trailing /
  # rubocop:disable LineLength
  response.headers[:location].gsub(%r{^.*/executions/}, '').gsub(%r{\/$}, '')
  # rubocop:enable LineLength
end

#get_execution(workflow_id, execution_id) ⇒ String

Get a WorkflowToken for the requested workflow_id and execution_id

Parameters:

  • workflow_id (String)

    Workflow GUID

  • execution_id (String)

    Execution GUID

Returns:

  • (String)

    JSON document for workflow token



67
68
69
70
# File 'lib/vcoworkflows/workflowservice.rb', line 67

def get_execution(workflow_id, execution_id)
  path = "/workflows/#{workflow_id}/executions/#{execution_id}"
  @session.get(path).body
end

#get_execution_list(workflow_id) ⇒ Hash

Get a list of executions for the given workflow GUID

Parameters:

  • workflow_id (String)

    Workflow GUID

Returns:

  • (Hash)

    workflow executions, keyed by execution ID



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vcoworkflows/workflowservice.rb', line 75

def get_execution_list(workflow_id)
  path = "/workflows/#{workflow_id}/executions/"
  relations = JSON.parse(@session.get(path).body)['relations']
  # The first two elements of the relations['link'] array are URLS,
  # so scrap them. Everything else is an execution.
  executions = {}
  relations['link'].each do |link|
    next unless link.key?('attributes')
    attributes = {}
    link['attributes'].each { |a| attributes[a['name']] = a['value'] }
    executions[attributes['id']] = attributes
  end
  executions
end

#get_log(workflow_id, execution_id) ⇒ String

Get the log for a specific execution

Parameters:

  • workflow_id (String)

    Workflow GUID

  • execution_id (String)

    Workflow execution ID

Returns:

  • (String)

    JSON log document



94
95
96
97
# File 'lib/vcoworkflows/workflowservice.rb', line 94

def get_log(workflow_id, execution_id)
  path = "/workflows/#{workflow_id}/executions/#{execution_id}/logs/"
  @session.get(path).body
end

#get_presentation(workflow_id) ⇒ String

Get the presentation for the given workflow GUID

Parameters:

  • workflow_id (String)

    workflow GUID

Returns:

  • (String)

    JSON document representation of Workflow Presentation



38
39
40
# File 'lib/vcoworkflows/workflowservice.rb', line 38

def get_presentation(workflow_id)
  @session.get("/workflows/#{workflow_id}/presentation/").body
end

#get_workflow_for_id(id) ⇒ String

Get a workflow by GUID

Parameters:

  • id (String)

    Workflow GUID

Returns:

  • (String)

    the JSON document of the requested workflow



31
32
33
# File 'lib/vcoworkflows/workflowservice.rb', line 31

def get_workflow_for_id(id)
  @session.get("/workflows/#{id}").body
end

#get_workflow_for_name(name) ⇒ String

Get one workflow with a specified name.

Parameters:

  • name (String)

    Name of the workflow

Returns:

  • (String)

    the JSON document of the requested workflow



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vcoworkflows/workflowservice.rb', line 45

def get_workflow_for_name(name)
  path = "/workflows?conditions=name=#{url_encode(name)}"
  response = JSON.parse(@session.get(path).body)

  # barf if we got anything other than a single workflow
  fail(IOError, ERR[:too_many_workflows]) if response['total'] > 1
  fail(IOError, ERR[:no_workflow_found]) if response['total'] == 0

  # yank out the workflow id and name from the result attributes
  workflow_id = nil
  response['link'][0]['attributes'].each do |a|
    workflow_id = a['value'] if a['name'].eql?('id')
  end

  # Get the workflow by GUID
  get_workflow_for_id(workflow_id)
end