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_account_share(id, name: nil) ⇒ Object



65
66
67
# File 'lib/lara/glossaries.rb', line 65

def (id, name: nil)
  glossary_from(@client.post("/v2/glossaries/#{id}/shares", body: { name: name }.compact))
end

#add_group_share(id, group_id, name: nil) ⇒ Object



77
78
79
# File 'lib/lara/glossaries.rb', line 77

def add_group_share(id, group_id, name: nil)
  glossary_from(@client.post("/v2/glossaries/#{id}/shares/groups/#{group_id}", body: { name: name }.compact))
end

#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:



181
182
183
184
185
186
187
# File 'lib/lara/glossaries.rb', line 181

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



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

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:



193
194
195
196
197
198
199
200
# File 'lib/lara/glossaries.rb', line 193

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



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

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:



166
167
168
169
170
171
172
173
174
175
# File 'lib/lara/glossaries.rb', line 166

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



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

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

#get_shares(id) ⇒ Lara::Models::GlossaryShares



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

def get_shares(id)
  Lara::Models::GlossaryShares.new(**@client.get("/v2/glossaries/#{id}/shares").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:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lara/glossaries.rb', line 98

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

#rename_account_share(id, name:) ⇒ Object



69
70
71
# File 'lib/lara/glossaries.rb', line 69

def (id, name:)
  glossary_from(@client.put("/v2/glossaries/#{id}/shares", body: { name: name }))
end

#rename_group_share(id, group_id, name:) ⇒ Object



81
82
83
# File 'lib/lara/glossaries.rb', line 81

def rename_group_share(id, group_id, name:)
  glossary_from(@client.put("/v2/glossaries/#{id}/shares/groups/#{group_id}", body: { name: name }))
end

#revoke_account_share(id) ⇒ Object



73
74
75
# File 'lib/lara/glossaries.rb', line 73

def (id)
  glossary_from(@client.delete("/v2/glossaries/#{id}/shares"))
end

#revoke_group_share(id, group_id) ⇒ Object



85
86
87
# File 'lib/lara/glossaries.rb', line 85

def revoke_group_share(id, group_id)
  glossary_from(@client.delete("/v2/glossaries/#{id}/shares/groups/#{group_id}"))
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



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

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