Sha256: 58b3d8eda8f123dc77cd4aeec982066f09e901492376d07de8e01a4caaeaa771
Contents?: true
Size: 1.56 KB
Versions: 9
Compression:
Stored size: 1.56 KB
Contents
module Scrivito class Task def self.find(id) new(id: id) end attr_reader :id def initialize(params = nil) return update(params) if params response = yield if params = extract_params(response) update(params) else update(status: 'success', result: response) end rescue ClientError => e update(status: 'error', message: e.message, code: e.backend_code) end def open? @status == 'open' end def success? @status == 'success' end def error? @status == 'error' end def result case when success? then @result when error? then raise ClientError.new(@message, 400, @code) else poll result end end private def extract_params(response) if response.is_a?(Hash) && response.keys == ['task'] && response['task'].is_a?(Hash) response['task'] end end def reload task_data = CmsRestApi.task_unaware_request(:get, "tasks/#{@id}") update(task_data) end def update(task_data) task_data = task_data.with_indifferent_access @id = task_data[:id] if task_data[:id] @status = task_data[:status] if task_data[:status] @result = task_data[:result] if task_data[:result] @message = task_data[:message] if task_data[:message] @code = task_data[:code] if task_data[:code] reload unless @status end def poll loop do sleep 2 reload break unless open? end end end end
Version data entries
9 entries across 9 versions & 1 rubygems