Class: Dor::Services::Response::Workflow

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/response/workflow.rb

Overview

The response from asking the server about a workflow for an item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml:) ⇒ Workflow

Returns a new instance of Workflow.

Parameters:

  • xml (String)

    An XML string representing a given workflow



9
10
11
# File 'lib/dor/services/response/workflow.rb', line 9

def initialize(xml:)
  @xml = xml
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



79
80
81
# File 'lib/dor/services/response/workflow.rb', line 79

def xml
  @xml
end

Instance Method Details

#active_for?(version:) ⇒ Boolean

Check if there are any processes for the provided version.

Parameters:

  • version (Integer)

    the version we are checking for.

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/dor/services/response/workflow.rb', line 23

def active_for?(version:)
  result = ng_xml.at_xpath("/workflow/process[@version=#{version}]")
  result ? true : false
end

#complete?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/dor/services/response/workflow.rb', line 61

def complete?
  complete_for?(version: latest_version)
end

#complete_for?(version:) ⇒ Boolean

Check if all processes are skipped or complete for the provided version.

Parameters:

  • version (Integer)

    the version we are checking for.

Returns:

  • (Boolean)


57
58
59
# File 'lib/dor/services/response/workflow.rb', line 57

def complete_for?(version:)
  incomplete_processes_for(version: version).empty?
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dor/services/response/workflow.rb', line 51

def empty?
  ng_xml.xpath('/workflow/process').empty?
end

#error_countObject



28
29
30
31
# File 'lib/dor/services/response/workflow.rb', line 28

def error_count
  process_names.map { |process_name| process_for_recent_version(name: process_name) }
               .count { |process| process.status == 'error' }
end

#incomplete_processesObject



71
72
73
# File 'lib/dor/services/response/workflow.rb', line 71

def incomplete_processes
  incomplete_processes_for(version: latest_version)
end

#incomplete_processes_for(version:) ⇒ Object



65
66
67
68
69
# File 'lib/dor/services/response/workflow.rb', line 65

def incomplete_processes_for(version:)
  process_nodes = ng_xml.xpath("/workflow/process[@version=#{version}]")
  incomplete_process_nodes = process_nodes.reject { |process_node| %w[skipped completed].include?(process_node.attr('status')) }
  incomplete_process_nodes.map { |process_node| to_process(process_node) }
end

#ng_xmlObject



75
76
77
# File 'lib/dor/services/response/workflow.rb', line 75

def ng_xml
  @ng_xml ||= Nokogiri::XML(@xml)
end

#pidObject



13
14
15
# File 'lib/dor/services/response/workflow.rb', line 13

def pid
  workflow['objectId']
end

#process_for(name:, version:) ⇒ Object

Returns the process for the specific version and name:



41
42
43
44
# File 'lib/dor/services/response/workflow.rb', line 41

def process_for(name:, version:)
  node = ng_xml.at_xpath("/workflow/process[@name = '#{name}' and @version = #{version}]")
  to_process(node)
end

#process_for_recent_version(name:) ⇒ Object

Returns the process for the most recent version that matches the given name:



34
35
36
37
38
# File 'lib/dor/services/response/workflow.rb', line 34

def process_for_recent_version(name:)
  nodes = process_nodes_for(name: name)
  node = nodes.max { |a, b| a.attr('version').to_i <=> b.attr('version').to_i }
  to_process(node)
end

#processesArray<String>

Returns a list of all processes

Returns:

  • (Array<String>)

    returns a list of all processes



47
48
49
# File 'lib/dor/services/response/workflow.rb', line 47

def processes
  ng_xml.xpath('/workflow/process').map { |node| to_process(node) }
end

#workflow_nameObject



17
18
19
# File 'lib/dor/services/response/workflow.rb', line 17

def workflow_name
  workflow['id']
end