Class: Translator::Smartling::API::UploadFile

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb

Instance Method Summary collapse

Methods included from Base

#call, #error, included, #make_request!, #response_body

Constructor Details

#initialize(translation_request:, project_id:, batch_id:, token:) ⇒ UploadFile

Returns a new instance of UploadFile.



7
8
9
10
11
12
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 7

def initialize(translation_request:, project_id:, batch_id:, token:)
  @translation_request = translation_request
  @project_id          = project_id
  @batch_id            = batch_id
  @token               = token
end

Instance Method Details

#build_requestObject



14
15
16
17
18
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 14

def build_request
  request = Net::HTTP::Post.new(uri.path, headers)
  request.set_form form_data, 'multipart/form-data'
  request
end

#cleanupObject



49
50
51
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 49

def cleanup
  file.unlink
end

#fileObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 53

def file
  @file ||= begin
    file = Tempfile.new
    file.write(
      Nexmo::Markdown::Pipelines::Smartling::Preprocessor.new.call(
        File.read(@translation_request.file_path)
      )
    )
    file.rewind
    file.close
    file
  end
end

#form_dataObject



20
21
22
23
24
25
26
27
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 20

def form_data
  [
    ['file', File.open(file)],
    ['fileUri', @translation_request.file_uri],
    ['fileType', 'markdown'],
    ['localeIdsToAuthorize[]', @translation_request.locale],
  ]
end

#headersObject



45
46
47
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 45

def headers
  { 'Authorization' => "Bearer #{@token}" }
end

#return_valueObject



37
38
39
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 37

def return_value
  @return_value ||= @translation_request.file_uri
end

#success?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 29

def success?
  @response.code == '202'
end

#to_sObject



41
42
43
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 41

def to_s
  return_value
end

#uriObject



33
34
35
# File 'lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb', line 33

def uri
  @uri ||= URI("https://api.smartling.com/jobs-batch-api/v2/projects/#{@project_id}/batches/#{@batch_id}/file")
end