Class: Blocks::Sdk::ServiceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/blocks/sdk/service_manager.rb

Overview

Manages all Blocks Service clients and their caching

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceManager

Returns a new instance of ServiceManager.



10
11
12
13
14
# File 'lib/blocks/sdk/service_manager.rb', line 10

def initialize
  @cache   = Cache::MemoryCache.new
  @clients = {}
  register_default_clients
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/blocks/sdk/service_manager.rb', line 8

def cache
  @cache
end

#clientsObject (readonly)

Returns the value of attribute clients.



8
9
10
# File 'lib/blocks/sdk/service_manager.rb', line 8

def clients
  @clients
end

Instance Method Details

#client(service_name, config = {}) ⇒ BaseClient

Get client instance for a service

Parameters:

  • service_name (String)

    Name of the service

  • config (Hash) (defaults to: {})

    Configuration for the client

Returns:

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/blocks/sdk/service_manager.rb', line 27

def client(service_name, config = {})
  client_class = @clients[service_name.to_s]
  raise ArgumentError, "Client not found for service: #{service_name}" unless client_class

  client_class.new(config)
end

#fetch(service_name:, params:, config: {}) ⇒ Hash

Fetch data from a service with caching

Parameters:

  • service_name (String)

    Name of the service

  • params (Hash)

    Parameters for fetching

  • config (Hash) (defaults to: {})

    Client configuration

Returns:

  • (Hash)

    Service response



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blocks/sdk/service_manager.rb', line 39

def fetch(service_name:, params:, config: {})
  cache_key = build_cache_key(service_name, params)
  cached_value = @cache.get(cache_key)

  return cached_value if cached_value

  client_instance = client(service_name, config)
  result = client_instance.fetch(**params)

  # Cache successful responses
  if result[:status] == :success || result[:translations]
    @cache.set(cache_key, result)
  end

  result
end

#handle_webhook(service_name:, payload:, config: {}) ⇒ Hash

Handle webhook callback and invalidate cache

Parameters:

  • service_name (String)

    Name of the service

  • payload (Hash)

    Webhook payload

  • config (Hash) (defaults to: {})

    Client configuration

Returns:

  • (Hash)

    Webhook handling result



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/blocks/sdk/service_manager.rb', line 61

def handle_webhook(service_name:, payload:, config: {})
  client_instance = client(service_name, config)
  webhook_result = client_instance.handle_webhook(payload)

  # Invalidate cache based on webhook result
  if webhook_result[:action] == :invalidate_cache
    @cache.invalidate(
      service: service_name,
      module_name: webhook_result[:module_name],
      language: webhook_result[:language]
    )
  end

  { success: true, service: service_name, webhook_result: webhook_result }
rescue StandardError => e
  { success: false, error: e.message }
end

#register_client(service_name, client_class) ⇒ Object

Register a client for a service

Parameters:

  • service_name (String)

    Name of the service

  • client_class (Class)

    Client class



19
20
21
# File 'lib/blocks/sdk/service_manager.rb', line 19

def register_client(service_name, client_class)
  @clients[service_name.to_s] = client_class
end