Class: ISend::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/isend/client.rb

Constant Summary collapse

API_BASE_URL =
'https://www.isend.ai/api'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config = {}) ⇒ Client

Create a new ISend::Client instance

Parameters:

  • api_key (String)

    Your isend.ai API key

  • config (Hash) (defaults to: {})

    Additional configuration options

Options Hash (config):

  • :timeout (Integer) — default: 30

    Request timeout in seconds

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/isend/client.rb', line 18

def initialize(api_key, config = {})
  if api_key.nil? || api_key.strip.empty?
    raise ISend::InvalidArgumentError, 'API key is required'
  end
  
  @api_key = api_key.strip
  @timeout = config[:timeout] || 30
  
  # Configure HTTParty
  self.class.base_uri API_BASE_URL
  self.class.default_timeout @timeout
  self.class.headers({
    'Authorization' => "Bearer #{@api_key}",
    'Content-Type' => 'application/json',
    'User-Agent' => "isend-ai-ruby-sdk/#{ISend::VERSION}"
  })
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/isend/client.rb', line 10

def api_key
  @api_key
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



10
11
12
# File 'lib/isend/client.rb', line 10

def timeout
  @timeout
end

Instance Method Details

#send_email(email_data) ⇒ Hash

Send an email using isend.ai

Parameters:

  • email_data (Hash)

    Email data including template_id, to, dataMapping, etc.

Options Hash (email_data):

  • :template_id (Integer)

    The template ID to use

  • :to (String)

    Recipient email address

  • :dataMapping (Hash)

    Data mapping for template variables

Returns:

  • (Hash)

    Response from the API

Raises:



44
45
46
47
48
49
50
51
52
53
# File 'lib/isend/client.rb', line 44

def send_email(email_data)
  validate_email_data(email_data)
  
  response = self.class.post('/send-email', {
    body: email_data.to_json,
    headers: { 'Content-Type' => 'application/json' }
  })
  
  handle_response(response)
end