Class: Britebox::FormatRecognizer

Inherits:
Object
  • Object
show all
Defined in:
lib/britebox/format_recognizer.rb

Constant Summary collapse

EMAIL_PATTERN =
/(\S+)@(\S+)/
COL_SEPARATORS =
[";", "|", "\t"]
PLUS_HEADERS =
['email_status', 'disposable', 'role_account']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ FormatRecognizer

Returns a new instance of FormatRecognizer.



11
12
13
14
# File 'lib/britebox/format_recognizer.rb', line 11

def initialize(file_name)
  @file_name = file_name
  @opts = {col_separator: nil, header_row: nil, email_index: nil}
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/britebox/format_recognizer.rb', line 3

def error
  @error
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/britebox/format_recognizer.rb', line 3

def opts
  @opts
end

Class Method Details

.is_header_row?(row) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/britebox/format_recognizer.rb', line 49

def self.is_header_row?(row)
  row.each do |v|
    v = v.first if v.instance_of? Array
    return false if v.to_s.match(EMAIL_PATTERN)
  end
  !row.join(" ").downcase.match("email").nil?
end

Instance Method Details

#find_email_index(row) ⇒ Object



42
43
44
45
46
47
# File 'lib/britebox/format_recognizer.rb', line 42

def find_email_index(row)
  row.each_with_index do |value, index|
    return index if value.to_s.match(EMAIL_PATTERN)
  end
  nil
end

#recognize!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/britebox/format_recognizer.rb', line 16

def recognize!
  test_lines = []
  begin
    line_n = 0
    CSV.foreach(@file_name) do |line|
      if line && line.size > 0
        if line_n < 5
          test_lines << line
        else
          break
        end

        line_n += 1
      end
    end
  rescue Exception => ex
    @error = ex.message.to_s
    return false
  end

  autoconfigure(test_lines) || return

  true
end