Skip to content

Instantly share code, notes, and snippets.

@oboxodo oboxodo/brite_verify.rb
Last active Jan 17, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.