Class: Dropmire::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dropmire/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Parser

Public: Creates a new Dropmire::Parser.

text - The String text to parse. options - The Hash of initialization options for the Parser.

Examples

Dropmire::Parser.new file.read
# => { first_name => "John", ... }

Returns the Hash of results from the string.


17
18
19
20
21
22
# File 'lib/dropmire/parser.rb', line 17

def initialize(text, options = {})
  @text = text
  @attrs = {}

  Dropmire::Validator.new(@text)
end

Instance Method Details

#address(text) ⇒ Object


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

def address(text)
  /\A[%][a-zA-Z\s]*/.match(text).to_s
end

#attrsObject


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

def attrs
  @attrs
end

#capitalize_or_nil(name) ⇒ Object

Capitalizes and returns @name if not nil, returns nil otherwise.


93
94
95
96
97
# File 'lib/dropmire/parser.rb', line 93

def capitalize_or_nil(name)
  unless name.nil?
    name.capitalize
  end
end

#carrot_string(text) ⇒ Object


64
65
66
67
68
# File 'lib/dropmire/parser.rb', line 64

def carrot_string(text)
  str = /\^(.*)\^/.match(text).to_s
  len = str.length-2
  str[1..len].split('^')
end

#city(addr) ⇒ Object


57
58
59
60
61
62
# File 'lib/dropmire/parser.rb', line 57

def city(addr)
  l = addr.length-1
  city_arr = addr[3..l].split(' ')
  city_arr.each { |c| c.capitalize! }
  city_arr.join(' ')
end

#date_of_birth(str) ⇒ Object


148
149
150
151
152
153
154
# File 'lib/dropmire/parser.rb', line 148

def date_of_birth(str)
  dob = str[5,8]
  year = dob[0,4]
  month = str[3,2]
  day = dob[6,2]
  @attrs[:date_of_birth] = "#{year}-#{month}-#{day}"
end

#expiration_date(dob) ⇒ Object


144
145
146
# File 'lib/dropmire/parser.rb', line 144

def expiration_date(dob)
  @attrs[:expiration_date] = transform_date(dob[1,4]) + "-#{dob[11,2]}"
end

#id_stringObject


119
120
121
# File 'lib/dropmire/parser.rb', line 119

def id_string
  /;[0-9]*=/.match(@text).to_s.gsub(/[;=]/, '')
end

#iin(str) ⇒ Object


123
124
125
# File 'lib/dropmire/parser.rb', line 123

def iin(str)
  @attrs[:iin] = str[0,6]
end

#license_classObject


114
115
116
117
# File 'lib/dropmire/parser.rb', line 114

def license_class
  str = /![\s]*[0-9]*[\s]*[A-Z]/.match(@text).to_s
  @attrs[:license_class] = str[-1]
end

#license_num(str) ⇒ Object


127
128
129
130
# File 'lib/dropmire/parser.rb', line 127

def license_num(str)
  num_len = str.length - 8
  @attrs[:license_num] = str[6,2].to_char.upcase + str[8, num_len]
end

#names(names) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dropmire/parser.rb', line 80

def names(names)
  @attrs[:first_name]  = names[1].capitalize
  @attrs[:last_name]   = names[0].capitalize
  @attrs[:middle_name] = capitalize_or_nil(names[2])

  # Temporary patch here, otherwise the Dropmire::Identity#method_missing
  # won't recognize the middle_name method.
  @attrs[:middle_name] = "" if @attrs[:middle_name].nil?

  [@attrs[:first_name], @attrs[:middle_name], @attrs[:last_name]]
end

#parseObject


32
33
34
# File 'lib/dropmire/parser.rb', line 32

def parse
  parse_methods.each { |method| self.send method }
end

#parse_addressObject


41
42
43
44
45
46
47
# File 'lib/dropmire/parser.rb', line 41

def parse_address
  addr = address(@text)
  @attrs[:state] = state(addr)
  @attrs[:city] = city(addr)

  [@attrs[:city], @attrs[:state]]
end

#parse_carrot_stringObject


70
71
72
73
74
# File 'lib/dropmire/parser.rb', line 70

def parse_carrot_string
  name_string, street_string = carrot_string(@text)
  names split_name(name_string)
  street street_string
end

#parse_datesObject


138
139
140
141
142
# File 'lib/dropmire/parser.rb', line 138

def parse_dates
  str = /=[0-9]*/.match(@text).to_s
  date_of_birth(str)
  expiration_date(str)
end

#parse_license_numObject


132
133
134
135
136
# File 'lib/dropmire/parser.rb', line 132

def parse_license_num
  id_str = id_string
  iin(id_str)
  license_num(id_str)
end

#parse_methodsObject


36
37
38
39
# File 'lib/dropmire/parser.rb', line 36

def parse_methods
  [ :parse_address, :parse_carrot_string, :parse_dates, 
    :zipcode, :license_class, :parse_license_num ]
end

#split_name(name) ⇒ Object


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

def split_name(name)
  name.split('$')
end

#state(addr) ⇒ Object


53
54
55
# File 'lib/dropmire/parser.rb', line 53

def state(addr)
  addr[1..2]
end

#street(street) ⇒ Object


99
100
101
102
103
104
105
106
# File 'lib/dropmire/parser.rb', line 99

def street(street)
  ary = street.split(' ')
  str = []
  ary.each do |s|
    str << s.capitalize
  end
  @attrs[:street] = str.join(' ')
end

#textObject


28
29
30
# File 'lib/dropmire/parser.rb', line 28

def text
  @text
end

#transform_date(date) ⇒ Object


156
157
158
159
160
# File 'lib/dropmire/parser.rb', line 156

def transform_date(date)
  y = date[0,2]
  m = date[2,2]
  "20#{y}-#{m}"
end

#zipcodeObject


108
109
110
111
112
# File 'lib/dropmire/parser.rb', line 108

def zipcode
  str = /![\s]*[0-9]*/.match(@text).to_s
  zip = str[1..(str.length)].strip
  @attrs[:zipcode] = zip[0,5]
end