Class: Poodle::Client
- Inherits:
-
Object
- Object
- Poodle::Client
- Defined in:
- lib/poodle/client.rb
Overview
Main Poodle SDK client for sending emails
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
The configuration object.
-
#http_client ⇒ HttpClient
readonly
The HTTP client.
Instance Method Summary collapse
-
#create_email_from_hash(data) ⇒ Email
private
Create an Email object from hash data.
-
#extract_email_from_hash(data) ⇒ Email
private
Extract email object from hash data.
-
#initialize(config_or_api_key = nil, **options) ⇒ Client
constructor
Initialize a new Client.
-
#send(from:, to:, subject:, html: nil, text: nil) ⇒ EmailResponse
Send an email with individual parameters.
-
#send_email(email) ⇒ EmailResponse
Send an email using an Email object or hash.
-
#send_html(from:, to:, subject:, html:) ⇒ EmailResponse
Send an HTML email.
-
#send_text(from:, to:, subject:, text:) ⇒ EmailResponse
Send a plain text email.
-
#validate_required_email_fields(data) ⇒ Object
private
Validate required email fields.
-
#version ⇒ String
Get the SDK version.
Constructor Details
#initialize(config_or_api_key = nil, **options) ⇒ Client
Initialize a new Client
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, **) @config = case config_or_api_key when Configuration config_or_api_key when String Configuration.new( api_key: config_or_api_key, base_url: [:base_url], timeout: [:timeout], connect_timeout: [:connect_timeout], debug: .fetch(:debug, false), http_options: .fetch(:http_options, {}) ) when nil # Support keyword arguments for convenience Configuration.new(**) else raise ArgumentError, "Expected Configuration object, API key string, or nil" end @http_client = HttpClient.new(@config) end |
Instance Attribute Details
#config ⇒ Configuration (readonly)
Returns the configuration object.
33 34 35 |
# File 'lib/poodle/client.rb', line 33 def config @config end |
#http_client ⇒ HttpClient (readonly)
Returns the HTTP client.
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
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
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
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
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
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
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 |
#version ⇒ String
Get the SDK version
182 183 184 |
# File 'lib/poodle/client.rb', line 182 def version @config.class::SDK_VERSION end |