Class: CED::EmailFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ced/email_fetcher.rb

Constant Summary collapse

EMAIL_PATH =
"/api/v1/Email/validate"

Instance Method Summary collapse

Constructor Details

#initialize(host, client, key) ⇒ EmailFetcher

Returns a new instance of EmailFetcher.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
# File 'lib/ced/email_fetcher.rb', line 10

def initialize(host, client, key)
  raise ArgumentError, "Missing CED host"   if host.nil?   || host.strip.empty?
  raise ArgumentError, "Missing CED client" if client.nil? || client.strip.empty?
  raise ArgumentError, "Missing CED key"    if key.nil?    || key.strip.empty?
  @host   = host
  @client = client
  @key    = key
end

Instance Method Details

#fetch_email(address) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ced/email_fetcher.rb', line 26

def fetch_email(address)
  uri              = verification_uri(address)
  http             = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Host certificate does not cover the subdomain
  request          = Net::HTTP::Get.new(uri.request_uri)
  response         = http.request(request)
  EmailResponse.new(response)
end

#fetch_raw_email(address) ⇒ Object



19
20
21
22
23
24
# File 'lib/ced/email_fetcher.rb', line 19

def fetch_raw_email(address)
  email_response = fetch_email(address)
  email_response.raw_email
rescue Timeout::Error => e
  {}
end

#verification_uri(address) ⇒ Object



36
37
38
39
40
41
# File 'lib/ced/email_fetcher.rb', line 36

def verification_uri(address)
  arguments = [{email: address}].to_json
  signature = sign_arguments(arguments)
  query     = URI.encode_www_form(client: @client, arguments: arguments, signature: signature)
  URI::HTTPS.build({host: @host, path: EMAIL_PATH, query: query})
end