Class: ChainMail::Providers::SendPulse

Inherits:
Base
  • Object
show all
Defined in:
lib/chain_mail/providers/send_pulse.rb

Class Method Summary collapse

Methods inherited from Base

post_json

Class Method Details

.build_email_payload(mail) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chain_mail/providers/send_pulse.rb', line 42

def self.build_email_payload(mail)
  {
    email: {
      from: { name: "ChainMail", email: mail.from.first },
      to: mail.to.map { |email| { name: "", email: email } },
      subject: mail.subject,
      htmlbody: mail.body.decoded,
      textbody: generate_plain_text(mail),
      attachments: []
    }
  }
end

.deliver(mail, creds) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chain_mail/providers/send_pulse.rb', line 63

def self.deliver(mail, creds)
  # Validate credentials
  validation_result = validate_credentials(creds)
  return validation_result unless validation_result[:success]

  # Initialize API
  user_id = creds[:client_id]
  secret = creds[:client_secret]
  api_result = initialize_api(user_id, secret)
  return api_result unless api_result[:success]

  # Build email payload and send
  email_payload = build_email_payload(mail)
  begin
    result = api_result[:api].smtp_send_mail(email_payload[:email])
    # Handle API result
    handle_api_result(result)
  rescue StandardError => e
    {
      success: false,
      response: nil,
      error: e.message
    }
  end
end

.generate_plain_text(mail) ⇒ Object



38
39
40
# File 'lib/chain_mail/providers/send_pulse.rb', line 38

def self.generate_plain_text(mail)
  mail.text_part&.decoded || mail.body.decoded.gsub(/<[^>]*>/, "")
end

.handle_api_result(result) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/chain_mail/providers/send_pulse.rb', line 55

def self.handle_api_result(result)
  if result[:is_error]
    { success: false, response: nil, error: result[:message] || "SendPulse API error" }
  else
    { success: true, response: result[:data], error: nil }
  end
end

.initialize_api(user_id, secret) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/chain_mail/providers/send_pulse.rb', line 27

def self.initialize_api(user_id, secret)
  api = SendpulseApi.new(user_id, secret)
  { success: true, api: api }
rescue StandardError => e
  {
    success: false,
    response: nil,
    error: "Failed to initialize SendPulse API: #{e.message}"
  }
end

.validate_credentials(creds) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chain_mail/providers/send_pulse.rb', line 11

def self.validate_credentials(creds)
  user_id = creds[:client_id]
  secret = creds[:client_secret]

  if user_id.nil? || user_id.to_s.strip.empty? ||
     secret.nil? || secret.to_s.strip.empty?
    {
      success: false,
      response: nil,
      error: "Missing SendPulse credentials: client_id and client_secret are required"
    }
  else
    { success: true }
  end
end