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.
Instance Method Summary collapse
-
#fetch(locale, force = false) ⇒ Object
Fetch a language file.
- #file_path_for_directory(locale) ⇒ Object
-
#file_path_for_locale(locale) ⇒ Object
Convenience method which returns the file path of a TranslationFile for a given locale.
- #file_path_for_locale_initial(locale) ⇒ Object
-
#initialize(id, file_path, api_key) ⇒ TranslationFile
constructor
A new instance of TranslationFile.
-
#upload(locale) ⇒ Object
Update a language file to Web Translate It by performing a PUT Request.
Constructor Details
#initialize(id, file_path, api_key) ⇒ TranslationFile
Returns a new instance of TranslationFile.
17 18 19 20 21 |
# File 'lib/web_translate_it/translation_file.rb', line 17 def initialize(id, file_path, api_key) self.id = id self.file_path = file_path self.api_key = api_key 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 |
Instance Method Details
#fetch(locale, 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
locale = configuration.locales.first
file = configuration.files.first
file.fetch(locale) # the first time, will return the content of the language file with a status 200 OK
file.fetch(locale) # returns nothing, with a status 304 Not Modified
file.fetch(locale, true) # force to re-download the file, will return the content of the file with a 200 OK
36 37 38 39 40 41 42 43 44 |
# File 'lib/web_translate_it/translation_file.rb', line 36 def fetch(locale, force = false) WebTranslateIt::Util.http_connection do |http| request = Net::HTTP::Get.new(api_url(locale)) request.add_field('If-Modified-Since', last_modification(file_path_for_locale(locale))) if File.exist?(file_path_for_locale(locale)) and !force response = http.request(request) File.open(file_path_for_locale(locale), 'w'){ |file| file << response.body } if response.code.to_i == 200 and response.body != '' response.code.to_i end end |
#file_path_for_directory(locale) ⇒ Object
80 81 82 |
# File 'lib/web_translate_it/translation_file.rb', line 80 def file_path_for_directory(locale) self.file_path.gsub("[locale].pot", locale.split(/-/, 2)[0].to_s.split(/_/, 2)[0].to_s + "/") end |
#file_path_for_locale(locale) ⇒ Object
Convenience method which returns the file path of a TranslationFile for a given locale.
76 77 78 |
# File 'lib/web_translate_it/translation_file.rb', line 76 def file_path_for_locale(locale) self.file_path.gsub("[locale].pot", locale.split(/-/, 2)[0].to_s.split(/_/, 2)[0].to_s + "/" + "translation.po") end |
#file_path_for_locale_initial(locale) ⇒ Object
71 72 73 |
# File 'lib/web_translate_it/translation_file.rb', line 71 def file_path_for_locale_initial(locale) self.file_path.gsub("[locale]", locale) end |
#upload(locale) ⇒ Object
Update a language file to Web Translate It by performing a PUT Request. Note that it is currently not possible to POST a new language file at the moment.
Example of implementation:
configuration = WebTranslateIt::Configuration.new
locale = configuration.locales.first
file = configuration.files.first
file.upload(locale) # should respond the HTTP code 202 Accepted
The meaning of the HTTP 202 code is: the request has been accepted for processing, but the processing has not been completed. 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.
61 62 63 64 65 66 67 68 69 |
# File 'lib/web_translate_it/translation_file.rb', line 61 def upload(locale) File.open(file_path_for_locale_initial(locale)) do |file| WebTranslateIt::Util.http_connection do |http| request = Net::HTTP::Put::Multipart.new(api_url('en'), "file" => UploadIO.new(file, "text/plain", file.path)) response = http.request(request) response.code.to_i end end end |