module Transfluent::TransfluentFileApi

Public Instance Methods

file_read(identifier, language = nil) click to toggle source

Read a file from Transfluent

@param [String] identifier file identifier @param [Transfluent::Language] language language of the file. Defaults to default_language

@return [Hash] a hash with keys :name and :contents

# File lib/transfluent/transfluent_file_api.rb, line 70
def file_read identifier, language = nil
  language = default_language if language.nil?
  uri = api_uri 'file/read'

  query = {
    'identifier' => identifier,
    'language' => language.id,
    'token' => auth_token
  }

  uri.query = URI.encode_www_form query
  response = Net::HTTP.get_response uri

  file = {
    :name => response['Content-Disposition'].partition('=').last,
    :contents => response.body
  }
end
file_save(options) click to toggle source

Saves a file to Transfluent

@param [Hash] options @option options [String] identifier unique identifier for the file @option options [String] content_base64 base64 encoded content of the file. Has priority over file option. @option options [File, String] file File instance or file name to read. Used if content_base64 is not defined. @option options [String] type File type. Must be one of FileType constants @option options ['UTF-8', 'UTF-16'] encoding endocing of the file. Defaults to UTF-8 @option options [Transfluent::Language] language language of the file. Defaults to default_language @option options [true, false] save_only_data If you have previously translated content,

you may save the translations by specifying this variable as true.
Remember to use the same file identifier as the source file has. Defaults to false

@return [Hash] hash that contains count of saved words

# File lib/transfluent/transfluent_file_api.rb, line 36
def file_save options
  uri = api_uri 'file/save'

  opts = @defaults.merge({
    :encoding => 'UTF-8'
  }).merge options

  data = {
    'format' => opts[:encoding],
    'identifier' => opts[:identifier],
    'language' => opts[:language].id,
    'save_only_data' => opts[:save_only_data],
    'type' => opts[:type],
    'token' => auth_token
  }

  if opts.has_key? :content_base64
    data['content'] = opts[:content_base64]
  elsif opts.has_key? :file
    f = opts[:file]
    f = File.new(f) unless f.is_a? File
    
    data['content'] = Base64.encode64 f.read
  end

  execute_post uri, data
end
file_status(identifier, language = nil) click to toggle source

Query translation status for a file

@param [String] identifier file identifier @param [Transfluent::Language] language language of the file. Defaults to default_language

@return [Hash] a hash with progress information

# File lib/transfluent/transfluent_file_api.rb, line 95
def file_status identifier, language = nil
  language = default_language if language.nil?

  uri = api_uri 'file/status'
  
  query = {
    'identifier' => identifier,
    'language' => language.id,
    'token' => auth_token
  }
  
  response = execute_get uri, query

  response[:progress_str] = response[:progress]
  response[:word_count] = response[:word_count].to_i
  response[:word_count_translated] = response[:word_count_translated].to_i
  response[:progress] = (response[:word_count_translated].to_f / response[:word_count]).round(4)
  response[:word_count] = response[:word_count].to_i

  response
end
file_translate(options) click to toggle source

Order translation for a file

@param [Hash] options @option options [String] identifier an unique identifier for a file @option options [Array] target_languages array of Transfluent::Language to translate file to @option options [Transfluent::Language] language language of the file. Defaults to default_language @option options [String] comment a context comment of the file for the translators @option options [1, 2, 3] level level of the translation. 1 == native, 2 == professional, 3 == pair of translators. Defaults to 3 @option options [URI] callback_uri optional URI to post the translated file once complete.

@return [Hash] a hash of word counts

# File lib/transfluent/transfluent_file_api.rb, line 128
def file_translate options
  opts = @defaults.merge options

  uri = api_uri 'file/translate'

  data = {
    'identifier' => opts[:identifier],
    'language' => opts[:language].id,
    'target_languages' => '[%s]' % opts[:target_languages].map { |lang| lang.id },
    'token' => auth_token
  }

  data['comment'] = opts[:comment] if opts.has_key? :comment
  data['level'] = opts[:level] if opts.has_key? :level
  data['callback_url'] = opts[:callback_uri].to_s if opts.has_key? :callback_uri

  execute_post(uri, data).inject({}) do |memo, (k, v)|
    memo[k] = v.to_i
    memo
  end
end