Class: WebTranslateIt::Translation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Translation

Initialize a new WebTranslateIt::Translation

Implementation Example:

WebTranslateIt::Translation.new({ :string_id => "1234", :text => "Super!" })

to instantiate a new Translation without any text.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/web_translate_it/translation.rb', line 18

def initialize(params = {})
  params.stringify_keys!
  self.id         = params["id"] || nil
  self.locale     = params["locale"] || nil
  self.text       = params["text"] || nil
  self.status     = params["status"] || "status_unproofread"
  self.created_at = params["created_at"] || nil
  self.updated_at = params["updated_at"] || nil
  self.version    = params["version"] || nil
  if params["string"]
    self.string_id  = params["string"]["id"]
  else
    self.string_id = nil
  end
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#localeObject

Returns the value of attribute locale.



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

def locale
  @locale
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#string_idObject

Returns the value of attribute string_id.



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

def string_id
  @string_id
end

#textObject

Returns the value of attribute text.



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

def text
  @text
end

#updated_atObject

Returns the value of attribute updated_at.



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

def updated_at
  @updated_at
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#saveObject

Save a WebTranslateIt::Translation

Implementation Example:

translation = WebTranslateIt::Translation.new({ :string_id => "1234", :text => "Super!" })
WebTranslateIt::Connection.new('secret_api_token') do
  translation.save
end


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/web_translate_it/translation.rb', line 44

def save
  tries ||= 3
  request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings/#{self.string_id}/locales/#{self.locale}/translations")
  WebTranslateIt::Util.add_fields(request)
  request.body = self.to_json
  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
end

#to_hashObject



62
63
64
65
66
67
68
# File 'lib/web_translate_it/translation.rb', line 62

def to_hash
  {
    "locale" => locale,
    "text" => text,
    "status" => status
  }
end

#to_jsonObject



70
71
72
# File 'lib/web_translate_it/translation.rb', line 70

def to_json
  MultiJson.dump(self.to_hash)
end