Class: Apiddress::Client
- Inherits:
-
Object
- Object
- Apiddress::Client
- Defined in:
- lib/apiddress/client.rb
Overview
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.apiddress.com"- DEFAULT_TIMEOUT =
seconds
10- DEFAULT_MAX_RETRIES =
2
Instance Method Summary collapse
-
#create_batch(emails, callback_url: nil, check_smtp: false) ⇒ BatchAcceptedResponse
Create an asynchronous batch job for 1-5000 email addresses.
-
#get_batch(batch_id) ⇒ BatchStatusResponse
Get the current state (and results, when completed) of a batch job.
-
#health ⇒ HealthResponse
Service health check.
-
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) ⇒ Client
constructor
A new instance of Client.
-
#me ⇒ ApiKeyProfileResponse
Get the profile (plan, limits, usage) of the authenticated API key.
-
#usage(month = nil) ⇒ UsageResponse
Get usage for a month (“YYYY-MM”).
-
#validate_email(email, check_smtp: false, allow_role_based: nil) ⇒ ValidateEmailResponse
Validate a single email address.
-
#validate_emails(emails, check_smtp: false) ⇒ BulkValidateResponse
Validate 1-100 email addresses synchronously.
-
#wait_for_batch(batch_id, poll_interval: 1.0, timeout: 60.0) ⇒ BatchStatusResponse
Poll a batch job until it reaches a terminal state (“completed” or “failed”).
Constructor Details
#initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 |
# File 'lib/apiddress/client.rb', line 21 def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, max_retries: DEFAULT_MAX_RETRIES) raise ArgumentError, "Apiddress::Client: api_key must not be empty" if api_key.nil? || api_key.empty? @api_key = api_key @base_url = base_url.chomp("/") @timeout = timeout @max_retries = max_retries end |
Instance Method Details
#create_batch(emails, callback_url: nil, check_smtp: false) ⇒ BatchAcceptedResponse
Create an asynchronous batch job for 1-5000 email addresses. Never retried automatically (to avoid duplicate jobs).
68 69 70 71 72 73 74 |
# File 'lib/apiddress/client.rb', line 68 def create_batch(emails, callback_url: nil, check_smtp: false) body = { emails:, check_smtp: } body[:callback_url] = callback_url unless callback_url.nil? BatchAcceptedResponse.from_hash( request(:post, "/api/v1/batches", body:, retryable: false), ) end |
#get_batch(batch_id) ⇒ BatchStatusResponse
Get the current state (and results, when completed) of a batch job.
80 81 82 83 84 |
# File 'lib/apiddress/client.rb', line 80 def get_batch(batch_id) BatchStatusResponse.from_hash( request(:get, "/api/v1/batches/#{URI.encode_www_form_component(batch_id)}"), ) end |
#health ⇒ HealthResponse
Service health check. Does not require authentication.
130 131 132 |
# File 'lib/apiddress/client.rb', line 130 def health HealthResponse.from_hash(request(:get, "/api/v1/health", auth: false)) end |
#me ⇒ ApiKeyProfileResponse
Get the profile (plan, limits, usage) of the authenticated API key.
116 117 118 |
# File 'lib/apiddress/client.rb', line 116 def me ApiKeyProfileResponse.from_hash(request(:get, "/api/v1/me")) end |
#usage(month = nil) ⇒ UsageResponse
Get usage for a month (“YYYY-MM”). Defaults to the current month.
123 124 125 126 |
# File 'lib/apiddress/client.rb', line 123 def usage(month = nil) query = month ? { month: } : nil UsageResponse.from_hash(request(:get, "/api/v1/usage", query:)) end |
#validate_email(email, check_smtp: false, allow_role_based: nil) ⇒ ValidateEmailResponse
Validate a single email address. Costs 1 request.
40 41 42 43 44 |
# File 'lib/apiddress/client.rb', line 40 def validate_email(email, check_smtp: false, allow_role_based: nil) body = { email: email, check_smtp: check_smtp } body[:allow_role_based] = allow_role_based unless allow_role_based.nil? ValidateEmailResponse.from_hash(request(:post, "/api/v1/validate-email", body:)) end |
#validate_emails(emails, check_smtp: false) ⇒ BulkValidateResponse
Validate 1-100 email addresses synchronously. Costs one request per email.
51 52 53 54 55 |
# File 'lib/apiddress/client.rb', line 51 def validate_emails(emails, check_smtp: false) BulkValidateResponse.from_hash( request(:post, "/api/v1/validate-emails", body: { emails:, check_smtp: }), ) end |
#wait_for_batch(batch_id, poll_interval: 1.0, timeout: 60.0) ⇒ BatchStatusResponse
Poll a batch job until it reaches a terminal state (“completed” or “failed”).
Returns the final state. Raises Error with code “timeout” if the job is still running after timeout seconds.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/apiddress/client.rb', line 95 def wait_for_batch(batch_id, poll_interval: 1.0, timeout: 60.0) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout loop do batch = get_batch(batch_id) return batch if %w[completed failed].include?(batch.status) if Process.clock_gettime(Process::CLOCK_MONOTONIC) + poll_interval > deadline raise Error.new(0, "timeout", "Batch #{batch_id} did not complete within #{timeout}s (last status: #{batch.status})") end sleep(poll_interval) end end |