Method: WebTranslateIt::TranslationFile#create

Defined in:
lib/web_translate_it/translation_file.rb

#create(http_connection) ⇒ Object

Create a master language file to Web Translate It by performing a POST Request.

Example of implementation:

configuration = WebTranslateIt::Configuration.new
file = TranslationFile.new(nil, file_path, nil, configuration.api_key)
file.create # should respond the HTTP code 201 Created

Note that the request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. This is due to the fact that language file imports are handled by background processing.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/web_translate_it/translation_file.rb', line 151

def create(http_connection) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  success = true
  tries ||= 3
  display = []
  display.push file_path
  display.push "#{StringUtil.checksumify(local_checksum.to_s)}..[     ]"
  if File.exist?(file_path)
    File.open(file_path) do |file|
      params = [['name', file_path]]
      params += [['file', file]]
      request = Net::HTTP::Post.new(api_url_for_create)
      WebTranslateIt::Util.add_fields(request)
      request.set_form params, 'multipart/form-data'
      display.push Util.handle_response(http_connection.request(request))
      puts StringUtil.array_to_columns(display)
    rescue Timeout::Error
      puts StringUtil.failure('Request timeout. Will retry in 5 seconds.')
      if (tries -= 1).positive?
        sleep(5)
        retry
      else
        success = false
      end
    rescue StandardError
      display.push StringUtil.failure("An error occured: #{$ERROR_INFO}")
      success = false
    end
  else
    puts StringUtil.failure("\nFile #{file_path} doesn't exist locally!")
  end
  success
end