Class: WebTranslateIt::Term
- Inherits:
-
Object
- Object
- WebTranslateIt::Term
- Defined in:
- lib/web_translate_it/term.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#new_record ⇒ Object
Returns the value of attribute new_record.
-
#text ⇒ Object
Returns the value of attribute text.
-
#translations ⇒ Object
Returns the value of attribute translations.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
-
.find(term_id) ⇒ Object
Find a Term based on its ID Returns a Term object or nil if not found.
-
.find_all(params = {}) ⇒ Object
Fetch all terms.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete a Term on WebTranslateIt.com.
-
#initialize(params = {}) ⇒ Term
constructor
Initialize a new WebTranslateIt::Term.
-
#save ⇒ Object
Update or create a Term to WebTranslateIt.com.
-
#translation_for(locale) ⇒ Object
Gets a Translation for a Term.
Constructor Details
#initialize(params = {}) ⇒ Term
Initialize a new WebTranslateIt::Term
Implementation Example:
WebTranslateIt::Term.new({ :text => "Term Name" })
to instantiate a new Term.
translation_es = WebTranslateIt::TermTranslation.new({ :locale => "es", :text => "Hola" })
translation_fr = WebTranslateIt::TermTranslation.new({ :locale => "fr", :text => "Bonjour" })
WebTranslateIt::Term.new({ "text" => "Hello", "translations" => [translation_es, translation_fr]})
to instantiate a new Term with a Term Translations in Spanish and French.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/web_translate_it/term.rb', line 23 def initialize(params = {}) params.stringify_keys! self.id = params["id"] || nil self.text = params["text"] || nil self.description = params["description"] || 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
#created_at ⇒ Object
Returns the value of attribute created_at.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def created_at @created_at end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def id @id end |
#new_record ⇒ Object
Returns the value of attribute new_record.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def new_record @new_record end |
#text ⇒ Object
Returns the value of attribute text.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def text @text end |
#translations ⇒ Object
Returns the value of attribute translations.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def translations @translations end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
7 8 9 |
# File 'lib/web_translate_it/term.rb', line 7 def updated_at @updated_at end |
Class Method Details
.find(term_id) ⇒ Object
Find a Term based on its ID Returns a Term object or nil if not found.
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
term = WebTranslateIt::Term.find(1234)
end
puts term.inspect #=> A Term object
to find and instantiate the Term which ID is ‘1234`.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/web_translate_it/term.rb', line 97 def self.find(term_id) success = true tries ||= 3 request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{term_id}.yaml") WebTranslateIt::Util.add_fields(request) begin response = Connection.http_connection.request(request) return nil if response.code.to_i == 404 term = WebTranslateIt::Term.new(YAML.load(response.body)) term.new_record = false return term 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
Fetch all terms
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
terms = WebTranslateIt::Term.find_all
end
puts terms.inspect #=> An array of WebTranslateIt::Term objects
44 45 46 47 48 49 50 51 52 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 |
# File 'lib/web_translate_it/term.rb', line 44 def self.find_all(params = {}) success = true tries ||= 3 params.stringify_keys! url = "/api/projects/#{Connection.api_key}/terms.yaml" url += '?' + HashUtil.to_params(params) unless params.empty? request = Net::HTTP::Get.new(url) WebTranslateIt::Util.add_fields(request) begin terms = [] while(request) do response = Connection.http_connection.request(request) YAML.load(response.body).each do |term_response| term = WebTranslateIt::Term.new(term_response) term.new_record = false terms.push(term) end if response["Link"] && 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 terms 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 Term on WebTranslateIt.com
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
term = WebTranslateIt::Term.find(1234)
term.delete
end
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/web_translate_it/term.rb', line 145 def delete success = true tries ||= 3 request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/terms/#{self.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) > 0 sleep(5) retry else success = false end end success end |
#save ⇒ Object
Update or create a Term to WebTranslateIt.com
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
term = WebTranslateIt::Term.find(1234)
term.text = "Hello"
term.save
end
131 132 133 |
# File 'lib/web_translate_it/term.rb', line 131 def save self.new_record ? self.create : self.update end |
#translation_for(locale) ⇒ Object
Gets a Translation for a Term
Implementation Example:
WebTranslateIt::Connection.new('secret_api_token') do
term = WebTranslateIt::Term.find(1234)
puts term.translation_for("fr") #=> A TermTranslation object
end
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/web_translate_it/term.rb', line 174 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}/terms/#{self.id}/locales/#{locale}/translations.yaml") WebTranslateIt::Util.add_fields(request) begin response = Util.handle_response(Connection.http_connection.request(request), true, true) array = YAML.load(response) return nil if array.empty? translations = [] array.each do |translation| term_translation = WebTranslateIt::TermTranslation.new(translation) translations.push(term_translation) end return translations 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 |