Class: DeepL::Requests::Document::Upload

Inherits:
Base
  • Object
show all
Defined in:
lib/deepl/requests/document/upload.rb

Constant Summary collapse

SUPPORTED_OPTIONS =
%w[formality glossary_id output_format].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#api, #options, #response

Instance Method Summary collapse

Constructor Details

#initialize(api, input_file_path, source_lang, target_lang, filename = nil, options = {}, additional_headers = {}) ⇒ Upload

rubocop:disable Metrics/ParameterLists



14
15
16
17
18
19
20
21
# File 'lib/deepl/requests/document/upload.rb', line 14

def initialize(api, input_file_path, source_lang, target_lang, filename = nil, # rubocop:disable Metrics/ParameterLists
               options = {}, additional_headers = {})
  super(api, options, additional_headers)
  @input_file_path = input_file_path
  @source_lang = source_lang
  @target_lang = target_lang
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/deepl/requests/document/upload.rb', line 10

def filename
  @filename
end

#input_file_pathObject (readonly)

Returns the value of attribute input_file_path.



10
11
12
# File 'lib/deepl/requests/document/upload.rb', line 10

def input_file_path
  @input_file_path
end

#source_langObject (readonly)

Returns the value of attribute source_lang.



10
11
12
# File 'lib/deepl/requests/document/upload.rb', line 10

def source_lang
  @source_lang
end

#target_langObject (readonly)

Returns the value of attribute target_lang.



10
11
12
# File 'lib/deepl/requests/document/upload.rb', line 10

def target_lang
  @target_lang
end

Instance Method Details

#detailsObject



39
40
41
42
43
44
# File 'lib/deepl/requests/document/upload.rb', line 39

def details
  "HTTP Headers: #{headers}\nPayload #{[
    ['file', "File at #{input_file_path} opened in binary mode"],
    ['source_lang', source_lang], ['target_lang', target_lang], ['filename', filename]
  ]}"
end

#requestObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/deepl/requests/document/upload.rb', line 23

def request # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  input_file = File.open(input_file_path, 'rb')
  form_data = [
    ['file', input_file], ['source_lang', source_lang],
    ['target_lang', target_lang]
  ]
  filename_param = filename || File.basename(input_file_path)
  form_data.push(['filename', filename_param]) unless filename_param.nil?
  # Manually add options due to multipart/form-data request
  SUPPORTED_OPTIONS.each do |option|
    form_data.push([option, options[option]]) unless options[option].nil?
  end
  build_doc_handle(*execute_request_with_retries(post_request_with_file(form_data),
                                                 [input_file]))
end

#to_sObject



46
47
48
# File 'lib/deepl/requests/document/upload.rb', line 46

def to_s
  "POST #{uri.request_uri}"
end