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
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.
-
#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.
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_key ⇒ String
18 19 20 |
# File 'lib/ruby/whatsapp/client.rb', line 18 def api_key @api_key end |
#host ⇒ String
10 11 12 |
# File 'lib/ruby/whatsapp/client.rb', line 10 def host @host end |
#phone_id ⇒ String
22 23 24 |
# File 'lib/ruby/whatsapp/client.rb', line 22 def phone_id @phone_id end |
#version ⇒ String
14 15 16 |
# File 'lib/ruby/whatsapp/client.rb', line 14 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.
28 29 30 |
# File 'lib/ruby/whatsapp/client.rb', line 28 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.
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.
78 79 80 |
# File 'lib/ruby/whatsapp/client.rb', line 78 def path_for(*segments) "/#{[version, *segments].join('/')}" end |