Class: Ordrin::Normalize::Normalizers

Inherits:
Object
  • Object
show all
Defined in:
lib/ordrin/normalize.rb

Instance Method Summary collapse

Instance Method Details

#alphanum(value) ⇒ Object



174
175
176
# File 'lib/ordrin/normalize.rb', line 174

def alphanum(value)
  regex(/^[a-zA-Z\d]+$/, Errors.alphanum)[value]
end

#credit_card(values) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ordrin/normalize.rb', line 119

def credit_card(values)
  number, cvc = values
  num = number.to_s.gsub(/\D/, '')
  unless luhn_valid?(num)
    raise Errors.credit_card[number]
  end
  card_type = cc_type(num)
  if card_type.nil?
    raise Errors.credit_card[number]
  else
    if card_type=='American Express'
      cvc_length = 4
    else
      cvc_length = 3
    end
    if cvc.length == cvc_length and !cvc.to_s[/^\d+$/].nil?
      [num, cvc, card_type]
    else
      raise Errors.cvc[cvc]
    end
  end
end

#date(date) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ordrin/normalize.rb', line 51

def date(date)
  if date.to_s.upcase == 'ASAP'
    'ASAP'
  else
    begin
      date.strftime('%m-%d')
    rescue NoMethodError
      raise Errors.date[date_time]
    end
  end
end

#datetime(date_time) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ordrin/normalize.rb', line 39

def datetime(date_time)
  if date_time.to_s.upcase == 'ASAP'
    'ASAP'
  else
    begin
      date_time.strftime('%m-%d+%H:%M')
    rescue NoMethodError
      raise Errors.date_time[date_time]
    end
  end
end

#email(email) ⇒ Object



166
167
168
# File 'lib/ordrin/normalize.rb', line 166

def email(email)
  regex(/^[^@\s]+@[^@\s]+\.[a-zA-Z]{2,3}/, Errors.email)[email]
end

#money(money) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ordrin/normalize.rb', line 30

def money(money)
  value = money.to_s.gsub(/,/, '')[/^\$?(\d+(\.\d+)?)$/, 1]
  if value.nil?
    raise Errors.money[money]
  else
    value
  end
end

#month(mon) ⇒ Object



162
163
164
# File 'lib/ordrin/normalize.rb', line 162

def month(mon)
  regex(/^\d{2}$/, Errors.month)[mon]
end

#name(value) ⇒ Object



146
147
148
# File 'lib/ordrin/normalize.rb', line 146

def name(value)
  unchecked(value)
end

#nick(nick) ⇒ Object



170
171
172
# File 'lib/ordrin/normalize.rb', line 170

def nick(nick)
  regex(/^[-\w]+/, Errors.nick)[nick]
end

#number(num) ⇒ Object



154
155
156
# File 'lib/ordrin/normalize.rb', line 154

def number(num)
  regex(/^\d+/, Errors.number)[num]
end

#phone(phone_number) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/ordrin/normalize.rb', line 19

def phone(phone_number)
  #strips out everything but digits from the phone number
  phone = phone_number.to_s.gsub(/\D/, '')
  if phone.length == 10
    phone[/^(\d{3})(\d{3})(\d{4})$/]
    "#{$1}-#{$2}-#{$3}"
  else
    raise Errors.phone[phone_number]
  end
end

#state(state) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/ordrin/normalize.rb', line 79

def state(state)
  state = state.to_s[/^[A-Za-z]{2}$/]
  if state.nil?
    raise Errors.state[state]
  else
    state.upcase
  end
end

#time(time) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ordrin/normalize.rb', line 63

def time(time)
  if time.to_s.upcase == 'ASAP'
    'ASAP'
  else
    begin
      time.strftime('%H:%M')
    rescue NoMethodError
      raise Errors.time[date_time]
    end
  end
end

#unchecked(value) ⇒ Object



142
143
144
# File 'lib/ordrin/normalize.rb', line 142

def unchecked(value)
  value.to_s
end

#url(url) ⇒ Object



75
76
77
# File 'lib/ordrin/normalize.rb', line 75

def url(url)
  url.to_s[/^(https?:\/\/)[-\w.~]+(:\d+)?(\/[-\w.~]+)*/]
end

#year(year) ⇒ Object



158
159
160
# File 'lib/ordrin/normalize.rb', line 158

def year(year)
  regex(/^\d{4}$/, Errors.year)[year]
end

#zip(zip) ⇒ Object



150
151
152
# File 'lib/ordrin/normalize.rb', line 150

def zip(zip)
  regex(/^\d{5}$/, Errors.zip)[zip]
end