Maciej Litwiniuk | ff5007f | 2023-03-07 09:32:18 -0800 | [diff] [blame] | 1 | #! /usr/bin/env ruby |
| 2 | |
| 3 | require './addressbook_pb' |
| 4 | require 'pry' |
| 5 | |
| 6 | # Iterates though all people in the AddressBook and prints info about them. |
| 7 | def list_people(address_book) |
| 8 | address_book.people.each do |person| |
| 9 | puts "Person ID: #{person.id}" |
| 10 | puts " Name: #{person.name}" |
| 11 | if person.email != "" |
| 12 | puts " Email: #{person.email}" |
| 13 | end |
| 14 | |
| 15 | person.phones.each do |phone_number| |
| 16 | type = |
| 17 | case phone_number.type |
| 18 | when :MOBILE |
| 19 | "Mobile phone" |
| 20 | when :HOME |
| 21 | "Home phone" |
| 22 | when :WORK |
| 23 | "Work phone" |
| 24 | end |
| 25 | puts " #{type} #: #{phone_number.number}" |
| 26 | end |
| 27 | end |
| 28 | end |
| 29 | |
| 30 | # Main procedure: Reads the entire address book from a file and prints all |
| 31 | # the information inside. |
| 32 | if ARGV.length != 1 |
| 33 | puts "Usage: #{$PROGRAM_NAME} ADDRESS_BOOK_FILE" |
| 34 | exit(-1) |
| 35 | end |
| 36 | |
| 37 | # Read the existing address book. |
| 38 | f = File.open(ARGV[0], "rb") |
| 39 | address_book = Tutorial::AddressBook.decode(f.read) |
| 40 | f.close |
| 41 | |
| 42 | list_people(address_book) |