Class: WebTranslateIt::String
- Inherits:
-
Object
- Object
- WebTranslateIt::String
- Defined in:
- lib/web_translate_it/string.rb
Overview
rubocop:todo Metrics/ClassLength
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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/web_translate_it/string.rb', line 24 def initialize(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def category @category end |
#created_at ⇒ Object
Returns the value of attribute created_at.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def created_at @created_at end |
#dev_comment ⇒ Object
Returns the value of attribute dev_comment.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def dev_comment @dev_comment end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def file @file end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def key @key end |
#labels ⇒ Object
Returns the value of attribute labels.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def labels @labels end |
#new_record ⇒ Object
Returns the value of attribute new_record.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def new_record @new_record end |
#plural ⇒ Object
Returns the value of attribute plural.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def plural @plural end |
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def status @status end |
#translations ⇒ Object
Returns the value of attribute translations.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def translations @translations end |
#type ⇒ Object
Returns the value of attribute type.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def type @type end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 def updated_at @updated_at end |
#word_count ⇒ Object
Returns the value of attribute word_count.
7 8 9 |
# File 'lib/web_translate_it/string.rb', line 7 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`.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/web_translate_it/string.rb', line 107 def self.find(id) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize success = true tries ||= 3 request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}") WebTranslateIt::Util.add_fields(request) begin response = Connection.http_connection.request(request) return nil if response.code.to_i == 404 string = WebTranslateIt::String.new(JSON.parse(response.body)) string.new_record = false return string rescue Timeout::Error puts 'Request timeout. Will retry in 5 seconds.' if (tries -= 1).positive? 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`.
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 |
# File 'lib/web_translate_it/string.rb', line 54 def self.find_all(params = {}) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity success = true tries ||= 3 params.stringify_keys! url = "/api/projects/#{Connection.api_key}/strings" url += "?#{HashUtil.to_params('filters' => params)}" unless params.empty? request = Net::HTTP::Get.new(url) WebTranslateIt::Util.add_fields(request) begin strings = [] while request response = Connection.http_connection.request(request) JSON.parse(response.body).each do |string_response| string = WebTranslateIt::String.new(string_response) string.new_record = false strings.push(string) end if response['Link']&.include?('rel="next"') url = response['Link'].match(/<(.*)>; rel="next"/)[1] request = Net::HTTP::Get.new(url) WebTranslateIt::Util.add_fields(request) else request = nil end end return strings rescue Timeout::Error puts 'Request timeout. Will retry in 5 seconds.' if (tries -= 1).positive? 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
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/web_translate_it/string.rb', line 156 def delete # rubocop:todo Metrics/MethodLength success = true tries ||= 3 request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/strings/#{id}") WebTranslateIt::Util.add_fields(request) 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).positive? 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
142 143 144 |
# File 'lib/web_translate_it/string.rb', line 142 def save new_record ? create : 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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/web_translate_it/string.rb', line 185 def translation_for(locale) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity success = true tries ||= 3 translation = translations.detect { |t| t.locale == locale } return translation if translation return nil if new_record request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/strings/#{id}/locales/#{locale}/translations") WebTranslateIt::Util.add_fields(request) begin response = Util.handle_response(Connection.http_connection.request(request), true, true) hash = JSON.parse(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).positive? sleep(5) retry else success = false end end success end |