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

Class Method Summary collapse

Class Method Details

.add_index(index) ⇒ Object

@@indexes



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

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)


191
192
193
# File 'lib/appending.rb', line 191

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.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/appending.rb', line 230

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.



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

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.



222
223
224
225
226
# File 'lib/appending.rb', line 222

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

.company_domain_fieldsObject



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

def self.company_domain_fields
    @@company_domain_fields
end

.email_fieldsObject



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

def self.email_fields
    @@email_fields
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.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/appending.rb', line 276

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.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/appending.rb', line 311

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.



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/appending.rb', line 260

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



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
# File 'lib/appending.rb', line 354

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



340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/appending.rb', line 340

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



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

def self.indexes
    @@indexes
end

.loggerObject



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

def self.logger
    @@logger
end

.phone_fieldsObject



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

def self.phone_fields
    @@phone_fields
end

.reportObject

@@report



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

def self.report
    @@report
end

.set(h) ⇒ Object

set configuration



99
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
# File 'lib/appending.rb', line 99

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]
end

.set_company_fields(fields) ⇒ Object

@@company_domain_fields



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

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

.set_email_fields(fields) ⇒ Object

@@email_fields



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

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

.set_indexes(indexes) ⇒ Object



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

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

.set_logger(logger) ⇒ Object

@@logger



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

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

.set_phone_fields(fields) ⇒ Object

@@phone_fields



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

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

.set_verifier_api_key(key) ⇒ Object

@@verifier_api_key



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

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

.set_verifier_url(url) ⇒ Object

@@verifier_url



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

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

.verifier_api_keyObject



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

def self.verifier_api_key
    @@verifier_api_key
end

.verifier_urlObject



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

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.



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/appending.rb', line 200

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



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/appending.rb', line 173

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