Method: WebTranslateIt::TranslationFile#upload

Defined in:
lib/web_translate_it/translation_file.rb

#upload(http_connection, merge = false, ignore_missing = false, label = nil, low_priority = false, minor_changes = false, force = false) ⇒ Object

Update a language file to Web Translate It by performing a PUT Request.

Example of implementation:

configuration = WebTranslateIt::Configuration.new
locale = configuration.locales.first
file = configuration.files.first
file.upload # should respond the HTTP code 202 Accepted

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/web_translate_it/translation_file.rb', line 85

def upload(http_connection, merge=false, ignore_missing=false, label=nil, low_priority=false, minor_changes=false, force=false)
  success = true
  tries ||= 3
  display = []
  display.push(self.file_path)
  display.push "#{StringUtil.checksumify(self.local_checksum.to_s)}..#{StringUtil.checksumify(self.remote_checksum.to_s)}"
  if File.exists?(self.file_path)
   if force or self.remote_checksum != self.local_checksum
      File.open(self.file_path) do |file|
        begin
          request = Net::HTTP::Put::Multipart.new(api_url, {"file" => UploadIO.new(file, "text/plain", file.path), "merge" => merge, "ignore_missing" => ignore_missing, "label" => label, "low_priority" => low_priority, "minor_changes" => minor_changes })
          WebTranslateIt::Util.add_fields(request)
          display.push Util.handle_response(http_connection.request(request))
        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
      display.push StringUtil.success("Skipped")
    end
    puts ArrayUtil.to_columns(display)
  else
    puts StringUtil.failure("Can't push #{self.file_path}. File doesn't exist.")
  end
  return success
end