Module: Transfluent::TransfluentFileApi

Included in:
Api
Defined in:
lib/transfluent/transfluent_file_api.rb

Instance Method Summary collapse

Instance Method Details

#file_read(identifier, language = nil) ⇒ Hash

Read a file from Transfluent

Parameters:

  • identifier (String)

    file identifier

  • language (Transfluent::Language) (defaults to: nil)

    language of the file. Defaults to default_language

Returns:

  • (Hash)

    a hash with keys :name and :contents



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Hash

Saves a file to Transfluent

Parameters:

  • options (Hash)

Options Hash (options):

  • identifier (String)

    unique identifier for the file

  • content_base64 (String)

    base64 encoded content of the file. Has priority over file option.

  • file (File, String)

    File instance or file name to read. Used if content_base64 is not defined.

  • type (String)

    File type. Must be one of FileType constants

  • encoding ('UTF-8', 'UTF-16')

    endocing of the file. Defaults to UTF-8

  •  language (Transfluent::Language)

    language of the file. Defaults to default_language

  • save_only_data (true, false)

    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

Returns:

  • (Hash)

    hash that contains count of saved words



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Hash

Query translation status for a file

Parameters:

  • identifier (String)

    file identifier

  • language (Transfluent::Language) (defaults to: nil)

    language of the file. Defaults to default_language

Returns:

  • (Hash)

    a hash with progress information



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Hash

Order translation for a file

Parameters:

  • options (Hash)

Options Hash (options):

  • identifier (String)

    an unique identifier for a file

  • target_languages (Array)

    array of Transfluent::Language to translate file to

  • language (Transfluent::Language)

    language of the file. Defaults to default_language

  • comment (String)

    a context comment of the file for the translators

  • level (1, 2, 3)

    level of the translation. 1 == native, 2 == professional, 3 == pair of translators. Defaults to 3

  • callback_uri (URI)

    optional URI to post the translated file once complete.

Returns:

  • (Hash)

    a hash of word counts



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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