Class: Poodle::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/poodle/client.rb

Overview

Main Poodle SDK client for sending emails

Examples:

Basic usage

client = Poodle::Client.new(api_key: "your_api_key")
response = client.send(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello World",
  html: "<h1>Hello!</h1>"
)

Using configuration object

config = Poodle::Configuration.new(
  api_key: "your_api_key",
  debug: true
)
client = Poodle::Client.new(config)

Using environment variables

ENV["POODLE_API_KEY"] = "your_api_key"
client = Poodle::Client.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_or_api_key = nil, **options) ⇒ Client

Initialize a new Client

Examples:

With API key

client = Poodle::Client.new("your_api_key")

With configuration

config = Poodle::Configuration.new(api_key: "your_api_key")
client = Poodle::Client.new(config)

With keyword arguments

client = Poodle::Client.new(
  api_key: "your_api_key",
  debug: true,
  timeout: 60
)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/poodle/client.rb', line 60

def initialize(config_or_api_key = nil, **options)
  @config = case config_or_api_key
            when Configuration
              config_or_api_key
            when String
              Configuration.new(
                api_key: config_or_api_key,
                base_url: options[:base_url],
                timeout: options[:timeout],
                connect_timeout: options[:connect_timeout],
                debug: options.fetch(:debug, false),
                http_options: options.fetch(:http_options, {})
              )
            when nil
              # Support keyword arguments for convenience
              Configuration.new(**options)
            else
              raise ArgumentError, "Expected Configuration object, API key string, or nil"
            end

  @http_client = HttpClient.new(@config)
end

Instance Attribute Details

#configConfiguration (readonly)



33
34
35
# File 'lib/poodle/client.rb', line 33

def config
  @config
end

#http_clientHttpClient (readonly)



36
37
38
# File 'lib/poodle/client.rb', line 36

def http_client
  @http_client
end

Instance Method Details

#create_email_from_hash(data) ⇒ Email (private)

Create an Email object from hash data

Raises:



193
194
195
196
# File 'lib/poodle/client.rb', line 193

def create_email_from_hash(data)
  validate_required_email_fields(data)
  extract_email_from_hash(data)
end

#extract_email_from_hash(data) ⇒ Email (private)

Extract email object from hash data



212
213
214
215
216
217
218
219
220
# File 'lib/poodle/client.rb', line 212

def extract_email_from_hash(data)
  Email.new(
    from: data[:from] || data["from"],
    to: data[:to] || data["to"],
    subject: data[:subject] || data["subject"],
    html: data[:html] || data["html"],
    text: data[:text] || data["text"]
  )
end

#send(from:, to:, subject:, html: nil, text: nil) ⇒ EmailResponse

Send an email with individual parameters

Examples:

response = client.send(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello World",
  html: "<h1>Hello!</h1>",
  text: "Hello!"
)

Raises:



132
133
134
135
# File 'lib/poodle/client.rb', line 132

def send(from:, to:, subject:, html: nil, text: nil)
  email = Email.new(from: from, to: to, subject: subject, html: html, text: text)
  send_email(email)
end

#send_email(email) ⇒ EmailResponse

Send an email using an Email object or hash

Examples:

With Email object

email = Poodle::Email.new(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello",
  html: "<h1>Hello!</h1>"
)
response = client.send_email(email)

With hash

response = client.send_email({
  from: "[email protected]",
  to: "[email protected]",
  subject: "Hello",
  text: "Hello!"
})

Raises:



106
107
108
109
110
111
# File 'lib/poodle/client.rb', line 106

def send_email(email)
  email_obj = email.is_a?(Email) ? email : create_email_from_hash(email)

  response_data = @http_client.post("v1/send-email", email_obj.to_h)
  EmailResponse.from_api_response(response_data)
end

#send_html(from:, to:, subject:, html:) ⇒ EmailResponse

Send an HTML email

Examples:

response = client.send_html(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Newsletter",
  html: "<h1>Newsletter</h1><p>Content here</p>"
)

Raises:



154
155
156
# File 'lib/poodle/client.rb', line 154

def send_html(from:, to:, subject:, html:)
  send(from: from, to: to, subject: subject, html: html)
end

#send_text(from:, to:, subject:, text:) ⇒ EmailResponse

Send a plain text email

Examples:

response = client.send_text(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Simple notification",
  text: "This is a simple text notification."
)

Raises:



175
176
177
# File 'lib/poodle/client.rb', line 175

def send_text(from:, to:, subject:, text:)
  send(from: from, to: to, subject: subject, text: text)
end

#validate_required_email_fields(data) ⇒ Object (private)

Validate required email fields

Raises:



202
203
204
205
206
# File 'lib/poodle/client.rb', line 202

def validate_required_email_fields(data)
  raise ValidationError.missing_field("from") unless data[:from] || data["from"]
  raise ValidationError.missing_field("to") unless data[:to] || data["to"]
  raise ValidationError.missing_field("subject") unless data[:subject] || data["subject"]
end

#versionString

Get the SDK version



182
183
184
# File 'lib/poodle/client.rb', line 182

def version
  @config.class::SDK_VERSION
end