Class: KapsoClientRuby::Rails::Service
- Inherits:
-
Object
- Object
- KapsoClientRuby::Rails::Service
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/kapso_client_ruby/rails/service.rb
Overview
Service class for Rails integration with KapsoClientRuby Provides a convenient interface for Rails applications to interact with the Kapso API
Instance Attribute Summary collapse
-
#client ⇒ KapsoClientRuby::Client
readonly
The configured Kapso client.
Instance Method Summary collapse
-
#get_message_status(message_id) ⇒ Hash
Get message status.
-
#get_templates(**options) ⇒ Array
Get templates.
-
#initialize(client = nil) ⇒ Service
constructor
A new instance of Service.
-
#process_webhook(webhook_data) ⇒ Hash
Process incoming webhook.
-
#send_media_message(to:, media_type:, media_url:, **options) ⇒ Hash
Send a media message.
-
#send_template_message(to:, template_name:, language: 'en', components: []) ⇒ Hash
Send a template message.
-
#send_text_message(to:, text:, **options) ⇒ Hash
Send a text message.
-
#upload_media(file_path:, media_type:) ⇒ Hash
Upload media file.
Constructor Details
#initialize(client = nil) ⇒ Service
Returns a new instance of Service.
13 14 15 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 13 def initialize(client = nil) @client = client || KapsoClientRuby::Client.new end |
Instance Attribute Details
#client ⇒ KapsoClientRuby::Client (readonly)
Returns The configured Kapso client.
11 12 13 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 11 def client @client end |
Instance Method Details
#get_message_status(message_id) ⇒ Hash
Get message status
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 106 def () Rails.logger.debug "Getting status for message: #{}" result = client..get_status() Rails.logger.debug "Message status: #{result['status']}" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to get message status: #{e.}" raise end |
#get_templates(**options) ⇒ Array
Get templates
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 121 def get_templates(**) Rails.logger.debug "Fetching templates with options: #{}" result = client.templates.list(**) Rails.logger.debug "Found #{result['data']&.length || 0} templates" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to fetch templates: #{e.}" raise end |
#process_webhook(webhook_data) ⇒ Hash
Process incoming webhook
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 136 def process_webhook(webhook_data) Rails.logger.info "Processing webhook: #{webhook_data}" # Process webhook data based on type entries = webhook_data['entry'] || [] entries.each do |entry| changes = entry['changes'] || [] changes.each do |change| case change['field'] when 'messages' (change['value']) when 'message_template_status_update' process_template_status_webhook(change['value']) else Rails.logger.warn "Unknown webhook field: #{change['field']}" end end end { status: 'processed' } rescue => e Rails.logger.error "Failed to process webhook: #{e.}" raise end |
#send_media_message(to:, media_type:, media_url:, **options) ⇒ Hash
Send a media message
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 67 def (to:, media_type:, media_url:, **) Rails.logger.info "Sending #{media_type} message to #{to}: #{media_url}" result = client..send_media( to: to, type: media_type, media_url: media_url, ** ) Rails.logger.info "Media message sent successfully. ID: #{result.dig('messages', 0, 'id')}" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to send media message: #{e.}" raise end |
#send_template_message(to:, template_name:, language: 'en', components: []) ⇒ Hash
Send a template message
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 44 def (to:, template_name:, language: 'en', components: []) Rails.logger.info "Sending template message '#{template_name}' to #{to}" result = client..send_template( to: to, name: template_name, language: language, components: components ) Rails.logger.info "Template message sent successfully. ID: #{result.dig('messages', 0, 'id')}" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to send template message: #{e.}" raise end |
#send_text_message(to:, text:, **options) ⇒ Hash
Send a text message
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 22 def (to:, text:, **) Rails.logger.info "Sending text message to #{to}: #{text.truncate(50)}" result = client..send_text( to: to, text: text, ** ) Rails.logger.info "Message sent successfully. ID: #{result.dig('messages', 0, 'id')}" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to send text message: #{e.}" raise end |
#upload_media(file_path:, media_type:) ⇒ Hash
Upload media file
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kapso_client_ruby/rails/service.rb', line 88 def upload_media(file_path:, media_type:) Rails.logger.info "Uploading media file: #{file_path}" result = client.media.upload( file_path: file_path, type: media_type ) Rails.logger.info "Media uploaded successfully. ID: #{result['id']}" result rescue KapsoClientRuby::Error => e Rails.logger.error "Failed to upload media: #{e.}" raise end |