Class: Ilovepdf::Task

Inherits:
Ilovepdf show all
Defined in:
lib/ilovepdf/task.rb

Constant Summary collapse

API_PARAMS =
[]
DOWNLOAD_INFO =
[:output_filename, :output_file, :output_filetype]

Constants included from Ilovepdf

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ilovepdf

root

Constructor Details

#initialize(public_key, secret_key, make_start = false) ⇒ Task

Returns a new instance of Task.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ilovepdf/task.rb', line 11

def initialize(public_key, secret_key, make_start=false)
  super(public_key, secret_key)
  # Assign default values
  self.ignore_errors    = true
  self.ignore_password  = true
  self.try_pdf_repair   = true

  @chained_task = (make_start == false)
  if make_start
    response = perform_start_request
    self.worker_server = "#{Servers::PROTOCOL}://" + response.body['server']
    self.task_id       = response.body['task']
  end
end

Instance Attribute Details

#ignore_errorsObject

Returns the value of attribute ignore_errors.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def ignore_errors
  @ignore_errors
end

#ignore_passwordObject

Returns the value of attribute ignore_password.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def ignore_password
  @ignore_password
end

#output_filenameObject

Returns the value of attribute output_filename.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def output_filename
  @output_filename
end

#packaged_filenameObject

Returns the value of attribute packaged_filename.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def packaged_filename
  @packaged_filename
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/ilovepdf/task.rb', line 6

def result
  @result
end

#task_idObject

Returns the value of attribute task_id.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def task_id
  @task_id
end

#toolObject

Returns the value of attribute tool.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def tool
  @tool
end

#try_pdf_repairObject

Returns the value of attribute try_pdf_repair.



3
4
5
# File 'lib/ilovepdf/task.rb', line 3

def try_pdf_repair
  @try_pdf_repair
end

Instance Method Details

#add_file(filepath) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/ilovepdf/task.rb', line 65

def add_file(filepath)
  raise ArgumentError.new("No file exists in '#{filepath}'") unless ::File.exist?(filepath)
  file = perform_upload_request(filepath)
  files << file
  files.last
end

#add_file_from_url(url) ⇒ Object



72
73
74
75
76
# File 'lib/ilovepdf/task.rb', line 72

def add_file_from_url(url)
  file = perform_upload_url_request(url)
  files << file
  files.last
end

#assign_meta_value(key, value) ⇒ Object



57
58
59
# File 'lib/ilovepdf/task.rb', line 57

def assign_meta_value(key, value)
  meta_values[key] = value
end

#blobObject



94
95
96
97
# File 'lib/ilovepdf/task.rb', line 94

def blob
  download_file
  download_info.output_file
end

#chained_task?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ilovepdf/task.rb', line 26

def chained_task?
  @chained_task
end

#delete!Object



113
114
115
# File 'lib/ilovepdf/task.rb', line 113

def delete!
  send_request('delete', 'task/' + self.task_id)
end

#delete_file(file) ⇒ Object

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ilovepdf/task.rb', line 117

def delete_file(file)
  raise Error.new("File was already deleted") if file.deleted?
  file_was_found = files.find{|f| f.server_filename == file.server_filename }
  raise Error.new("File to delete not found") if !file_was_found
  response = perform_deletefile_request(file)
  if response.success?
    file.mark_as_deleted
    new_files = files.reject{|f| f.server_filename == file.server_filename }
    send(:files=, new_files)
  else
    raise ApiError.new(response, custom_msg: "No error ocurred but response was not successful when deleting the desired file")
  end
  true
end

#download(path = nil, create_directory: false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ilovepdf/task.rb', line 78

def download(path=nil, create_directory: false)
  download_file

  if path
    path = Pathname.new(path).to_s if path.is_a?(Pathname)
    path.chop! if path.end_with? '/'
  else
    path = '.'
  end

  destination = "#{path}/#{download_info.output_filename}"
  FileUtils.mkdir_p(path) if create_directory
  ::File.open(destination, 'wb'){|file| file.write(download_info.output_file) }
  true
end

#download_infoObject



99
100
101
# File 'lib/ilovepdf/task.rb', line 99

def download_info
  @download_info ||= Struct.new(*DOWNLOAD_INFO).new
end

#enable_file_encryption(enable, new_encrypt_key = nil) ⇒ Object

Raises:



132
133
134
135
# File 'lib/ilovepdf/task.rb', line 132

def enable_file_encryption(enable, new_encrypt_key = nil)
  raise Error.new("Encryption mode cannot be assigned after uploading the files") if files.size > 0
  super(enable, new_encrypt_key)
end

#executeObject



109
110
111
# File 'lib/ilovepdf/task.rb', line 109

def execute
  @result = perform_process_request
end

#filesObject



61
62
63
# File 'lib/ilovepdf/task.rb', line 61

def files
  @files ||= []
end

#next(next_tool) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ilovepdf/task.rb', line 30

def next(next_tool)
  body = {
    v: API_VERSION,
    task: self.task_id,
    tool: next_tool,
  }
  extracted_body = RequestPayload::FormUrlEncoded.new(body).extract_to_s

  begin
    response = send_request('post', "task/next", body: extracted_body)
    no_task_present = !response.body.key?('task') || response.body['task'].to_s.empty?
    raise StartError.new(response, custom_msg: "No task assigned on chained start") if no_task_present
  rescue ApiError => e
    raise StartError.new(e, custom_msg: "Error on start chained task")
  end

  next_task = self.new_task(next_tool)
  next_task.send(:"worker_server=", worker_server)
  next_task.send(:"task_id=", response.body['task'])

  response.body['files'].each do |server_filename, filename|
    next_task.files << File.new(server_filename, filename)
  end

  next_task
end

#statusObject

API Methods

Actions on task



105
106
107
# File 'lib/ilovepdf/task.rb', line 105

def status
  http_response = query_task_status(worker_server,task_id)
end