Class: WebTranslateIt::String

Inherits:
Object
  • Object
show all
Defined in:
lib/web_translate_it/string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#categoryObject

Returns the value of attribute category


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def category
  @category
end

#created_atObject

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_commentObject

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

#fileObject

Returns the value of attribute file


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def file
  @file
end

#idObject

Returns the value of attribute id


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def id
  @id
end

#keyObject

Returns the value of attribute key


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def key
  @key
end

#labelsObject

Returns the value of attribute labels


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def labels
  @labels
end

#new_recordObject

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

#pluralObject

Returns the value of attribute plural


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def plural
  @plural
end

#statusObject

Returns the value of attribute status


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def status
  @status
end

#translationsObject

Returns the value of attribute translations


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def translations
  @translations
end

#typeObject

Returns the value of attribute type


6
7
8
# File 'lib/web_translate_it/string.rb', line 6

def type
  @type
end

#updated_atObject

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_countObject

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`.


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/web_translate_it/string.rb', line 102

def self.find(id)
  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 "The request timed out. The service may be overloaded. We will retry in 5 seconds."
    sleep(5)
    retry
  end
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
# File 'lib/web_translate_it/string.rb', line 53

def self.find_all(params = {})
  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 "The request timed out. The service may be overloaded. We will retry in 5 seconds."
    sleep(5)
    retry
  end
end

Instance Method Details

#deleteObject

Delete a String on WebTranslateIt.com

Implementation Example:

WebTranslateIt::Connection.new('secret_api_token') do
  string = WebTranslateIt::String.find(1234)
  string.delete
end

145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/web_translate_it/string.rb', line 145

def delete
  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 "The request timed out. The service may be overloaded. We will retry in 5 seconds."
    sleep(5)
    retry
  end
end

#saveObject

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

131
132
133
# File 'lib/web_translate_it/string.rb', line 131

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

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/web_translate_it/string.rb', line 169

def translation_for(locale)
  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 "The request timed out. The service may be overloaded. We will retry in 5 seconds."
    sleep(5)
    retry
  end      
end