Sha256: 96fd6ddf05b71c9b35779bfb684891450ab6126760d728e338c7f31adcbeb567

Contents?: true

Size: 1.65 KB

Versions: 88

Compression:

Stored size: 1.65 KB

Contents

module Translator
  module Smartling
    module API
      class UploadFile
        include Base

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

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

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

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

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

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

        def to_s
          return_value
        end

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

        def cleanup
          file.unlink
        end

        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
      end
    end
  end
end

Version data entries

88 entries across 88 versions & 1 rubygems

Version Path
station-0.5.16 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.15 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.14 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.13 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.12 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.11 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.10 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.9 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.8 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.7 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.6 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.5 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.4 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.3 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.2 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.1 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.5.0 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.4.9 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.4.8 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb
station-0.4.7 lib/nexmo_developer/app/services/translator/smartling/api/upload_file.rb