Class: Whatsapp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/whatsapp/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

Default request timeout in seconds.

30
SEGMENT_UNRESERVED =

Characters that must be percent-escaped in a path segment (everything except RFC 3986 "unreserved").

/[^A-Za-z0-9\-._~]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: Whatsapp.configuration.host, version: Whatsapp.configuration.version, api_key: Whatsapp.configuration.api_key, phone_id: Whatsapp.configuration.phone_id, waba_id: Whatsapp.configuration.waba_id, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String) (defaults to: Whatsapp.configuration.host)

    The API origin (e.g. "https://graph.facebook.com")

  • version (String) (defaults to: Whatsapp.configuration.version)

    The API version (e.g. "v24.0")

  • api_key (String) (defaults to: Whatsapp.configuration.api_key)

    The API key for authentication

  • phone_id (String) (defaults to: Whatsapp.configuration.phone_id)

    The WhatsApp phone number ID

  • waba_id (String) (defaults to: Whatsapp.configuration.waba_id)

    The WhatsApp Business Account ID

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    The request timeout in seconds

  • logger (Logger, nil) (defaults to: nil)

    The logger instance for instrumentation (optional)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby/whatsapp/client.rb', line 40

def initialize(
  host: Whatsapp.configuration.host,
  version: Whatsapp.configuration.version,
  api_key: Whatsapp.configuration.api_key,
  phone_id: Whatsapp.configuration.phone_id,
  waba_id: Whatsapp.configuration.waba_id,
  timeout: DEFAULT_TIMEOUT,
  logger: nil
)
  @host = host
  @version = version
  @api_key = api_key
  @phone_id = phone_id
  @waba_id = waba_id
  @timeout = timeout
  @logger = logger
end

Instance Attribute Details

#api_keyString

Returns:

  • (String)


21
22
23
# File 'lib/ruby/whatsapp/client.rb', line 21

def api_key
  @api_key
end

#hostString

Returns:

  • (String)


13
14
15
# File 'lib/ruby/whatsapp/client.rb', line 13

def host
  @host
end

#phone_idString

Returns:

  • (String)


25
26
27
# File 'lib/ruby/whatsapp/client.rb', line 25

def phone_id
  @phone_id
end

#versionString

Returns:

  • (String)


17
18
19
# File 'lib/ruby/whatsapp/client.rb', line 17

def version
  @version
end

#waba_idString?

The WhatsApp Business Account ID. Used by account-scoped endpoints such as template management, which address the WABA rather than a phone number.

Returns:

  • (String, nil)


31
32
33
# File 'lib/ruby/whatsapp/client.rb', line 31

def waba_id
  @waba_id
end

Instance Method Details

#connectionHTTP::Client

Sets up and returns the persistent HTTP client, authenticated and instrumented.

The persistent base is the origin only — HTTP.persistent normalizes its argument to the origin, so the API version must NOT be baked into it or it is silently dropped. The version is carried per-request via #path_for.

Returns:

  • (HTTP::Client)

    The configured HTTP client



64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby/whatsapp/client.rb', line 64

def connection
  @connection ||= begin
    http = HTTP.persistent(@host)
    http = http.auth("Bearer #{@api_key}") if @api_key
    http = http.use(instrumentation: { instrumenter: Instrumentation.new(@logger) }) if @logger
    http = http.timeout(@timeout) if @timeout

    http
  end
end

#inspectString

Redacts the api_key so it never leaks into logs or console output.

Returns:

  • (String)


88
89
90
91
92
# File 'lib/ruby/whatsapp/client.rb', line 88

def inspect
  redacted_api_key = api_key.nil? ? "nil" : "[REDACTED]"
  "#<#{self.class.name} host=#{host.inspect} version=#{version.inspect} " \
    "api_key=#{redacted_api_key} phone_id=#{phone_id.inspect} waba_id=#{waba_id.inspect}>"
end

#path_for(*segments) ⇒ String

Builds a versioned request path. Segments are percent-encoded so a caller-supplied ID can't inject a query string or redirect to a different Graph edge.

Examples:

path_for(phone_id, "messages") #=> "/v24.0/<phone_id>/messages"
path_for(media_id)             #=> "/v24.0/<media_id>"

Parameters:

  • segments (Array<String>)

    Path segments appended after the version.

Returns:

  • (String)

    The full request path.



82
83
84
# File 'lib/ruby/whatsapp/client.rb', line 82

def path_for(*segments)
  "/#{[version, *segments].map { |segment| encode_segment(segment) }.join('/')}"
end