Class: OCRSDK::Promise

Inherits:
AbstractEntity show all
Includes:
Verifiers::Status
Defined in:
lib/ocrsdk/promise.rb

Constant Summary

Constants included from Verifiers::Status

Verifiers::Status::STATUSES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Verifiers::Status

#status_to_s, #status_to_sym, #supported_status?

Constructor Details

#initialize(task_id) ⇒ Promise

Returns a new instance of Promise.



10
11
12
13
# File 'lib/ocrsdk/promise.rb', line 10

def initialize(task_id)
  super()
  @task_id = task_id
end

Instance Attribute Details

#estimate_processing_timeObject (readonly)

Returns the value of attribute estimate_processing_time.



4
5
6
# File 'lib/ocrsdk/promise.rb', line 4

def estimate_processing_time
  @estimate_processing_time
end

#result_urlObject (readonly)

Returns the value of attribute result_url.



4
5
6
# File 'lib/ocrsdk/promise.rb', line 4

def result_url
  @result_url
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/ocrsdk/promise.rb', line 4

def status
  @status
end

#task_idObject (readonly)

Returns the value of attribute task_id.



4
5
6
# File 'lib/ocrsdk/promise.rb', line 4

def task_id
  @task_id
end

Class Method Details

.from_response(xml_string) ⇒ Object



6
7
8
# File 'lib/ocrsdk/promise.rb', line 6

def self.from_response(xml_string)
  OCRSDK::Promise.new(nil).parse_response xml_string
end

Instance Method Details

#completed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ocrsdk/promise.rb', line 45

def completed?
  @status == :completed
end

#estimate_completionObject



15
16
17
# File 'lib/ocrsdk/promise.rb', line 15

def estimate_completion
  @registration_time + @estimate_processing_time.seconds
end

#failed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ocrsdk/promise.rb', line 49

def failed?
  [:processing_failed, :deleted, :not_enough_credits].include? @status
end

#parse_response(xml_string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ocrsdk/promise.rb', line 19

def parse_response(xml_string)
  xml = Nokogiri::XML.parse xml_string
  begin
    task = xml.xpath('/response/task').first
    @task_id = task['id']
  rescue NoMethodError # if Nokogiri can't find root node
    raise OCRSDK::OCRSDKError, "Problem parsing provided xml string: #{xml_string}"
  end

  @status     = status_to_sym task['status']
  @result_url = task['resultUrl']
  @registration_time        = DateTime.parse task['registrationTime']    
  @estimate_processing_time = task['estimatedProcessingTime'].to_i

  # admin should be notified in this case
  raise OCRSDK::NotEnoughCredits  if @status == :not_enough_credits

  self
end

#processing?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ocrsdk/promise.rb', line 53

def processing?
  [:submitted, :queued, :in_progress].include? @status
end

#result(retry_sleep = OCRSDK.config.retry_wait_time) ⇒ Object



57
58
59
60
61
62
# File 'lib/ocrsdk/promise.rb', line 57

def result(retry_sleep=OCRSDK.config.retry_wait_time)
  raise OCRSDK::ProcessingFailed  if failed?
  retryable tries: OCRSDK.config.number_or_retries, on: OCRSDK::NetworkError, sleep: retry_sleep do
    api_get_result
  end
end

#update(retry_sleep = OCRSDK.config.retry_wait_time) ⇒ Object



39
40
41
42
43
# File 'lib/ocrsdk/promise.rb', line 39

def update(retry_sleep=OCRSDK.config.retry_wait_time)
  retryable tries: OCRSDK.config.number_or_retries, on: OCRSDK::NetworkError, sleep: retry_sleep do
    parse_response api_update_status
  end
end

#wait(seconds = OCRSDK.config.default_poll_time) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/ocrsdk/promise.rb', line 64

def wait(seconds=OCRSDK.config.default_poll_time)
  while processing? do
    sleep seconds
    update
  end

  self
end