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

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/parsing/v2/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

Returns a new instance of Job.

Parameters:

  • server_response (Hash)

    Parsed JSON payload from the API.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mindee/parsing/v2/job.rb', line 34

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'])
  @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.



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

def alias
  @alias
end

#created_atDateTime? (readonly)

Returns Timestamp of creation.

Returns:

  • (DateTime, nil)

    Timestamp of creation.



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

def created_at
  @created_at
end

#errorErrorResponse? (readonly)

Returns Error details when the job failed.

Returns:



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

def error
  @error
end

#filenameString (readonly)

Returns Name of the processed file.

Returns:

  • (String)

    Name of the processed file.



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

def filename
  @filename
end

#idString (readonly)

Returns Unique job identifier.

Returns:

  • (String)

    Unique job identifier.



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

def id
  @id
end

#model_idString (readonly)

Returns Identifier of the model used.

Returns:

  • (String)

    Identifier of the model used.



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

def model_id
  @model_id
end

#polling_urlString (readonly)

Returns URL to query for updated status.

Returns:

  • (String)

    URL to query for updated status.



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

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.



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

def result_url
  @result_url
end

#statusString? (readonly)

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

Returns:

  • (String, nil)

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



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

def status
  @status
end

#webhooksArray<JobWebhook> (readonly)

Returns Webhooks triggered by the job.

Returns:

  • (Array<JobWebhook>)

    Webhooks triggered by the job.



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

def webhooks
  @webhooks
end

Instance Method Details

#to_sString

String representation.

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mindee/parsing/v2/job.rb', line 56

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.map(&:to_s).join("\n\n"),
    ]
  end

  lines.join("\n")
end