Class: WebTranslateIt::TranslationFile
- Inherits:
-
Object
- Object
- WebTranslateIt::TranslationFile
- Defined in:
- lib/web_translate_it/translation_file.rb
Overview
A TranslationFile is the representation of a master language file on Web Translate It.
This class allows to manipulate TranslationFiles, more specifically upload and download them. If you pass a Locale to the master language file you will be able to manipulate a target language file.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#id ⇒ Object
Returns the value of attribute id.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#master_id ⇒ Object
Returns the value of attribute master_id.
-
#remote_checksum ⇒ Object
Returns the value of attribute remote_checksum.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
-
#create(http_connection, low_priority = false) ⇒ Object
Create a master language file to Web Translate It by performing a POST Request.
-
#delete(http_connection) ⇒ Object
Delete a master language file from Web Translate It by performing a DELETE Request.
- #exists? ⇒ Boolean
-
#fetch(http_connection, force = false) ⇒ Object
Fetch a language file.
-
#initialize(id, file_path, locale, api_key, updated_at = nil, remote_checksum = "", master_id = nil) ⇒ TranslationFile
constructor
A new instance of TranslationFile.
- #modified_remotely? ⇒ Boolean
-
#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.
Constructor Details
#initialize(id, file_path, locale, api_key, updated_at = nil, remote_checksum = "", master_id = nil) ⇒ TranslationFile
Returns a new instance of TranslationFile.
17 18 19 20 21 22 23 24 25 |
# File 'lib/web_translate_it/translation_file.rb', line 17 def initialize(id, file_path, locale, api_key, updated_at = nil, remote_checksum = "", master_id = nil) self.id = id self.file_path = file_path self.locale = locale self.api_key = api_key self.updated_at = updated_at self.remote_checksum = remote_checksum self.master_id = master_id end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def api_key @api_key end |
#file_path ⇒ Object
Returns the value of attribute file_path.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def file_path @file_path end |
#id ⇒ Object
Returns the value of attribute id.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def id @id end |
#locale ⇒ Object
Returns the value of attribute locale.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def locale @locale end |
#master_id ⇒ Object
Returns the value of attribute master_id.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def master_id @master_id end |
#remote_checksum ⇒ Object
Returns the value of attribute remote_checksum.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def remote_checksum @remote_checksum end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
15 16 17 |
# File 'lib/web_translate_it/translation_file.rb', line 15 def updated_at @updated_at end |
Instance Method Details
#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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/web_translate_it/translation_file.rb', line 116 def create(http_connection, low_priority=false) 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 }) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) 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.") sleep(5) retry end end else puts StringUtil.failure("\nFile #{self.file_path} doesn't exist!") end end |
#delete(http_connection) ⇒ Object
Delete a master language file from Web Translate It by performing a DELETE Request.
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 141 def delete(http_connection) 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::Delete.new(api_url_for_delete) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) 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.") sleep(5) retry end end else puts StringUtil.failure("\nFile #{self.file_path} doesn't exist!") end end |
#exists? ⇒ Boolean
164 165 166 |
# File 'lib/web_translate_it/translation_file.rb', line 164 def exists? File.exists?(file_path) end |
#fetch(http_connection, force = false) ⇒ Object
Fetch a language file. By default it will make a conditional GET Request, using the ‘If-Modified-Since` tag. You can force the method to re-download your file by passing `true` as a second argument
Example of implementation:
configuration = WebTranslateIt::Configuration.new
file = configuration.files.first
file.fetch # the first time, will return the content of the language file with a status 200 OK
file.fetch # returns nothing, with a status 304 Not Modified
file.fetch(true) # force to re-download the file, will return the content of the file with a 200 OK
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/web_translate_it/translation_file.rb', line 39 def fetch(http_connection, force = false) display = [] display.push(self.file_path) display.push "#{StringUtil.checksumify(self.local_checksum.to_s)}..#{StringUtil.checksumify(self.remote_checksum.to_s)}" if !File.exist?(self.file_path) or force or self.remote_checksum != self.local_checksum begin request = Net::HTTP::Get.new(api_url) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) FileUtils.mkpath(self.file_path.split('/')[0..-2].join('/')) unless File.exist?(self.file_path) or self.file_path.split('/')[0..-2].join('/') == "" begin response = http_connection.request(request) File.open(self.file_path, 'wb'){ |file| file << response.body } if response.code.to_i == 200 and response.body != '' display.push Util.handle_response(response) rescue Timeout::Error puts StringUtil.failure("Request timeout. Will retry in 5 seconds.") sleep(5) retry rescue display.push StringUtil.failure("An error occured: #{$!}") end end else display.push StringUtil.success("Skipped") end print ArrayUtil.to_columns(display) end |
#modified_remotely? ⇒ Boolean
168 169 170 |
# File 'lib/web_translate_it/translation_file.rb', line 168 def modified_remotely? fetch == "200 OK" end |
#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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/web_translate_it/translation_file.rb', line 78 def upload(http_connection, merge=false, ignore_missing=false, label=nil, low_priority=false, minor_changes=false, force=false) 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 }) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) display.push Util.handle_response(http_connection.request(request)) rescue Timeout::Error puts StringUtil.failure("Request timeout. Will retry in 5 seconds.") sleep(5) retry 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 end |