class Xlocalize::WebtranslateIt

Attributes

http[R]
key[R]
plurals_file_id[R]
source_locale[R]
xliff_file_id[R]

Public Class Methods

new(key, http = Net::HTTP.new("webtranslateit.com", 443)) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 14
def initialize(key, http = Net::HTTP.new("webtranslateit.com", 443))
  @key = key

  @http = http
  @http.use_ssl = true

  @http.request(Net::HTTP::Get.new("/api/projects/#{@key}")) {|response|
    project = JSON.parse(response.body)["project"]
    @source_locale = project["source_locale"]["code"]
    project["project_files"].each {|file|
      next if file["locale_code"] != @source_locale
      @xliff_file_id = file["id"] if file['name'].end_with? '.xliff'
      @plurals_file_id = file["id"] if file['name'] == 'plurals.yml'
    }
  }
  raise "Could not find master xliff file for source locale #{@source_locale}" if @xliff_file_id.nil?
end

Public Instance Methods

master_file_for_locale_request(file_id, file) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 43
def master_file_for_locale_request(file_id, file)
  # /api/projects/:project_token/files/:master_project_file_id/locales/:locale_code [PUT]
  return Net::HTTP::Put::Multipart.new("/api/projects/#{@key}/files/#{file_id}/locales/#{@source_locale}", {
    "file" => UploadIO.new(file, "text/plain", file.path),
    "merge" => true,
    "ignore_missing" => true,
    "label" => "",
    "low_priority" => false
  })
end
pull(locale) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 81
def pull(locale)
  # downloading master xliff file
  data = {}
  res = send_request(Net::HTTP::Get.new("/api/projects/#{@key}/files/#{@xliff_file_id}/locales/#{locale}"))
  data['xliff'] = res.body
  # downloading master plurals file
  if !@plurals_file_id.nil?
    res = send_request(Net::HTTP::Get.new("/api/projects/#{@key}/files/#{@plurals_file_id}/locales/#{locale}"))
    data['plurals'] = res.body
  end
  return data
end
push_master(file, plurals_file) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 73
def push_master(file, plurals_file)
  if $VERBOSE
    $stderr.puts 'Updating xliff file'
  end
  send_request(master_file_for_locale_request(@xliff_file_id, file))
  push_master_plurals(plurals_file) if not plurals_file.nil?
end
push_master_plurals(plurals_file) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 54
def push_master_plurals(plurals_file)
  if @plurals_file_id.nil?
    if $VERBOSE
      $stderr.puts 'Creating plurals file'
    end
    # /api/projects/:project_token/files [POST]
    send_request(Net::HTTP::Post::Multipart.new("/api/projects/#{@key}/files", {
      "file" => UploadIO.new(plurals_file, "text/plain", plurals_file.path),
      "name" => "plurals.yml",
      "low_priority" => false
    }))
  else
    if $VERBOSE
      $stderr.puts 'Updating plurals file'
    end
    send_request(master_file_for_locale_request(@plurals_file_id, plurals_file))
  end
end
send_request(request) click to toggle source
# File lib/xlocalize/webtranslateit.rb, line 32
def send_request(request)
  resp = nil
  @http.request(request) { |res|
    if !res.code.to_i.between?(200, 300)
      raise JSON.parse(res.body)["error"]
    end
    resp = res
  }
  return resp
end