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.



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

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:



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

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



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

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

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



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

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

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



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

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:



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

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



114
115
116
117
118
119
120
121
# File 'lib/lara/glossaries.rb', line 114

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

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

Returns:



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

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



90
91
92
# File 'lib/lara/glossaries.rb', line 90

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: true) ⇒ Lara::Models::GlossaryImport

Parameters:

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

    Either FileFormat::UNIDIRECTIONAL or FileFormat::MULTIDIRECTIONAL

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lara/glossaries.rb', line 66

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

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

  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" }
  body["content_type"] = content_type unless content_type == FileFormat::UNIDIRECTIONAL

  files = { "csv" => Faraday::UploadIO.new(buffer, "application/gzip", "#{basename}.gz") }
  Lara::Models::GlossaryImport.new(**@client.post("/v2/glossaries/#{id}/import",
                                                  body: body, files: files).transform_keys(&:to_sym))
end

#listArray<Lara::Models::Glossary>

Returns:



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

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



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lara/glossaries.rb', line 95

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