Module: BlackStack::Appending

Defined in:
lib/appending.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

@@logger =
nil
@@report =
nil
@@indexes =
[]
@@verifier_url =
'https://connectionsphere.com/api1.0/emails/verify.json'
@@verifier_api_key =
nil
@@email_fields =
[]
@@phone_fields =
[]
@@company_domain_fields =
[]
@@zerobounce_api_key =
nil
@@emailverify_api_key =
nil

Class Method Summary collapse

Class Method Details

.add_index(index) ⇒ Object

@@indexes



30
31
32
33
34
35
36
37
38
39
# File 'lib/appending.rb', line 30

def self.add_index(index)
    expected = [:company_name, :first_name, :last_name]

    # validation: keys must be `[:company_name, :first_name, :last_name]`
    if !index.keys.eql?(expected)
        raise "Invalid index: #{index.keys}. Expected: #{expected}."
    end
    # add the index
    @@indexes << index
end

.catch_all?(domain) ⇒ Boolean

return true if the domain get any random address as valid

This is a support method for the append methods. The end-user should not call this method directly.

Returns:

  • (Boolean)


214
215
216
# File 'lib/appending.rb', line 214

def self.catch_all?(domain)
    BlackStack::Appending.verify("008e77980535470e848a4ca859a83db0@#{domain}")
end

.cleanup_company(company) ⇒ Object

This is a support method for the append methods. The end-user should not call this method directly.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/appending.rb', line 253

def self.cleanup_company(company)
    return '' if company.nil?
    ret = ''
    # stage 1: remove company-type suffixes
    company = company.split(/ at /).last
    company.gsub!(/LLC/, '')
    company.gsub!(/Inc/, '')
    company.strip! # remove leading and trailing spaces
    # stage 2: remove LinkedIn suffixes            
    company.gsub!(/\(\d\d\d\d - Present\)/, '')
    company.strip! # remove leading and trailing spaces
    # stege 3: remove non-alphanumeric characters
    company.gsub!(/\.$/, '')  
    company.gsub!(/\,$/, '') 
    # stege 4: remove extra spaces
    company.gsub!(/[^a-zA-Z0-9,\.\-\s]/, '') # remove extra spaces
    company.strip! # remove leading and trailing spaces
    # stage 5: choose the first part of the company name
    company.split(' ').each { |word|
        ret += word + ' '
        #break if word.size >= 5 || ret.split(' ').size > 2
        break if ret.split(' ').size > 2
    } 
    ret.strip!
    # return
    ret
end

.cleanup_fname(name) ⇒ Object

This is a support method for the append methods. The end-user should not call this method directly.



237
238
239
240
241
# File 'lib/appending.rb', line 237

def self.cleanup_fname(name)
    return '' if name.nil?
    a = name.split(/[^a-zA-Z]/)
    a.size > 0 ? a[0] : ''
end

.cleanup_lname(name) ⇒ Object

This is a support method for the append methods. The end-user should not call this method directly.



245
246
247
248
249
# File 'lib/appending.rb', line 245

def self.cleanup_lname(name)
    return '' if name.nil?
    a = name.split(/[^a-zA-Z]/)
    a.size > 1 ? a[1] : ''
end

.company_domain_fieldsObject



95
96
97
# File 'lib/appending.rb', line 95

def self.company_domain_fields
    @@company_domain_fields
end

.email_fieldsObject



77
78
79
# File 'lib/appending.rb', line 77

def self.email_fields
    @@email_fields
end

.emailverify_credits?Boolean

call emailverify api to ask if there are credits

Returns:

  • (Boolean)


205
206
207
# File 'lib/appending.rb', line 205

def self.emailverify_credits?
    BlackStack::Appending.emailverify_verify('x') == 'error_credit'
end

.emailverify_verify(email) ⇒ Object

call emailverify api to verify an email reutrn ‘ok’ if the email is valid



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/appending.rb', line 192

def self.emailverify_verify(email)
    # validation: email must be a string
    raise "Invalid email: #{email.class}. Expected: String." if !email.is_a?(String)
    url = "https://apps.emaillistverify.com/api/verifEmail"
    params = {
        :secret => @@emailverify_api_key,
        :email => email,
    }
    res = BlackStack::Netting::call_get(url, params)
    res.body
end

.find_persons(fname, lname, cname, l = nil) ⇒ Object

Find a person in the indexes by its first name, last name and company name. Append all the information in the index row.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/appending.rb', line 299

def self.find_persons(fname, lname, cname, l=nil)
    l = BlackStack::DummyLogger.new if l.nil?
    h = {
        :matches => [],
        :enlapsed_seconds => 0,
        :files_processed => 0,
    }
    # cleaning up company name
    l.logs "Cleaning up company name #{cname}... "
    cname = BlackStack::Appending::cleanup_company(cname)
    l.logf cname
    # looking for a record that matches with first name, last name and company name
    appends = []
    enlapsed_seconds = 0
    files_processed = 0
    BlackStack::Appending.indexes.each { |i|
        l.logs "Searching into #{i.name}... "
        ret = i.find([cname, fname, lname], false, nil)
        # add the name of the index in the last position of the match
        ret[:matches].each { |m| m.unshift(i.name.to_s) }
        # add matches to the list
        h[:matches] += ret[:matches]
        # sum the total files and the total enlapsed seconds                
        h[:enlapsed_seconds] += ret[:enlapsed_seconds]
        h[:files_processed] += ret[:files_processed]
        l.done
    }
    # update report
    @@report = h
    # return results
    h[:matches].map { |m| BlackStack::Appending::Result.new(m) }
end

.find_persons_by_company(cname, l = nil) ⇒ Object

Find a company in the indexes by its first name, last name and company name. Append all the information in the index row.



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/appending.rb', line 334

def self.find_persons_by_company(cname, l=nil)
    l = BlackStack::DummyLogger.new if l.nil?
    h = {
        :matches => [],
        :enlapsed_seconds => 0,
        :files_processed => 0,
    }
    # looking for a record that matches with first name, last name and company name
    appends = []
    enlapsed_seconds = 0
    files_processed = 0
    BlackStack::Appending.indexes.each { |i|
        l.logs "Searching into #{i.name}... "
        ret = i.find([cname], true, nil)
        # add the name of the index in the last position of the match
        ret[:matches].each { |m| m.unshift(i.name.to_s) }
        # add matches to the list
        h[:matches] += ret[:matches]
        # sum the total files and the total enlapsed seconds                
        h[:enlapsed_seconds] += ret[:enlapsed_seconds]
        h[:files_processed] += ret[:files_processed]
        l.done
    }
    # update report
    @@report = h
    # return results
    h[:matches].map { |m| BlackStack::Appending::Result.new(m) }
end

.find_persons_with_full_name(name, cname, l = nil) ⇒ Object

Find a person in the indexes by its full name and company name. Append all the information in the index row.



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/appending.rb', line 283

def self.find_persons_with_full_name(name, cname, l=nil)
    l = BlackStack::DummyLogger.new if l.nil?

    l.logs "Guessing fname from #{name}... "
    fname = BlackStack::Appending::cleanup_fname(name)
    l.logf fname

    l.logs "Guessing lname from #{name}... "
    lname = BlackStack::Appending::cleanup_lname(name)
    l.logf lname

    BlackStack::Appending.find_persons(fname, lname, cname, l)
end

.find_verified_emails(fname, lname, cname, l = nil) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/appending.rb', line 377

def self.find_verified_emails(fname, lname, cname, l=nil)
    l = BlackStack::DummyLogger.new if l.nil?
    emails = []
    domains = []
    verified_emails = []
    # 
    return verified_emails if cname =~ /Self.*employed/i
    # get lead emails from in the indexes
    l.logs ("Searching index emails... ")
        emails = BlackStack::Appending.find_persons(fname, lname, cname, l).map { |res| 
            res.emails 
        }.flatten.uniq.reject { |email|
            email.to_s.empty?
        }.uniq
    # remove duplications
    # IMPORTANT: be sure you have no spances, and no duplications, in order to don't repeat this issue https://github.com/leandrosardi/emails/issues/124
    emails = emails.map { |email| email.to_s.downcase.strip }.uniq
    l.logf "done (#{emails.to_s})"
    # get company domains from the indexes
    l.logs ("Searching index domains... ")
        domains = BlackStack::Appending.find_persons_by_company(cname, l).map { |res| 
            res.company_domains
        }.flatten.reject { |email|
            email.to_s.empty?
        }.map { |domain|
            # normalize domain
            domain.to_s.gsub('www.', '').downcase
        }.uniq
    # remove duplications
    # IMPORTANT: be sure you have no spances, and no duplications, in order to don't repeat this issue https://github.com/leandrosardi/emails/issues/124
    domains = domains.map { |domain| domain.to_s.downcase.strip }.uniq
    l.logf "done (#{domains.to_s})"
    # add emails using fname and lname and company domains
    # if the domain has not a catch-all address, then add all possible email variations to the array `emails`.
    # if the domain has a catch-all address, then remove all emails with such a domain from the array `emails`.
    l.logs ("Building list of candidates... ")
    domains.each { |domain|
        if !BlackStack::Appending.catch_all?(domain)
            emails += [
                "#{fname}@#{domain}",
                "#{lname}@#{domain}",
                "#{fname}.#{lname}@#{domain}",
                "#{lname}.#{fname}@#{domain}",
                "#{fname}#{lname}@#{domain}",
                "#{lname}#{fname}@#{domain}",
                "#{fname[0]}#{lname}@#{domain}",
                "#{fname[0]}.#{lname}@#{domain}",
            ]
        else
            emails = emails.reject { |email| email =~ /@#{domain}$/ } 
        end
    }
    # delete duplicates
    emails.uniq!
    l.logf "done (#{emails.to_s})"
    # verify all the emails found in the indexes
    l.logs ("Verifying emails... ")
    emails.each { |email|
        l.logs "Verifying #{email}... "
        verified_emails << email if BlackStack::Appending.verify(email)
        l.done
        # IMPORTANT: I added this line in order to resolve this issue https://github.com/leandrosardi/emails/issues/124
        break if verified_emails.size > 0
    }
    l.done
    # verify all the emails found in the indexes
    # IMPORTANT: I added this double-check in order to don't repeat this issue https://github.com/leandrosardi/emails/issues/124
    l.logs ("Double check emails... ")
    verified_emails.each { |email|
        l.logs "Double-checking #{email}... "
        if BlackStack::Appending.verify(email)
            l.logf 'keep'
        else
            verified_emails.delete(email)
            l.logf "removed"
        end
    }
    l.done
    # return 
    verified_emails.uniq
end

.find_verified_emails_with_full_name(name, cname, l = nil) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/appending.rb', line 363

def self.find_verified_emails_with_full_name(name, cname, l=nil)
    l = BlackStack::DummyLogger.new if l.nil?

    l.logs "Guessing fname from #{name}... "
    fname = BlackStack::Appending::cleanup_fname(name)
    l.logf fname

    l.logs "Guessing lname from #{name}... "
    lname = BlackStack::Appending::cleanup_lname(name)
    l.logf lname

    BlackStack::Appending.find_verified_emails(fname, lname, cname, l)
end

.indexesObject



45
46
47
# File 'lib/appending.rb', line 45

def self.indexes
    @@indexes
end

.loggerObject



25
26
27
# File 'lib/appending.rb', line 25

def self.logger
    @@logger
end

.phone_fieldsObject



86
87
88
# File 'lib/appending.rb', line 86

def self.phone_fields
    @@phone_fields
end

.reportObject

@@report



50
51
52
# File 'lib/appending.rb', line 50

def self.report
    @@report
end

.set(h) ⇒ Object

set configuration



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/appending.rb', line 100

def self.set(h)
    errors = []

    # validation: if :indexes is present, it must be an array of objects BlackStack::CSVIndexer::Index
    if h[:indexes]
        if !h[:indexes].is_a?(Array)
            errors << "Invalid :indexes: #{h[:indexes].class}. Expected: Array."
        else
            h[:indexes].each { |index|
                if !index.is_a?(BlackStack::CSVIndexer::Index)
                    errors << "Invalid :indexes: #{index.class}. Expected: BlackStack::CSVIndexer::Index."
                end
            }
        end
    end

    # validation: if :verifier_url is present, it must be a string
    errors << ":verifier_url must be a string." if h[:verifier_url] && !h[:verifier_url].is_a?(String)

    # validation: if :verifier_api_key is present, it must be a string
    errors << ":verifier_api_key must be a string." if h[:verifier_api_key] && !h[:verifier_api_key].is_a?(String)

    # validation: if :email_fields is present, it must be an array of strings
    if h[:email_fields]
        if !h[:email_fields].is_a?(Array)
            errors << "Invalid :email_fields: #{h[:email_fields].class}. Expected: Array."
        else
            h[:email_fields].each { |field|
                if !field.is_a?(String)
                    errors << "Invalid :email_fields: #{field.class}. Expected: String."
                end
            }
        end
    end

    # validation: if :phone_fields is present, it must be an array of strings
    if h[:phone_fields]
        if !h[:phone_fields].is_a?(Array)
            errors << "Invalid :phone_fields: #{h[:phone_fields].class}. Expected: Array."
        else
            h[:phone_fields].each { |field|
                if !field.is_a?(String)
                    errors << "Invalid :phone_fields: #{field.class}. Expected: String."
                end
            }
        end
    end

    # validation: if :company_domain_fields is present, it must be an array of strings
    if h[:company_domain_fields]
        if !h[:company_domain_fields].is_a?(Array)
            errors << "Invalid :company_domain_fields: #{h[:company_domain_fields].class}. Expected: Array."
        else
            h[:company_domain_fields].each { |field|
                if !field.is_a?(String)
                    errors << "Invalid :company_domain_fields: #{field.class}. Expected: String."
                end
            }
        end
    end

    # mapping
    @@indexes = h[:indexes] if h[:indexes]
    @@verifier_url = h[:verifier_url] if h[:verifier_url]
    @@verifier_api_key = h[:verifier_api_key] if h[:verifier_api_key]
    @@email_fields = h[:email_fields] if h[:email_fields]
    @@phone_fields = h[:phone_fields] if h[:phone_fields]
    @@company_domain_fields = h[:company_domain_fields] if h[:company_domain_fields]

    # zerobounce api key
    @@zerobounce_api_key = h[:zerobounce_api_key] if h[:zerobounce_api_key]

    # emailverify api key
    @@emailverify_api_key = h[:emailverify_api_key] if h[:emailverify_api_key]
end

.set_company_fields(fields) ⇒ Object

@@company_domain_fields



91
92
93
# File 'lib/appending.rb', line 91

def self.set_company_fields(fields)
    @@company_domain_fields = fields
end

.set_email_fields(fields) ⇒ Object

@@email_fields



73
74
75
# File 'lib/appending.rb', line 73

def self.set_email_fields(fields)
    @@email_fields = fields
end

.set_indexes(indexes) ⇒ Object



41
42
43
# File 'lib/appending.rb', line 41

def self.set_indexes(indexes)
    @@indexes = indexes
end

.set_logger(logger) ⇒ Object

@@logger



21
22
23
# File 'lib/appending.rb', line 21

def self.set_logger(logger)
    @@logger = logger
end

.set_phone_fields(fields) ⇒ Object

@@phone_fields



82
83
84
# File 'lib/appending.rb', line 82

def self.set_phone_fields(fields)
    @@phone_fields = fields
end

.set_verifier_api_key(key) ⇒ Object

@@verifier_api_key



64
65
66
# File 'lib/appending.rb', line 64

def self.set_verifier_api_key(key)
    @@verifier_api_key = key
end

.set_verifier_url(url) ⇒ Object

@@verifier_url



55
56
57
# File 'lib/appending.rb', line 55

def self.set_verifier_url(url)
    @@verifier_url = url
end

.verifier_api_keyObject



68
69
70
# File 'lib/appending.rb', line 68

def self.verifier_api_key
    @@verifier_api_key
end

.verifier_urlObject



59
60
61
# File 'lib/appending.rb', line 59

def self.verifier_url
    @@verifier_url
end

.verify(email) ⇒ Object

verify an email address using the AWS IP address of our website, wich is more reliable

This is a support method for the append methods. The end-user should not call this method directly.



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/appending.rb', line 223

def self.verify(email)
    url = @@verifier_url
    params = {
        :email => email,
    }
    # send HTTP-GET request to @@verifier_url, using the standard HTTP module
    res = Net::HTTP.get_response(URI.parse("#{url}?email=#{params[:email]}"))
    parsed = JSON.parse(res.body)
    #parsed = JSON.parse(`curl --location --request GET '#{url}?email=#{email}' --header 'Content-Type: application/json'`)
    parsed['status'] == 'success'            
end

.zerobounce_verify(email) ⇒ Object

call zerobounce api



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/appending.rb', line 177

def self.zerobounce_verify(email)
    # validation: email must be a string
    raise "Invalid email: #{email.class}. Expected: String." if !email.is_a?(String)
    url = "https://api.zerobounce.net/v2/validate"
    params = {
        :api_key => @@zerobounce_api_key,
        :email => email,
        :ip_address => nil,
    }
    res = BlackStack::Netting::call_get(url, params)
    JSON.parse(res.body)            
end