Skip to content

Instantly share code, notes, and snippets.

@oboxodo
Last active January 17, 2019 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oboxodo/209f3d4399c1950a8a1429923738015b to your computer and use it in GitHub Desktop.
Save oboxodo/209f3d4399c1950a8a1429923738015b to your computer and use it in GitHub Desktop.
BriteVerify simple email validation client supporting configurable timeouts
class BriteVerify
require "json"
require "faraday"
URL = "https://bpi.briteverify.com"
PATH = "/emails.json"
DEFAULT_TIMEOUT = 1.freeze
class Error < StandardError; end
attr_reader :key, :open_timeout, :timeout
def initialize(key: ENV['BRITEVERIFY_API_KEY'], open_timeout: DEFAULT_TIMEOUT, timeout: DEFAULT_TIMEOUT)
@key = key
@open_timeout = open_timeout
@timeout = timeout
end
def verify(address)
JSON(connection.get(PATH, address: address, apikey: key).body)
rescue Faraday::Error => e
raise Error.new(e)
end
private
def connection
@conn ||=
Faraday.new(
url: URL,
request: {
open_timeout: open_timeout, # opening a connection
timeout: timeout # waiting for response
}
)
end
end
BriteVerify.new(key: "something").verify("foo@example.com")
#=> {"address"=>"foo@example.com", "account"=>"foo", "domain"=>"example.com", "status"=>"invalid", "connected"=>nil, "disposable"=>false, "role_address"=>false, "error_code"=>"email_domain_invalid", "error"=>"Email domain invalid", "duration"=>0.02342901}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment