Class: Anypost::Client

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

Overview

Client for the Anypost email API.

require "anypost"

client = Anypost::Client.new("ap_your_api_key") # or Anypost::Client.new to read ANYPOST_API_KEY
email = client.email.send(
  from: "Acme <you@yourdomain.com>",
  to: ["someone@example.com"],
  subject: "Hello",
  html: "<p>It worked.</p>"
)
email.id

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.anypost.com/v1"
DEFAULT_TIMEOUT =
30
DEFAULT_MAX_RETRIES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES, default_headers: {}, connection: nil, sleeper: nil, jitter: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    defaults to the ANYPOST_API_KEY environment variable



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/anypost/client.rb', line 37

def initialize(api_key = nil, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT,
  max_retries: DEFAULT_MAX_RETRIES, default_headers: {}, connection: nil, sleeper: nil, jitter: nil)
  key = api_key
  key = ENV["ANYPOST_API_KEY"] if key.nil? || key.empty?
  if key.nil? || key.empty?
    raise ArgumentError,
      "An Anypost API key is required. Pass it to the constructor or set ANYPOST_API_KEY."
  end

  http = HttpClient.new(
    api_key: key,
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    default_headers: default_headers,
    connection: connection,
    sleeper: sleeper,
    jitter: jitter
  )

  @email = Resources::Email.new(http)
  @domains = Resources::Domains.new(http)
  @api_keys = Resources::ApiKeys.new(http)
  @templates = Resources::Templates.new(http)
  @suppressions = Resources::Suppressions.new(http)
  @webhooks = Resources::Webhooks.new(http)
  @events = Resources::Events.new(http)
  @identity = Resources::Identity.new(http)
end

Instance Attribute Details

#api_keysResources::ApiKeys (readonly)

Returns API-key operations (‘/api-keys`).

Returns:



26
27
28
# File 'lib/anypost/client.rb', line 26

def api_keys
  @api_keys
end

#domainsResources::Domains (readonly)

Returns sending-domain operations (‘/domains`).

Returns:



24
25
26
# File 'lib/anypost/client.rb', line 24

def domains
  @domains
end

#emailResources::Email (readonly)

Returns send operations (‘/email`, `/email/batch`).

Returns:



22
23
24
# File 'lib/anypost/client.rb', line 22

def email
  @email
end

#eventsResources::Events (readonly)

Returns read access to the event stream (‘/events`).

Returns:



34
35
36
# File 'lib/anypost/client.rb', line 34

def events
  @events
end

#suppressionsResources::Suppressions (readonly)

Returns suppression-list operations (‘/suppressions`).

Returns:



30
31
32
# File 'lib/anypost/client.rb', line 30

def suppressions
  @suppressions
end

#templatesResources::Templates (readonly)

Returns template operations, including draft/publish.

Returns:



28
29
30
# File 'lib/anypost/client.rb', line 28

def templates
  @templates
end

#webhooksResources::Webhooks (readonly)

Returns webhook operations, including test and rotation.

Returns:



32
33
34
# File 'lib/anypost/client.rb', line 32

def webhooks
  @webhooks
end

Instance Method Details

#whoamiObject

Identify the team and permission level behind the current API key.



68
69
70
# File 'lib/anypost/client.rb', line 68

def whoami
  @identity.whoami
end