Class: FlowChat::Whatsapp::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/whatsapp/configuration.rb

Constant Summary collapse

@@configurations =

Class-level storage for named configurations

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Configuration

Returns a new instance of Configuration.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flow_chat/whatsapp/configuration.rb', line 10

def initialize(name)
  @name = name
  @access_token = nil
  @phone_number_id = nil
  @verify_token = nil
  @app_id = nil
  @app_secret = nil
  @webhook_verify_token = nil
  @business_account_id = nil
  @skip_signature_validation = false

  FlowChat.logger.debug { "WhatsApp::Configuration: Initialized configuration with name: #{name || "anonymous"}" }

  register_as(name) if name.present?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def access_token
  @access_token
end

#app_idObject

Returns the value of attribute app_id.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def app_secret
  @app_secret
end

#business_account_idObject

Returns the value of attribute business_account_id.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def 
  @business_account_id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def name
  @name
end

#phone_number_idObject

Returns the value of attribute phone_number_id.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def phone_number_id
  @phone_number_id
end

#skip_signature_validationObject

Returns the value of attribute skip_signature_validation.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def skip_signature_validation
  @skip_signature_validation
end

#verify_tokenObject

Returns the value of attribute verify_token.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def verify_token
  @verify_token
end

#webhook_verify_tokenObject

Returns the value of attribute webhook_verify_token.



4
5
6
# File 'lib/flow_chat/whatsapp/configuration.rb', line 4

def webhook_verify_token
  @webhook_verify_token
end

Class Method Details

.clear_all!Object

Clear all registered configurations (useful for testing)



96
97
98
99
# File 'lib/flow_chat/whatsapp/configuration.rb', line 96

def self.clear_all!
  FlowChat.logger.debug { "WhatsApp::Configuration: Clearing all registered configurations" }
  @@configurations.clear
end

.configuration_namesObject

Get all configuration names



89
90
91
92
93
# File 'lib/flow_chat/whatsapp/configuration.rb', line 89

def self.configuration_names
  names = @@configurations.keys
  FlowChat.logger.debug { "WhatsApp::Configuration: Available configurations: #{names}" }
  names
end

.exists?(name) ⇒ Boolean

Check if a named configuration exists

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/flow_chat/whatsapp/configuration.rb', line 82

def self.exists?(name)
  exists = @@configurations.key?(name.to_sym)
  FlowChat.logger.debug { "WhatsApp::Configuration: Configuration '#{name}' exists: #{exists}" }
  exists
end

.from_credentialsObject

Load configuration from Rails credentials or environment variables



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flow_chat/whatsapp/configuration.rb', line 27

def self.from_credentials
  FlowChat.logger.info { "WhatsApp::Configuration: Loading configuration from credentials/environment" }

  config = new(nil)

  if defined?(Rails) && Rails.application.credentials.whatsapp
    FlowChat.logger.debug { "WhatsApp::Configuration: Loading from Rails credentials" }
    credentials = Rails.application.credentials.whatsapp
    config.access_token = credentials[:access_token]
    config.phone_number_id = credentials[:phone_number_id]
    config.verify_token = credentials[:verify_token]
    config.app_id = credentials[:app_id]
    config.app_secret = credentials[:app_secret]
    config. = credentials[:business_account_id]
    config.skip_signature_validation = credentials[:skip_signature_validation] || false
  else
    FlowChat.logger.debug { "WhatsApp::Configuration: Loading from environment variables" }
    # Fallback to environment variables
    config.access_token = ENV["WHATSAPP_ACCESS_TOKEN"]
    config.phone_number_id = ENV["WHATSAPP_PHONE_NUMBER_ID"]
    config.verify_token = ENV["WHATSAPP_VERIFY_TOKEN"]
    config.app_id = ENV["WHATSAPP_APP_ID"]
    config.app_secret = ENV["WHATSAPP_APP_SECRET"]
    config. = ENV["WHATSAPP_BUSINESS_ACCOUNT_ID"]
    config.skip_signature_validation = ENV["WHATSAPP_SKIP_SIGNATURE_VALIDATION"] == "true"
  end

  if config.valid?
    FlowChat.logger.info { "WhatsApp::Configuration: Configuration loaded successfully - phone_number_id: #{config.phone_number_id}" }
  else
    FlowChat.logger.warn { "WhatsApp::Configuration: Incomplete configuration loaded - missing required fields" }
  end

  config
end

.get(name) ⇒ Object

Get a named configuration



70
71
72
73
74
75
76
77
78
79
# File 'lib/flow_chat/whatsapp/configuration.rb', line 70

def self.get(name)
  config = @@configurations[name.to_sym]
  if config
    FlowChat.logger.debug { "WhatsApp::Configuration: Retrieved configuration '#{name}'" }
    config
  else
    FlowChat.logger.error { "WhatsApp::Configuration: Configuration '#{name}' not found" }
    raise ArgumentError, "WhatsApp configuration '#{name}' not found"
  end
end

.register(name, config) ⇒ Object

Register a named configuration



64
65
66
67
# File 'lib/flow_chat/whatsapp/configuration.rb', line 64

def self.register(name, config)
  FlowChat.logger.debug { "WhatsApp::Configuration: Registering configuration '#{name}'" }
  @@configurations[name.to_sym] = config
end

Instance Method Details

#api_base_urlObject

Get API base URL from global config



132
133
134
# File 'lib/flow_chat/whatsapp/configuration.rb', line 132

def api_base_url
  FlowChat::Config.whatsapp.api_base_url
end

#api_headersObject

Headers for API requests



137
138
139
140
141
142
# File 'lib/flow_chat/whatsapp/configuration.rb', line 137

def api_headers
  {
    "Authorization" => "Bearer #{access_token}",
    "Content-Type" => "application/json"
  }
end

#media_url(media_id) ⇒ Object



123
124
125
# File 'lib/flow_chat/whatsapp/configuration.rb', line 123

def media_url(media_id)
  "#{FlowChat::Config.whatsapp.api_base_url}/#{media_id}"
end

#messages_urlObject

API endpoints



119
120
121
# File 'lib/flow_chat/whatsapp/configuration.rb', line 119

def messages_url
  "#{FlowChat::Config.whatsapp.api_base_url}/#{phone_number_id}/messages"
end

#phone_numbers_urlObject



127
128
129
# File 'lib/flow_chat/whatsapp/configuration.rb', line 127

def phone_numbers_url
  "#{FlowChat::Config.whatsapp.api_base_url}/#{}/phone_numbers"
end

#register_as(name) ⇒ Object

Register this configuration with a name



102
103
104
105
106
107
# File 'lib/flow_chat/whatsapp/configuration.rb', line 102

def register_as(name)
  FlowChat.logger.debug { "WhatsApp::Configuration: Registering configuration as '#{name}'" }
  @name = name.to_sym
  self.class.register(@name, self)
  self
end

#valid?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
# File 'lib/flow_chat/whatsapp/configuration.rb', line 109

def valid?
  is_valid = access_token && !access_token.to_s.empty? &&
    phone_number_id && !phone_number_id.to_s.empty? &&
    verify_token && !verify_token.to_s.empty?

  FlowChat.logger.debug { "WhatsApp::Configuration: Configuration valid: #{is_valid}" }
  is_valid
end