Class: Anvil::Workflow

Inherits:
Resources::Base show all
Defined in:
lib/anvil/resources/workflow.rb

Instance Attribute Summary

Attributes inherited from Resources::Base

#attributes, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resources::Base

#==, build_from_response, #initialize, #inspect, list, #method_missing, #respond_to_missing?, #to_h, #to_json, with_client

Constructor Details

This class inherits a constructor from Anvil::Resources::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Anvil::Resources::Base

Class Method Details

.create(name:, **options) ⇒ Workflow

Create a new workflow

Parameters:

  • name (String)

    Name of the workflow

  • options (Hash)

    Additional options (forges, casts, slug)

Returns:

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/anvil/resources/workflow.rb', line 79

def create(name:, **options)
  api_key = options.delete(:api_key)
  client = api_key ? Client.new(api_key: api_key) : self.client

  payload = { name: name }
  payload[:slug] = options[:slug] if options[:slug]
  payload[:forges] = options[:forges] if options[:forges]
  payload[:casts] = options[:casts] if options[:casts]

  result = client.graphql(create_weld_mutation, variables: { input: payload })
  raise APIError, "Failed to create workflow: #{result}" unless result && result[:createWeld]

  new(result[:createWeld], client: client)
end

.find(workflow_eid, client: nil) ⇒ Workflow

Find a workflow by EID

Parameters:

  • workflow_eid (String)

    The workflow EID

  • client (Client) (defaults to: nil)

    Optional client instance

Returns:

Raises:



99
100
101
102
103
104
105
106
# File 'lib/anvil/resources/workflow.rb', line 99

def find(workflow_eid, client: nil)
  client ||= self.client

  result = client.graphql(weld_query, variables: { eid: workflow_eid })
  raise NotFoundError, "Workflow not found: #{workflow_eid}" unless result && result[:weld]

  new(result[:weld], client: client)
end

Instance Method Details

#draft?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/anvil/resources/workflow.rb', line 29

def draft?
  status == 'draft'
end

#eidObject



9
10
11
# File 'lib/anvil/resources/workflow.rb', line 9

def eid
  attributes[:eid]
end

#idObject



5
6
7
# File 'lib/anvil/resources/workflow.rb', line 5

def id
  attributes[:eid] || attributes[:id]
end

#nameObject



13
14
15
# File 'lib/anvil/resources/workflow.rb', line 13

def name
  attributes[:name]
end

#published?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/anvil/resources/workflow.rb', line 25

def published?
  status == 'published'
end

#reload!Object

Reload from API



67
68
69
70
71
# File 'lib/anvil/resources/workflow.rb', line 67

def reload!
  refreshed = self.class.find(eid, client: client)
  @attributes = refreshed.attributes
  self
end

#slugObject



21
22
23
# File 'lib/anvil/resources/workflow.rb', line 21

def slug
  attributes[:slug]
end

#start(data: {}) ⇒ Hash

Start the workflow with initial data

Parameters:

  • data (Hash) (defaults to: {})

    The data to start the workflow with

Returns:

  • (Hash)

    The submission data

Raises:



37
38
39
40
41
42
43
44
45
# File 'lib/anvil/resources/workflow.rb', line 37

def start(data: {})
  result = client.graphql(self.class.send(:create_weld_data_mutation), variables: {
                            eid: eid,
                            input: data
                          })
  raise APIError, "Failed to start workflow: #{eid}" unless result && result[:createWeldData]

  result[:createWeldData]
end

#statusObject



17
18
19
# File 'lib/anvil/resources/workflow.rb', line 17

def status
  attributes[:status]
end

#submissions(limit: 10, offset: 0) ⇒ Array<Hash>

Get submissions for this workflow

Parameters:

  • limit (Integer) (defaults to: 10)

    Number of submissions to return

  • offset (Integer) (defaults to: 0)

    Offset for pagination

Returns:

  • (Array<Hash>)

    The workflow submissions



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/anvil/resources/workflow.rb', line 52

def submissions(limit: 10, offset: 0)
  result = client.graphql(self.class.send(:weld_data_query), variables: {
                            eid: eid,
                            limit: limit,
                            offset: offset
                          })

  if result && result[:weldData]
    Array(result[:weldData])
  else
    []
  end
end