parsed_response['people'].each do|person| if person['active'] == true eligible_users << { name:"#{person['firstName']}#{person['lastName']}", email: person['emailAddress'] } end end
parser.people.each do|person| if person['active'] == true eligible_users << { name:"#{person['firstName']}#{person['lastName']}", email: person['emailAddress'] } end end
eligible_users
Value object for a person
我們需要一個 pure ruby object 來處理 person
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classPerson definitialize(attributes) @attributes = attributes end
defactive? @attributes['active'] end
defemail @attributes['emailAddress'] end
defname "#{@attributes['firstName']}#{@attributes['lastName']}" end end
有了上述的 Person Object 我們的 Parser 可以再來進行修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classPeopleParser definitialize(raw_response) @raw_response = raw_response end
defpeople @parsed_json['people'].map { |attributes| Person.new(attributes) } end
private
defparsed_json @parsed_json||= JSON.parse(@raw_response) end end