Module: WolfCore::Barton::Parsing

Defined in:
lib/wolf_core/application/barton/parsing.rb

Instance Method Summary collapse

Instance Method Details

#parse_date(date_string) ⇒ Object



48
49
50
51
52
53
# File 'lib/wolf_core/application/barton/parsing.rb', line 48

def parse_date(date_string)
  return nil if date_string.nil?
  date_string = date_string.strip
  return nil if date_string.empty?
  Date.parse(date_string).strftime('%Y-%m-%d') rescue nil
end

#split_address(address_string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wolf_core/application/barton/parsing.rb', line 4

def split_address(address_string)
  address_string ||= ''
  address = { street: nil, city: nil, state: nil, zip: nil }
  city_and_state_regex_found = false
  
  city_and_state_regex = /\b([A-Za-z\s]+),\s*([A-Za-z]{2})\b/
  zip_regex = /\b\d{5}(?:-\d{4})?\b/
  street_regex = /\A([^,]+)/
  state_regex = /\b[A-Za-z]{2}\b/
  city_regex = /\b[a-zA-Z0-9\s]{2,}\b/
  
  if match_data = address_string.match(city_and_state_regex)
    address[:city] = match_data[1].strip
    address[:state] = match_data[2].strip
    address_string.sub!(city_and_state_regex, '')
    city_and_state_regex_found = true
  end
  
  if zip_match = address_string.match(zip_regex)
    address[:zip] = zip_match[0].strip
    address_string.sub!(zip_regex, '')
  end
  
  if street_match = address_string.match(street_regex)
    address[:street] = street_match[0].strip
    address[:street] = nil if address[:street].empty?
    address_string.sub!(street_regex, '')
  end
  
  return address if city_and_state_regex_found
  
  if state_match = address_string.match(state_regex)
    address[:state] = state_match[0].strip
    address_string.sub!(state_regex, '')
  end
  
  if city_match = address_string.match(city_regex)
    address[:city] = city_match[0].strip
    address_string.sub!(city_regex, '')
  end
  
  return address
end

#split_name(full_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wolf_core/application/barton/parsing.rb', line 55

def split_name(full_name)
  words = full_name.split(' ')
  
  if words.length == 2
    return { firstname: words[0], lastname: words[1] }
  end
  
  name = words[0..-2].join(' ')
  lastname = words[-1]
  return { firstname: name, lastname: lastname }
end