Guest User

Untitled

a guest
Jul 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. require 'rspec'
  2.  
  3. class Person
  4. require 'date'
  5.  
  6. # NOTES: Because I couldn't find abbreviation list of US cities, I used states list instead
  7. STATES = {
  8. AK: "Alaska",
  9. AL: "Alabama",
  10. AR: "Arkansas",
  11. AS: "American Samoa",
  12. AZ: "Arizona",
  13. CA: "California",
  14. CO: "Colorado",
  15. CT: "Connecticut",
  16. DC: "District of Columbia",
  17. DE: "Delaware",
  18. FL: "Florida",
  19. GA: "Georgia",
  20. GU: "Guam",
  21. HI: "Hawaii",
  22. IA: "Iowa",
  23. ID: "Idaho",
  24. IL: "Illinois",
  25. IN: "Indiana",
  26. KS: "Kansas",
  27. KY: "Kentucky",
  28. LA: "Louisiana",
  29. MA: "Massachusetts",
  30. MD: "Maryland",
  31. ME: "Maine",
  32. MI: "Michigan",
  33. MN: "Minnesota",
  34. MO: "Missouri",
  35. MS: "Mississippi",
  36. MT: "Montana",
  37. NC: "North Carolina",
  38. ND: "North Dakota",
  39. NE: "Nebraska",
  40. NH: "New Hampshire",
  41. NJ: "New Jersey",
  42. NM: "New Mexico",
  43. NV: "Nevada",
  44. NY: "New York",
  45. OH: "Ohio",
  46. OK: "Oklahoma",
  47. OR: "Oregon",
  48. PA: "Pennsylvania",
  49. PR: "Puerto Rico",
  50. RI: "Rhode Island",
  51. SC: "South Carolina",
  52. SD: "South Dakota",
  53. TN: "Tennessee",
  54. TX: "Texas",
  55. UT: "Utah",
  56. VA: "Virginia",
  57. VI: "Virgin Islands",
  58. VT: "Vermont",
  59. WA: "Washington",
  60. WI: "Wisconsin",
  61. WV: "West Virginia",
  62. WY: "Wyoming"
  63. }
  64.  
  65. attr_reader :first_name, :state_name, :birthday
  66.  
  67. def initialize(first_name, state_name, birthday)
  68. if first_name.nil? || first_name.empty?
  69. @first_name = ''
  70. else
  71. @first_name = first_name.strip
  72. end
  73.  
  74. if state_name.nil? || state_name.empty?
  75. @state_name = ''
  76. else
  77. @state_name = state_name.strip
  78. end
  79.  
  80. if birthday.nil? || birthday.empty?
  81. @birthday = ''
  82. else
  83. @birthday = birthday.strip
  84. end
  85. end
  86.  
  87. def valid?
  88. @state_name = find_state
  89. @birthday = format_birthday
  90.  
  91. return !first_name.empty? && state_name && birthday
  92. end
  93.  
  94. def format
  95. "#{first_name} #{state_name} #{birthday.strftime('%-m/%-d/%Y')}"
  96. end
  97.  
  98. def find_state
  99. return if state_name.empty?
  100.  
  101. if STATES.assoc(state_name.to_sym) # abbreviation
  102. STATES[state_name.to_sym]
  103. elsif STATES.key(state_name.to_s) # full state name
  104. state_name
  105. else
  106. nil
  107. end
  108. end
  109.  
  110. def format_birthday
  111. return if birthday.empty?
  112.  
  113. birthday.gsub!(/[-.\/]/, '/')
  114. begin
  115. Date.strptime(birthday, '%m/%d/%Y')
  116. rescue Exception => e
  117. return
  118. end
  119. end
  120. end
  121.  
  122. class PeopleController
  123. def self.normalize(request_params)
  124. people = []
  125.  
  126. return people if request_params.empty?
  127.  
  128. request_params.each do |key, values|
  129. unless values.empty?
  130. values.each do |val|
  131. if key.to_s == 'comma'
  132. person_info = val.split(',')
  133. person = Person.new person_info[0], person_info[1], person_info[2]
  134. elsif key.to_s == 'dollar'
  135. person_info = val.split('$')
  136. person = Person.new person_info[3], person_info[0], person_info[1]
  137. end
  138.  
  139. people.push person.format if person.valid?
  140. end
  141. end
  142. end
  143.  
  144. people
  145. end
  146. end
  147.  
  148. RSpec.describe PeopleController do
  149. describe "#normalize" do
  150. context 'when person first_name is missing' do
  151. it 'should not return a person' do
  152. test_case = {
  153. comma: [' , Alabama, 5/29/1986']
  154. }
  155.  
  156. expect(PeopleController.normalize(test_case)).to be_empty
  157. end
  158. end
  159.  
  160. context 'when person birthday is missing or invalid' do
  161. it 'should not return a person' do
  162. test_case = {
  163. dollar: ['NY $$Mckayla ']
  164. }
  165. expect(PeopleController.normalize(test_case)).to be_empty
  166.  
  167. test_case = {
  168. comma: ['Mckayla,Alabama, 5/209/1986']
  169. }
  170. expect(PeopleController.normalize(test_case)).to be_empty
  171. end
  172. end
  173.  
  174. context 'when person state is missing or invalid' do
  175. it 'should not return a person' do
  176. test_case = {
  177. dollar: ['$ 29-5-1986 $Mckayla']
  178. }
  179. expect(PeopleController.normalize(test_case)).to be_empty
  180.  
  181. test_case = {
  182. comma: ['Mckayla ,Hanoi,5/29/1986']
  183. }
  184. expect(PeopleController.normalize(test_case)).to be_empty
  185. end
  186. end
  187.  
  188. context 'when all person information correct' do
  189. it 'should return correct people information' do
  190. test_case = {
  191. comma: [ # Fields: first name, city name, birth date
  192. 'Mckayla, Alabama, 5/29/1986',
  193. 'Elliot, New York, 4/3/1947',
  194. ],
  195. dollar: [ # Fields: city abbreviation, birth date, last name, first name
  196. 'LA $ 10-4-1974 $ Nolan $ Rhiannon',
  197. 'NY $ 12-1-1962 $ Bruen $ Rigoberto',
  198. ],
  199. }
  200.  
  201. expect(PeopleController.normalize(test_case)).to eq([
  202. 'Mckayla Alabama 5/29/1986',
  203. 'Elliot New York 4/3/1947',
  204. 'Rhiannon Louisiana 10/4/1974',
  205. 'Rigoberto New York 12/1/1962',
  206. ])
  207. end
  208. end
  209. end
  210. end
Add Comment
Please, Sign In to add comment