class WebTranslateIt::TermTranslation

Attributes

description[RW]
id[RW]
locale[RW]
new_record[RW]
status[RW]
term_id[RW]
text[RW]

Public Class Methods

new(params = {}) click to toggle source

Initialize a new WebTranslateIt::TermTranslation

Implementation Example:

WebTranslateIt::TermTranslation.new({ :text => "Super!" })

to instantiate a new TermTranslation.

# File lib/web_translate_it/term_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.description = params['description'] || nil
  self.status      = params['status'] || nil
  self.term_id     = params['term_id'] || nil
  self.new_record  = true
end

Public Instance Methods

save() click to toggle source

Update or Create a WebTranslateIt::TermTranslation

Implementation Example:

translation = WebTranslateIt::TermTranslation.new({ :term_id => "1234", :text => "Super!" })
WebTranslateIt::Connection.new('secret_api_token') do
  translation.save
end
# File lib/web_translate_it/term_translation.rb, line 39
def save
  new_record ? create : update
end
to_hash() click to toggle source
# File lib/web_translate_it/term_translation.rb, line 43
def to_hash
  {
    'id' => id,
    'locale' => locale,
    'text' => text,
    'description' => description,
    'status' => status
  }
end
to_json(*_args) click to toggle source
# File lib/web_translate_it/term_translation.rb, line 53
def to_json(*_args)
  MultiJson.dump(to_hash)
end

Protected Instance Methods

create() click to toggle source
# File lib/web_translate_it/term_translation.rb, line 59
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  success = true
  tries ||= 3
  request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms/#{term_id}/locales/#{locale}/translations")
  WebTranslateIt::Util.add_fields(request)
  request.body = to_json

  begin
    response = JSON.parse(Util.handle_response(Connection.http_connection.request(request), true, true))
    self.id = response['id']
    self.new_record = false
    return 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
update() click to toggle source
# File lib/web_translate_it/term_translation.rb, line 83
def update # rubocop:todo Metrics/MethodLength
  success = true
  tries ||= 3
  request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}")
  WebTranslateIt::Util.add_fields(request)
  request.body = 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).positive?
      sleep(5)
      retry
    else
      success = false
    end
  end
  success
end