Class: EcfDgii::FrontendClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ecf_dgii/frontend_client.rb

Overview

A restricted, read-only client that only exposes GET endpoints. Suitable for use in frontend / browser code where write operations should not be available.

Token lifecycle is handled automatically:

  1. On each request, checks get_cached_token. If nil, calls get_token then cache_token.

  2. On 401 responses, calls get_token again, updates the cache, and retries the request.

Mirrors the TypeScript EcfFrontendClient 1:1.

Examples:

Basic usage

client = EcfDgii::FrontendClient.new(
  get_token: -> { my_backend.fetch_token },
  environment: :test
)
ecfs = client.query_ecf("131460941", "E310000051630")

Constant Summary collapse

ENVIRONMENT_URLS =
{
  test: "https://api.test.ecfx.ssd.com.do",
  cert: "https://api.cert.ecfx.ssd.com.do",
  prod: "https://api.prod.ecfx.ssd.com.do"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(get_token:, cache_token: nil, get_cached_token: nil, base_url: nil, environment: :test, timeout: 30) ⇒ FrontendClient

Create a new frontend client.

Parameters:

  • get_token (Proc)

    Function that fetches a fresh token (e.g. calls your backend’s GET /ecf-token). Required.

  • cache_token (Proc, nil) (defaults to: nil)

    Function to cache the token. Defaults to file-based cache at ~/.ecf-dgii/token.

  • get_cached_token (Proc, nil) (defaults to: nil)

    Function to retrieve a cached token. Defaults to file-based cache.

  • base_url (String, nil) (defaults to: nil)

    Base URL override. Takes precedence over environment.

  • environment (Symbol) (defaults to: :test)

    Target environment (:test, :cert, :prod). Defaults to :test.

  • timeout (Integer) (defaults to: 30)

    HTTP timeout in seconds. Defaults to 30.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ecf_dgii/frontend_client.rb', line 48

def initialize(get_token:, cache_token: nil, get_cached_token: nil,
               base_url: nil, environment: :test, timeout: 30)
  raise ArgumentError, "get_token is required for EcfDgii::FrontendClient" unless get_token

  resolved_url = base_url || ENVIRONMENT_URLS[environment.to_sym]
  raise ArgumentError, "Invalid environment or base URL" if resolved_url.nil? || resolved_url.empty?

  @get_token = get_token
  @cache_token = cache_token || method(:default_cache_token)
  @get_cached_token = get_cached_token || method(:default_get_cached_token)

  config = EcfDgii::Generated::Configuration.new
  uri = URI.parse(resolved_url)

  config.scheme = uri.scheme
  config.host = uri.host
  config.base_path = uri.path.empty? ? "" : uri.path
  config.timeout = timeout

  @api_client = EcfDgii::Generated::ApiClient.new(config)
  @environment = environment.to_sym
end

Instance Attribute Details

#api_clientEcfDgii::Generated::ApiClient (readonly)

Returns The underlying generated API client.

Returns:



31
32
33
# File 'lib/ecf_dgii/frontend_client.rb', line 31

def api_client
  @api_client
end

#environmentSymbol (readonly)

Returns The configured environment.

Returns:

  • (Symbol)

    The configured environment.



34
35
36
# File 'lib/ecf_dgii/frontend_client.rb', line 34

def environment
  @environment
end

Instance Method Details

#company_apiObject



79
80
81
# File 'lib/ecf_dgii/frontend_client.rb', line 79

def company_api
  @company_api ||= EcfDgii::Generated::CompanyApi.new(token_api_client)
end

#ecf_apiObject


Base API clients (lazy-loaded)




75
76
77
# File 'lib/ecf_dgii/frontend_client.rb', line 75

def ecf_api
  @ecf_api ||= EcfDgii::Generated::EcfApi.new(token_api_client)
end

#get_companies(opts = {}) ⇒ Object

List companies with optional filters.



112
113
114
# File 'lib/ecf_dgii/frontend_client.rb', line 112

def get_companies(opts = {})
  company_api.get_companies(opts)
end

#get_company_by_rnc(rnc) ⇒ Object

Get a company by RNC.



117
118
119
# File 'lib/ecf_dgii/frontend_client.rb', line 117

def get_company_by_rnc(rnc)
  company_api.get_company_by_rnc(rnc)
end

#get_ecf_by_id(rnc, id) ⇒ Object

Get a specific ECF by message ID.



103
104
105
# File 'lib/ecf_dgii/frontend_client.rb', line 103

def get_ecf_by_id(rnc, id)
  ecf_api.get_ecf_by_id(rnc, id)
end

#query_ecf(rnc, encf, opts = {}) ⇒ Object

Query ECFs by RNC and eNCF.



88
89
90
# File 'lib/ecf_dgii/frontend_client.rb', line 88

def query_ecf(rnc, encf, opts = {})
  ecf_api.query_ecf(rnc, encf, opts)
end

#search_all_ecfs(opts = {}) ⇒ Object

Search all ECFs across all companies.



98
99
100
# File 'lib/ecf_dgii/frontend_client.rb', line 98

def search_all_ecfs(opts = {})
  ecf_api.search_all_ecfs(opts)
end

#search_ecfs(rnc, opts = {}) ⇒ Object

Search ECFs for a specific RNC.



93
94
95
# File 'lib/ecf_dgii/frontend_client.rb', line 93

def search_ecfs(rnc, opts = {})
  ecf_api.search_ecfs(rnc, opts)
end