Class: Blocks::Sdk::Clients::TranslationsClient

Inherits:
BaseClient
  • Object
show all
Defined in:
lib/blocks/sdk/clients/translations_client.rb

Overview

Client for UILM Translation Service

Instance Attribute Summary

Attributes inherited from BaseClient

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClient

inherited

Constructor Details

#initialize(config = {}) ⇒ TranslationsClient

Returns a new instance of TranslationsClient.



18
19
20
21
22
# File 'lib/blocks/sdk/clients/translations_client.rb', line 18

def initialize(config = {})
  super(config)
  @base_url    = config[:base_url] || ENV.fetch("UILM_BASE_URL")
  @project_key = config[:project_key] || ENV.fetch("UILM_PROJECT_KEY")
end

Class Method Details

.service_nameObject



13
14
15
# File 'lib/blocks/sdk/clients/translations_client.rb', line 13

def service_name
  "translations"
end

Instance Method Details

#fetch(module_name:, language: "en") ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/blocks/sdk/clients/translations_client.rb', line 24

def fetch(module_name:, language: "en")
  conn = build_connection

  request_params = {
    ModuleName: module_name,
    ProjectKey: @project_key,
    Language: normalize_language(language)
  }

  response = conn.get(
    @base_url,
    request_params,
    build_headers
  )

  handle_response(response)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  log_error("UILM API connection failed: #{e.message}")
  { error: "Connection failed", status: :connection_error }
rescue StandardError => e
  log_error("Unexpected error in TranslationsClient: #{e.message}")
  { error: "Unexpected error occurred", status: :error }
end

#handle_webhook(payload) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/blocks/sdk/clients/translations_client.rb', line 48

def handle_webhook(payload)
  # Extract cache invalidation parameters from webhook payload
  # Expected payload structure: { module_name: "...", language: "..." }
  module_name = payload[:module_name] || payload["module_name"]
  language = payload[:language] || payload["language"] || "en"

  {
    service: service_name,
    module_name: module_name,
    language: language,
    action: :invalidate_cache
  }
end