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) ⇒ Task

Returns a new instance of Task.



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

def initialize(public_key, secret_key)
  super(public_key, secret_key)
  response = perform_create_request
  self.worker_server = 'https://' + response.body['server']
  self.task_id       = response.body['task']

  # Assign default values
  self.ignore_errors    = true
  self.ignore_password  = true
  self.try_pdf_repair   = true
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)


31
32
33
34
35
36
# File 'lib/ilovepdf/task.rb', line 31

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



38
39
40
41
42
# File 'lib/ilovepdf/task.rb', line 38

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

#assign_meta_value(key, value) ⇒ Object



23
24
25
# File 'lib/ilovepdf/task.rb', line 23

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

#blobObject



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

def blob
  download_file
  download_info.output_file
end

#delete!Object



79
80
81
# File 'lib/ilovepdf/task.rb', line 79

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

#delete_file(file) ⇒ Object

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ilovepdf/task.rb', line 83

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ilovepdf/task.rb', line 44

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



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

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

#enable_file_encryption(enable, new_encrypt_key = nil) ⇒ Object

Raises:



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

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



75
76
77
# File 'lib/ilovepdf/task.rb', line 75

def execute
  @result = perform_process_request
end

#filesObject



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

def files
  @files ||= []
end

#statusObject

API Methods

Actions on task



71
72
73
# File 'lib/ilovepdf/task.rb', line 71

def status
  http_response = query_task_status(worker_server,task_id)
end