Class: WebTranslateIt::String
- Inherits:
-
Object
- Object
- WebTranslateIt::String
- Defined in:
- lib/web_translate_it/string.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#dev_comment ⇒ Object
Returns the value of attribute dev_comment.
-
#file ⇒ Object
Returns the value of attribute file.
-
#id ⇒ Object
Returns the value of attribute id.
-
#key ⇒ Object
Returns the value of attribute key.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#new_record ⇒ Object
Returns the value of attribute new_record.
-
#plural ⇒ Object
Returns the value of attribute plural.
-
#status ⇒ Object
Returns the value of attribute status.
-
#translations ⇒ Object
Returns the value of attribute translations.
-
#type ⇒ Object
Returns the value of attribute type.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
-
#word_count ⇒ Object
Returns the value of attribute word_count.
Class Method Summary collapse
-
.find(id) ⇒ Object
Find a String based on its ID Return a String object, or nil if not found.
-
.find_all(params = {}) ⇒ Object
Find a String based on filters.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete a String on WebTranslateIt.com.
-
#initialize(params = {}) ⇒ String
constructor
Initialize a new WebTranslateIt::String.
-
#save ⇒ Object
Update or create a String to WebTranslateIt.com.
-
#translation_for(locale) ⇒ Object
Gets a Translation for a String.
Constructor Details
#initialize(params = {}) ⇒ String
Initialize a new WebTranslateIt::String
Implementation Example:
WebTranslateIt::String.new({ :key => "product_name_123" })
to instantiate a new String without any text.
translation_en = WebTranslateIt::Translation.new({ :locale => "en", :text => "Hello" })
translation_fr = WebTranslateIt::Translation.new({ :locale => "fr", :text => "Bonjour" })
WebTranslateIt::String.new({ :key => "product_name_123", :translations => [translation_en, translation_fr]})
to instantiate a new String with a source and target translation.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/web_translate_it/string.rb', line 23 def initialize(params = {}) params.stringify_keys! self.id = params["id"] || nil self.key = params["key"] || nil self.plural = params["plural"] || nil self.type = params["type"] || nil self.dev_comment = params["dev_comment"] || nil self.word_count = params["word_count"] || nil self.status = params["status"] || nil self.category = params["category"] || nil self.labels = params["labels"] || nil self.file = params["file"] || nil self.created_at = params["created_at"] || nil self.updated_at = params["updated_at"] || nil self.translations = params["translations"] || [] self.new_record = true end |
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def category @category end |
#created_at ⇒ Object
Returns the value of attribute created_at.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def created_at @created_at end |
#dev_comment ⇒ Object
Returns the value of attribute dev_comment.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def dev_comment @dev_comment end |
#file ⇒ Object
Returns the value of attribute file.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def file @file end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def key @key end |
#labels ⇒ Object
Returns the value of attribute labels.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def labels @labels end |
#new_record ⇒ Object
Returns the value of attribute new_record.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def new_record @new_record end |
#plural ⇒ Object
Returns the value of attribute plural.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def plural @plural end |
#status ⇒ Object
Returns the value of attribute status.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def status @status end |
#translations ⇒ Object
Returns the value of attribute translations.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def translations @translations end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def type @type end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def updated_at @updated_at end |
#word_count ⇒ Object
Returns the value of attribute word_count.
6 7 8 |
# File 'lib/web_translate_it/string.rb', line 6 def word_count @word_count end |
Class Method Details
.find(id) ⇒ Object
Find a String based on its ID Return a String object, or nil if not found.
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
string = WebTranslateIt::String.find(1234)
end
puts string.inspect #=> A WebTranslateIt::String object
to find and instantiate the String which ID is ‘1234`.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/web_translate_it/string.rb', line 109 def self.find(id) success = true tries ||= 3 request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}.yaml") request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) begin response = Connection.http_connection.request(request) return nil if response.code.to_i == 404 string = WebTranslateIt::String.new(YAML.load(response.body)) string.new_record = false return string rescue Timeout::Error puts "Request timeout. Will retry in 5 seconds." if (tries -= 1) > 0 sleep(5) retry else success = false end end success end |
.find_all(params = {}) ⇒ Object
Find a String based on filters
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
strings = WebTranslateIt::String.find_all({ :key => "product_name_123" })
end
puts strings.inspect #=> An array of WebTranslateIt::String objects
to find and instantiate an array of String which key is like ‘product_name_123`.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/web_translate_it/string.rb', line 53 def self.find_all(params = {}) success = true tries ||= 3 params.stringify_keys! url = "/api/projects/#{Connection.api_key}/strings.yaml" url += '?' + HashUtil.to_params("filters" => params) unless params.empty? request = Net::HTTP::Get.new(url) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) begin strings = [] while(request) do response = Connection.http_connection.request(request) YAML.load(response.body).each do |string_response| string = WebTranslateIt::String.new(string_response) string.new_record = false strings.push(string) end if response["Link"] && response["Link"].include?("rel=\"next\"") url = response["Link"].match(/<(.*)>; rel="next"/)[1] request = Net::HTTP::Get.new(url) request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) else request = nil end end return strings rescue Timeout::Error puts "Request timeout. Will retry in 5 seconds." if (tries -= 1) > 0 sleep(5) retry else success = false end end success end |
Instance Method Details
#delete ⇒ Object
Delete a String on WebTranslateIt.com
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
string = WebTranslateIt::String.find(1234)
string.delete
end
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/web_translate_it/string.rb', line 159 def delete success = true tries ||= 3 request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{self.id}") request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) begin Util.handle_response(Connection.http_connection.request(request), true, true) rescue Timeout::Error puts "Request timeout. Will retry in 5 seconds." if (tries -= 1) > 0 sleep(5) retry else success = false end end success end |
#save ⇒ Object
Update or create a String to WebTranslateIt.com
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
string = WebTranslateIt::String.find(1234)
string.status = "status_obsolete"
string.save
end
145 146 147 |
# File 'lib/web_translate_it/string.rb', line 145 def save self.new_record ? self.create : self.update end |
#translation_for(locale) ⇒ Object
Gets a Translation for a String
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
string = WebTranslateIt::String.find(1234)
puts string.translation_for("fr") #=> A Translation object
end
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/web_translate_it/string.rb', line 190 def translation_for(locale) success = true tries ||= 3 translation = self.translations.detect{ |t| t.locale == locale } return translation if translation return nil if self.new_record request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{self.id}/locales/#{locale}/translations.yaml") request.add_field("X-Client-Name", "web_translate_it") request.add_field("X-Client-Version", WebTranslateIt::Util.version) begin response = Util.handle_response(Connection.http_connection.request(request), true, true) hash = YAML.load(response) return nil if hash.empty? translation = WebTranslateIt::Translation.new(hash) return translation rescue Timeout::Error puts "Request timeout. Will retry in 5 seconds." if (tries -= 1) > 0 sleep(5) retry else success = false end end success end |