Class: CopyleaksApi::CopyleaksProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/copyleaks_api/copyleaks_process.rb

Constant Summary collapse

STATUSES =
['processing', 'ready', 'allocated', 'finished', 'error', 'deleted'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CopyleaksProcess

constructor



10
11
12
13
14
15
16
17
18
# File 'lib/copyleaks_api/copyleaks_process.rb', line 10

def initialize(options)
  @cloud = options[:cloud]
  [:cloud, :process_id, :custom_fields, :result, :status, :progress].each do |attr|
    instance_variable_set("@#{attr}", options[attr]) if options[attr]
  end

  @created_at = DateTime.parse(options[:created_at]) if options[:created_at]
  @status = STATUSES[options[:status_code].to_i + 1] if options[:status_code]
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



7
8
9
# File 'lib/copyleaks_api/copyleaks_process.rb', line 7

def created_at
  @created_at
end

#custom_fieldsObject

Returns the value of attribute custom_fields.



7
8
9
# File 'lib/copyleaks_api/copyleaks_process.rb', line 7

def custom_fields
  @custom_fields
end

#process_idObject

Returns the value of attribute process_id.



7
8
9
# File 'lib/copyleaks_api/copyleaks_process.rb', line 7

def process_id
  @process_id
end

#progressObject

Returns the value of attribute progress.



7
8
9
# File 'lib/copyleaks_api/copyleaks_process.rb', line 7

def progress
  @progress
end

#statusObject

returns status information or reload if no data is specified



39
40
41
# File 'lib/copyleaks_api/copyleaks_process.rb', line 39

def status
  @status
end

Class Method Details

.create(cloud, hash) ⇒ Object

create CopyleaksProcess based on data got from any create endpoint



61
62
63
# File 'lib/copyleaks_api/copyleaks_process.rb', line 61

def create(cloud, hash)
  new(cloud: cloud, process_id: hash['ProcessId'], created_at: hash['CreationTimeUTC'])
end

.create_from_list(cloud, hash) ⇒ Object

creates CopyleaksProcess based on data got from list endpoint



76
77
78
79
# File 'lib/copyleaks_api/copyleaks_process.rb', line 76

def create_from_list(cloud, hash)
  new(cloud: cloud, process_id: hash['ProcessId'], created_at: hash['CreationTimeUTC'],
      status: hash['Status'].downcase, custom_fields: hash['CustomFields'])
end

.create_from_result(cloud, id, result) ⇒ Object

creates CopyleaksProcess based on data got from result endpoint



71
72
73
# File 'lib/copyleaks_api/copyleaks_process.rb', line 71

def create_from_result(cloud, id, result)
  new(cloud: cloud, process_id: id, result: result)
end

.create_from_status(cloud, id, hash) ⇒ Object

create CopyleaksProcess based on data got from status endpoint



66
67
68
# File 'lib/copyleaks_api/copyleaks_process.rb', line 66

def create_from_status(cloud, id, hash)
  new(cloud: cloud, process_id: id, status: hash['Status'].downcase, progress: hash['ProgressPercents'])
end

Instance Method Details

#deleteObject

deletes process from API



45
46
47
48
# File 'lib/copyleaks_api/copyleaks_process.rb', line 45

def delete
  @cloud.delete(process_id)
  @status = 'deleted'
end

#processing?Boolean

returns true if process status means processing data on server side

Returns:

  • (Boolean)


29
30
31
# File 'lib/copyleaks_api/copyleaks_process.rb', line 29

def processing?
  ['ready', 'allocated', 'processing'].include?(@status)
end

#reloadObject

reload object attributes using status endpoint



51
52
53
54
55
56
57
# File 'lib/copyleaks_api/copyleaks_process.rb', line 51

def reload
  response = @cloud.status(process_id, raw: true)
  @status = response['Status'].downcase
  @progress = response['ProgressPercents'].to_i
  @result = nil
  self
end

#resultObject

return result data or retrieves from result endpoint if nothing specified



34
35
36
# File 'lib/copyleaks_api/copyleaks_process.rb', line 34

def result
  @result ||= @cloud.result(process_id, raw: true)
end