Class: WebTranslateIt::String

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

Overview

rubocop:todo Metrics/ClassLength

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.



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

#categoryObject

Returns the value of attribute category.



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

def category
  @category
end

#created_atObject

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_commentObject

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

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#labelsObject

Returns the value of attribute labels.



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

def labels
  @labels
end

#new_recordObject

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

#pluralObject

Returns the value of attribute plural.



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

def plural
  @plural
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#translationsObject

Returns the value of attribute translations.



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

def translations
  @translations
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#updated_atObject

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_countObject

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

#deleteObject

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

#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


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