Class: Postnhost::Translation::ArticleVariantTranslator

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

Instance Method Summary collapse

Methods inherited from BaseService

call

Constructor Details

#initialize(article_id:, language_id:, llm_client: OpenaiLlmClient.new) ⇒ ArticleVariantTranslator

Returns a new instance of ArticleVariantTranslator.



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

def initialize(article_id:, language_id:, llm_client: OpenaiLlmClient.new)
  @article_id = article_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
26
# File 'app/services/postnhost/translation/article_variant_translator.rb', line 10

def call
  load_records
  return failure("Translation source or language not found") unless article && language
  return failure("Published translation source not found") unless source

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

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

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