Class: Postnhost::Translation::CategoryVariantTranslator

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/postnhost/translation/category_variant_translator.rb

Instance Method Summary collapse

Methods inherited from BaseService

call

Constructor Details

#initialize(category_id:, language_id:, llm_client: OpenaiLlmClient.new) ⇒ CategoryVariantTranslator

Returns a new instance of CategoryVariantTranslator.



4
5
6
7
8
# File 'app/services/postnhost/translation/category_variant_translator.rb', line 4

def initialize(category_id:, language_id:, llm_client: OpenaiLlmClient.new)
  @category_id = category_id
  @language_id = language_id
  @llm_client = llm_client
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/postnhost/translation/category_variant_translator.rb', line 10

def call
  load_records
  return failure("Category or language not found") unless category && language

  load_variant
  return failure("Category variant not found") unless variant

  attributes = translated_attributes
  return finish_with_failure("Translation failed") if attributes[:name].blank?

  variant.update!(attributes.merge(generating: false))
  success(variant)
rescue StandardError => e
  mark_generation_finished
  failure(e.message)
end