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:



21
22
23
# File 'lib/vcoworkflows/workflowservice.rb', line 21

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



101
102
103
104
105
106
107
# File 'lib/vcoworkflows/workflowservice.rb', line 101

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 /
  response.headers[:location].gsub(%r{^.*/executions/}, '').gsub(%r{\/$}, '')
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



65
66
67
68
# File 'lib/vcoworkflows/workflowservice.rb', line 65

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



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

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



92
93
94
95
# File 'lib/vcoworkflows/workflowservice.rb', line 92

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



36
37
38
# File 'lib/vcoworkflows/workflowservice.rb', line 36

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



29
30
31
# File 'lib/vcoworkflows/workflowservice.rb', line 29

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

Raises:

  • (IOError)


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

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
  raise(IOError, ERR[:too_many_workflows]) if response['total'] > 1
  raise(IOError, ERR[:no_workflow_found]) if response['total'].zero?

  # 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