Method: WebTranslateIt::TranslationFile#create

Defined in:
lib/web_translate_it/translation_file.rb

#create(http_connection, low_priority = false) ⇒ 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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/web_translate_it/translation_file.rb', line 132

def create(http_connection, low_priority=false)
  success = true
  tries ||= 3
  display = []
  display.push file_path
  display.push "#{StringUtil.checksumify(self.local_checksum.to_s)}..[     ]"
  if File.exists?(self.file_path)
    File.open(self.file_path) do |file|
      begin
        request = Net::HTTP::Post::Multipart.new(api_url_for_create, { "name" => self.file_path, "file" => UploadIO.new(file, "text/plain", file.path), "low_priority" => low_priority })
        WebTranslateIt::Util.add_fields(request)
        display.push Util.handle_response(http_connection.request(request))
        puts ArrayUtil.to_columns(display)
      rescue Timeout::Error
        puts StringUtil.failure("Request timeout. Will retry in 5 seconds.")
        if (tries -= 1) > 0
          sleep(5)
          retry
        else
          success = false
        end
      rescue
        display.push StringUtil.failure("An error occured: #{$!}")
        success = false
      end
    end
  else
    puts StringUtil.failure("\nFile #{self.file_path} doesn't exist!")
  end
  return success
end