Class: Whatsapp::Client
- Inherits:
-
Object
- Object
- Whatsapp::Client
- 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
- #api_key ⇒ String
- #host ⇒ String
- #phone_id ⇒ String
- #version ⇒ String
-
#waba_id ⇒ String?
The WhatsApp Business Account ID.
Instance Method Summary collapse
-
#connection ⇒ HTTP::Client
Sets up and returns the persistent HTTP client, authenticated and instrumented.
-
#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
constructor
A new instance of Client.
-
#inspect ⇒ String
Redacts the api_key so it never leaks into logs or console output.
-
#path_for(*segments) ⇒ String
Builds a versioned request path.
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.
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_key ⇒ String
21 22 23 |
# File 'lib/ruby/whatsapp/client.rb', line 21 def api_key @api_key end |
#host ⇒ String
13 14 15 |
# File 'lib/ruby/whatsapp/client.rb', line 13 def host @host end |
#phone_id ⇒ String
25 26 27 |
# File 'lib/ruby/whatsapp/client.rb', line 25 def phone_id @phone_id end |
#version ⇒ String
17 18 19 |
# File 'lib/ruby/whatsapp/client.rb', line 17 def version @version end |
#waba_id ⇒ String?
The WhatsApp Business Account ID. Used by account-scoped endpoints such as template management, which address the WABA rather than a phone number.
31 32 33 |
# File 'lib/ruby/whatsapp/client.rb', line 31 def waba_id @waba_id end |
Instance Method Details
#connection ⇒ HTTP::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.
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 |
#inspect ⇒ String
Redacts the api_key so it never leaks into logs or console output.
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.
82 83 84 |
# File 'lib/ruby/whatsapp/client.rb', line 82 def path_for(*segments) "/#{[version, *segments].map { |segment| encode_segment(segment) }.join('/')}" end |