class Dropmire::Parser

Public Class Methods

new(text, options = {}) click to toggle source

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.

# File lib/dropmire/parser.rb, line 17
def initialize(text, options = {})
  @text = text
  @attrs = {}

  Dropmire::Validator.new(@text)
end

Public Instance Methods

address(text) click to toggle source
# File lib/dropmire/parser.rb, line 49
def address(text)
  /\A[%][a-zA-Z\s]*/.match(text).to_s
end
attrs() click to toggle source
# File lib/dropmire/parser.rb, line 24
def attrs
  @attrs
end
capitalize_or_nil(name) click to toggle source

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

# File lib/dropmire/parser.rb, line 93
def capitalize_or_nil(name)
  unless name.nil?
    name.capitalize
  end
end
carrot_string(text) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/dropmire/parser.rb, line 144
def expiration_date(dob)
  @attrs[:expiration_date] = transform_date(dob[1,4]) + "-#{dob[11,2]}"
end
id_string() click to toggle source
# File lib/dropmire/parser.rb, line 119
def id_string
  /;[0-9]*=/.match(@text).to_s.gsub(/[;=]/, '')
end
iin(str) click to toggle source
# File lib/dropmire/parser.rb, line 123
def iin(str)
  @attrs[:iin] = str[0,6]
end
license_class() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
parse() click to toggle source
# File lib/dropmire/parser.rb, line 32
def parse
  parse_methods.each { |method| self.send method }
end
parse_address() click to toggle source
# 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_string() click to toggle source
# 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_dates() click to toggle source
# 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_num() click to toggle source
# File lib/dropmire/parser.rb, line 132
def parse_license_num
  id_str = id_string
  iin(id_str)
  license_num(id_str)
end
parse_methods() click to toggle source
# 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) click to toggle source
# File lib/dropmire/parser.rb, line 76
def split_name(name)
  name.split('$')
end
state(addr) click to toggle source
# File lib/dropmire/parser.rb, line 53
def state(addr)
  addr[1..2]
end
street(street) click to toggle source
# 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
text() click to toggle source
# File lib/dropmire/parser.rb, line 28
def text
  @text
end
transform_date(date) click to toggle source
# File lib/dropmire/parser.rb, line 156
def transform_date(date)
  y = date[0,2]
  m = date[2,2]
  "20#{y}-#{m}"
end
zipcode() click to toggle source
# 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