Class: KapsoClientRuby::Rails::Service

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#clientKapsoClientRuby::Client (readonly)

Returns The configured Kapso client.

Returns:



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

Parameters:

  • message_id (String)

    The message ID

Returns:

  • (Hash)

    Message status



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kapso_client_ruby/rails/service.rb', line 106

def get_message_status(message_id)
  Rails.logger.debug "Getting status for message: #{message_id}"
  
  result = client.messages.get_status(message_id)
  
  Rails.logger.debug "Message status: #{result['status']}"
  result
rescue KapsoClientRuby::Error => e
  Rails.logger.error "Failed to get message status: #{e.message}"
  raise
end

#get_templates(**options) ⇒ Array

Get templates

Parameters:

  • options (Hash)

    Query options

Returns:

  • (Array)

    List of 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(**options)
  Rails.logger.debug "Fetching templates with options: #{options}"
  
  result = client.templates.list(**options)
  
  Rails.logger.debug "Found #{result['data']&.length || 0} templates"
  result
rescue KapsoClientRuby::Error => e
  Rails.logger.error "Failed to fetch templates: #{e.message}"
  raise
end

#process_webhook(webhook_data) ⇒ Hash

Process incoming webhook

Parameters:

  • webhook_data (Hash)

    The webhook payload

Returns:

  • (Hash)

    Processed webhook data



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'
        process_message_webhook(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.message}"
  raise
end

#send_media_message(to:, media_type:, media_url:, **options) ⇒ Hash

Send a media message

Parameters:

  • to (String)

    The recipient’s phone number

  • media_type (String)

    Type of media (‘image’, ‘document’, ‘video’, ‘audio’)

  • media_url (String)

    URL of the media file

  • options (Hash)

    Additional options

Returns:

  • (Hash)

    API response



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 send_media_message(to:, media_type:, media_url:, **options)
  Rails.logger.info "Sending #{media_type} message to #{to}: #{media_url}"
  
  result = client.messages.send_media(
    to: to,
    type: media_type,
    media_url: media_url,
    **options
  )
  
  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.message}"
  raise
end

#send_template_message(to:, template_name:, language: 'en', components: []) ⇒ Hash

Send a template message

Parameters:

  • to (String)

    The recipient’s phone number

  • template_name (String)

    The template name

  • language (String) (defaults to: 'en')

    The language code (default: ‘en’)

  • components (Array) (defaults to: [])

    Template components

Returns:

  • (Hash)

    API response



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 send_template_message(to:, template_name:, language: 'en', components: [])
  Rails.logger.info "Sending template message '#{template_name}' to #{to}"
  
  result = client.messages.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.message}"
  raise
end

#send_text_message(to:, text:, **options) ⇒ Hash

Send a text message

Parameters:

  • to (String)

    The recipient’s phone number

  • text (String)

    The message text

  • options (Hash)

    Additional options

Returns:

  • (Hash)

    API response



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 send_text_message(to:, text:, **options)
  Rails.logger.info "Sending text message to #{to}: #{text.truncate(50)}"
  
  result = client.messages.send_text(
    to: to,
    text: text,
    **options
  )
  
  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.message}"
  raise
end

#upload_media(file_path:, media_type:) ⇒ Hash

Upload media file

Parameters:

  • file_path (String)

    Path to the media file

  • media_type (String)

    Type of media

Returns:

  • (Hash)

    Upload response with media ID



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.message}"
  raise
end