• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env ruby
2
3require './addressbook_pb'
4require 'pry'
5
6# Iterates though all people in the AddressBook and prints info about them.
7def 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
28end
29
30# Main procedure:  Reads the entire address book from a file and prints all
31#   the information inside.
32if ARGV.length != 1
33  puts "Usage: #{$PROGRAM_NAME} ADDRESS_BOOK_FILE"
34  exit(-1)
35end
36
37# Read the existing address book.
38f = File.open(ARGV[0], "rb")
39address_book = Tutorial::AddressBook.decode(f.read)
40f.close
41
42list_people(address_book)
43