Class: Emailverify::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/emailverify/request.rb

Constant Summary collapse

DEFAULT_HEADERS =
{ "Content-Type" => "application/json" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: nil) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/emailverify/request.rb', line 10

def initialize(api_key: nil, base_url: nil, timeout: nil)
  @config = Emailverify.configuration
  @api_key = api_key || @config.api_key
  @base_url = base_url || @config.base_url
  @timeout = timeout || @config.timeout

  raise AuthenticationError, "API key missing" unless @api_key
  raise RequestError, "base_url must be configured" unless @base_url

  @conn = Faraday.new(url: @base_url) do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
    faraday.options.timeout = @timeout
  end
end

Instance Method Details

#check_balance(batch_id = nil, endpoint: nil) ⇒ Object

Check balance or status of a completed batch. Accepts an id or options. Check account balance. By default this calls the EmailVerify.io endpoint /api/v2/check-account-balance and passes the API key as the ‘key` query param (app.emailverify.io/api/v2/check-account-balance?key=<Your_API_Key>) If your provider uses a different pattern you may pass `endpoint:` or override `Emailverify.configuration.endpoints`.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/emailverify/request.rb', line 63

def check_balance(batch_id = nil, endpoint: nil)
  path = endpoint || @config.endpoints[:check_balance]

  # If a batch_id is provided, append it to the path (useful for batch status
  # endpoints that accept an id as part of the path).
  if batch_id
    path = File.join(path, batch_id.to_s)
  end

  # EmailVerify.io expects the API key as query param `key` for this endpoint.
  params = { key: @api_key }

  request(:get, path, params: params)
end

#valid?(email, endpoint: nil) ⇒ Boolean

Return boolean true/false for whether the email is valid. For EmailVerify.io v1 the API returns ‘status` with values like “valid”, “invalid” and may include `sub_status` such as “permitted”. This method returns true when `status` equals “valid” (case-insensitive).

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/emailverify/request.rb', line 41

def valid?(email, endpoint: nil)
  resp = validate(email, endpoint: endpoint)

  # Support Response objects or raw Hashes
  status_value = if resp.respond_to?(:status)
    resp.status
  elsif resp.is_a?(Hash)
    resp["status"] || resp[:status]
  else
    nil
  end

  return false unless status_value
  status_value.to_s.strip.downcase == "valid"
end

#validate(email, endpoint: nil) ⇒ Object

Validate a single email address. Returns parsed JSON from the API. By default this uses EmailVerify.io’s v1 endpoint which expects GET with query params ‘key` and `email`, e.g.: app.emailverify.io/api/v1/validate?key=yourapikey&[email protected] You can override the endpoint path via `endpoint:` if the API differs.



31
32
33
34
35
# File 'lib/emailverify/request.rb', line 31

def validate(email, endpoint: nil)
  path = endpoint || @config.endpoints[:validate]
  params = { key: @api_key, email: email }
  request(:get, path, params: params)
end