Class: Lara::Glossaries

Inherits:
Object
  • Object
show all
Defined in:
lib/lara/glossaries.rb

Defined Under Namespace

Modules: FileFormat

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Glossaries

Returns a new instance of Glossaries.



22
23
24
25
# File 'lib/lara/glossaries.rb', line 22

def initialize(client)
  @client = client
  @polling_interval = 2
end

Instance Method Details

#add_or_replace_entry(glossary_id, terms, guid: nil) ⇒ Lara::Models::GlossaryImport

Returns The import operation.

Parameters:

  • glossary_id (String)

    The glossary ID

  • terms (Array<Hash>)

    Array of term hashes with :language and :value keys

  • guid (String, nil) (defaults to: nil)

    Optional unique identifier for multidirectional glossary units

Returns:



152
153
154
155
156
157
158
# File 'lib/lara/glossaries.rb', line 152

def add_or_replace_entry(glossary_id, terms, guid: nil)
  body = { terms: terms }
  body[:guid] = guid if guid

  Lara::Models::GlossaryImport.new(**@client.put("/v2/glossaries/#{glossary_id}/content",
                                                 body: body).transform_keys(&:to_sym))
end

#counts(id) ⇒ Lara::Models::GlossaryCounts



61
62
63
# File 'lib/lara/glossaries.rb', line 61

def counts(id)
  Lara::Models::GlossaryCounts.new(**@client.get("/v2/glossaries/#{id}/counts").transform_keys(&:to_sym))
end

#create(name:) ⇒ Lara::Models::Glossary



35
36
37
38
# File 'lib/lara/glossaries.rb', line 35

def create(name:)
  Lara::Models::Glossary.new(**@client.post("/v2/glossaries",
                                            body: { name: name }).transform_keys(&:to_sym))
end

#delete(id) ⇒ Lara::Models::Glossary



50
51
52
# File 'lib/lara/glossaries.rb', line 50

def delete(id)
  Lara::Models::Glossary.new(**@client.delete("/v2/glossaries/#{id}").transform_keys(&:to_sym))
end

#delete_entry(glossary_id, term: nil, guid: nil) ⇒ Lara::Models::GlossaryImport

Returns The import operation.

Parameters:

  • glossary_id (String)

    The glossary ID

  • term (Hash, nil) (defaults to: nil)

    Optional term hash with :language and :value keys

  • guid (String, nil) (defaults to: nil)

    Optional unique identifier for multidirectional glossary units

Returns:



164
165
166
167
168
169
170
171
# File 'lib/lara/glossaries.rb', line 164

def delete_entry(glossary_id, term: nil, guid: nil)
  body = {}
  body[:guid] = guid if guid
  body[:term] = term if term

  Lara::Models::GlossaryImport.new(**@client.delete("/v2/glossaries/#{glossary_id}/content",
                                                    body: body).transform_keys(&:to_sym))
end

#export(id, content_type: FileFormat::UNIDIRECTIONAL, source: nil) ⇒ String

Exports a csv file with the glossary content.

Parameters:

  • content_type (String) (defaults to: FileFormat::UNIDIRECTIONAL)

    Either FileFormat::UNIDIRECTIONAL or FileFormat::MULTIDIRECTIONAL

  • source (String, nil) (defaults to: nil)

    Optional source language

Returns:

  • (String)

    bytes



124
125
126
127
128
129
130
131
# File 'lib/lara/glossaries.rb', line 124

def export(id, content_type: FileFormat::UNIDIRECTIONAL, source: nil)
  unless FileFormat.valid?(content_type)
    raise ArgumentError, "Invalid content_type. Supported formats: #{FileFormat.all.join(', ')}"
  end

  @client.get("/v2/glossaries/#{id}/export",
              params: { content_type: content_type, source: source }.compact)
end

#export_async(id, callback_url:, content_type: FileFormat::UNIDIRECTIONAL, source: nil) ⇒ Lara::Models::GlossaryExport

Parameters:

  • callback_url (String)

    URL notified when the export is ready

  • content_type (String) (defaults to: FileFormat::UNIDIRECTIONAL)

    Either FileFormat::UNIDIRECTIONAL or FileFormat::MULTIDIRECTIONAL

  • source (String, nil) (defaults to: nil)

    Optional source language (unidirectional exports only)

Returns:



137
138
139
140
141
142
143
144
145
146
# File 'lib/lara/glossaries.rb', line 137

def export_async(id, callback_url:, content_type: FileFormat::UNIDIRECTIONAL, source: nil)
  unless FileFormat.valid?(content_type)
    raise ArgumentError, "Invalid content_type. Supported formats: #{FileFormat.all.join(', ')}"
  end

  params = { callback_url: callback_url, content_type: content_type }
  params[:source] = source if source
  Lara::Models::GlossaryExport.new(**@client.get("/v2/glossaries/#{id}/export/async",
                                                 params: params).transform_keys(&:to_sym))
end

#get(id) ⇒ Lara::Models::Glossary?

Returns:



41
42
43
44
45
46
47
# File 'lib/lara/glossaries.rb', line 41

def get(id)
  Lara::Models::Glossary.new(**@client.get("/v2/glossaries/#{id}").transform_keys(&:to_sym))
rescue Lara::LaraApiError => e
  return nil if e.status_code == 404

  raise
end

#get_import_status(import_id) ⇒ Lara::Models::GlossaryImport



100
101
102
# File 'lib/lara/glossaries.rb', line 100

def get_import_status(import_id)
  Lara::Models::GlossaryImport.new(**@client.get("/v2/glossaries/imports/#{import_id}").transform_keys(&:to_sym))
end

#import_csv(id, csv_path, content_type: FileFormat::UNIDIRECTIONAL, gzip: false, callback_url: nil) ⇒ Lara::Models::GlossaryImport

Parameters:

  • content_type (String) (defaults to: FileFormat::UNIDIRECTIONAL)

    Either FileFormat::UNIDIRECTIONAL or FileFormat::MULTIDIRECTIONAL

  • gzip (Boolean) (defaults to: false)

    When true, compress the CSV before upload and set compression=gzip

  • callback_url (String, nil) (defaults to: nil)

    Optional URL notified when the import completes

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lara/glossaries.rb', line 69

def import_csv(id, csv_path, content_type: FileFormat::UNIDIRECTIONAL, gzip: false,
               callback_url: nil)
  unless FileFormat.valid?(content_type)
    raise ArgumentError, "Invalid content_type. Supported formats: #{FileFormat.all.join(', ')}"
  end

  basename = File.basename(csv_path)
  if gzip
    require "stringio"
    require "zlib"

    buffer = StringIO.new
    gz = Zlib::GzipWriter.new(buffer, 7, Zlib::DEFAULT_STRATEGY)
    File.open(csv_path, "rb") { |_f| IO.copy_stream(_f, gz) }
    gz.finish
    buffer.rewind

    body = { "compression" => "gzip" }
    files = { "csv" => Faraday::UploadIO.new(buffer, "application/gzip", "#{basename}.gz") }
  else
    body = {}
    files = { "csv" => Faraday::UploadIO.new(csv_path, "text/csv", basename) }
  end

  body["content_type"] = content_type unless content_type == FileFormat::UNIDIRECTIONAL
  body["callback_url"] = callback_url if callback_url
  Lara::Models::GlossaryImport.new(**@client.post("/v2/glossaries/#{id}/import",
                                                  body: body, files: files).transform_keys(&:to_sym))
end

#listArray<Lara::Models::Glossary>

Returns:



28
29
30
31
32
# File 'lib/lara/glossaries.rb', line 28

def list
  (@client.get("/v2/glossaries") || []).map do |_h|
    Lara::Models::Glossary.new(**_h.transform_keys(&:to_sym))
  end
end

#update(id, name:) ⇒ Lara::Models::Glossary



55
56
57
58
# File 'lib/lara/glossaries.rb', line 55

def update(id, name:)
  Lara::Models::Glossary.new(**@client.put("/v2/glossaries/#{id}",
                                           body: { name: name }).transform_keys(&:to_sym))
end

#wait_for_import(glossary_import, max_wait_time: 0) ⇒ Lara::Models::GlossaryImport



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lara/glossaries.rb', line 105

def wait_for_import(glossary_import, max_wait_time: 0)
  start = Time.now
  current = glossary_import
  while current.progress && current.progress < 1.0
    if max_wait_time.to_f.positive? && (Time.now - start) > max_wait_time.to_f
      raise Timeout::Error
    end

    sleep @polling_interval
    current = get_import_status(current.id)
    yield current if block_given?
  end
  current
end