Class: Mindee::V2::Parsing::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/v2/parsing/job.rb

Overview

Metadata returned when polling a job (asynchronous request).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_response) ⇒ Job

rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • server_response (Hash)

    Parsed JSON payload from the API.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mindee/v2/parsing/job.rb', line 37

def initialize(server_response)
  raise ArgumentError, 'server_response must be a Hash' unless server_response.is_a?(Hash)

  @id          = server_response['id']
  @status      = server_response['status'] if server_response.key?('status')
  unless server_response['error'].nil? || server_response['error'].empty?
    @error = ErrorResponse.new(server_response['error'])
  end
  @created_at = Time.iso8601(server_response['created_at'])
  if server_response.key?('completed_at') && !server_response['completed_at'].nil?
    @completed_at = Time.iso8601(server_response['completed_at'])
  end
  @model_id    = server_response['model_id']
  @polling_url = server_response['polling_url']
  @filename    = server_response['filename']
  @result_url  = server_response['result_url']
  @alias       = server_response['alias']
  @webhooks = []
  server_response['webhooks'].each do |webhook|
    @webhooks.push JobWebhook.new(webhook)
  end
end

Instance Attribute Details

#aliasString (readonly)

Returns Optional alias for the file.

Returns:

  • (String)

    Optional alias for the file.



23
24
25
# File 'lib/mindee/v2/parsing/job.rb', line 23

def alias
  @alias
end

#completed_atTime? (readonly)

Returns Date and time of the Job completion. Filled once processing is finished.

Returns:

  • (Time, nil)

    Date and time of the Job completion. Filled once processing is finished.



17
18
19
# File 'lib/mindee/v2/parsing/job.rb', line 17

def completed_at
  @completed_at
end

#created_atTime (readonly)

Returns Date and time of the Job creation.

Returns:

  • (Time)

    Date and time of the Job creation.



15
16
17
# File 'lib/mindee/v2/parsing/job.rb', line 15

def created_at
  @created_at
end

#errorErrorResponse? (readonly)

Returns Error details when the job failed.

Returns:



33
34
35
# File 'lib/mindee/v2/parsing/job.rb', line 33

def error
  @error
end

#filenameString (readonly)

Returns Name of the processed file.

Returns:

  • (String)

    Name of the processed file.



21
22
23
# File 'lib/mindee/v2/parsing/job.rb', line 21

def filename
  @filename
end

#idString (readonly)

Returns Unique job identifier.

Returns:

  • (String)

    Unique job identifier.



13
14
15
# File 'lib/mindee/v2/parsing/job.rb', line 13

def id
  @id
end

#model_idString (readonly)

Returns Identifier of the model used.

Returns:

  • (String)

    Identifier of the model used.



19
20
21
# File 'lib/mindee/v2/parsing/job.rb', line 19

def model_id
  @model_id
end

#polling_urlString (readonly)

Returns URL to query for updated status.

Returns:

  • (String)

    URL to query for updated status.



27
28
29
# File 'lib/mindee/v2/parsing/job.rb', line 27

def polling_url
  @polling_url
end

#result_urlString? (readonly)

Returns URL that redirects to the final result once ready.

Returns:

  • (String, nil)

    URL that redirects to the final result once ready.



29
30
31
# File 'lib/mindee/v2/parsing/job.rb', line 29

def result_url
  @result_url
end

#statusString? (readonly)

Returns Current status (submitted, processing, done, …).

Returns:

  • (String, nil)

    Current status (submitted, processing, done, …).



25
26
27
# File 'lib/mindee/v2/parsing/job.rb', line 25

def status
  @status
end

#webhooksArray<JobWebhook> (readonly)

Returns Webhooks triggered by the job.

Returns:

  • (Array<JobWebhook>)

    Webhooks triggered by the job.



31
32
33
# File 'lib/mindee/v2/parsing/job.rb', line 31

def webhooks
  @webhooks
end

Instance Method Details

#to_sString

String representation.

Returns:

  • (String)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mindee/v2/parsing/job.rb', line 63

def to_s
  lines = [
    'Job',
    '###',
    ":ID: #{@id}",
    ":CreatedAt: #{@created_at}",
    ":ModelID: #{@model_id}",
    ":Filename: #{@filename}",
    ":Alias: #{@alias}",
    ":Status: #{@status}",
    ":PollingURL: #{@polling_url}",
    ":ResultURL: #{@result_url}",
  ]

  lines << @error.to_s if @error

  unless @webhooks.empty?
    lines += [
      '',
      'Webhooks',
      '=========',
      @webhooks.join("\n\n"),
    ]
  end

  lines.join("\n")
end