Auto-generate files after cl/721951543
[google-protobuf.git] / examples / list_people.rb
blobfa633615aa17f972a6da0d681c4829d44e10a6d6
1 #! /usr/bin/env ruby
3 require './addressbook_pb'
4 require 'pry'
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
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
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
37 # Read the existing address book.
38 f = File.open(ARGV[0], "rb")
39 address_book = Tutorial::AddressBook.decode(f.read)
40 f.close
42 list_people(address_book)