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

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)



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

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)


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

def api_key
  @api_key
end

#hostString

Returns:

  • (String)


10
11
12
# File 'lib/ruby/whatsapp/client.rb', line 10

def host
  @host
end

#phone_idString

Returns:

  • (String)


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

def phone_id
  @phone_id
end

#versionString

Returns:

  • (String)


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

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)


28
29
30
# File 'lib/ruby/whatsapp/client.rb', line 28

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby/whatsapp/client.rb', line 61

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

#path_for(*segments) ⇒ String

Builds a versioned request path.

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.



78
79
80
# File 'lib/ruby/whatsapp/client.rb', line 78

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