Skip to content

Instantly share code, notes, and snippets.

@kiefer136
Last active April 7, 2016 16:15
Show Gist options
  • Save kiefer136/3230c6af8954c7c73e4cee766d70f2f5 to your computer and use it in GitHub Desktop.
Save kiefer136/3230c6af8954c7c73e4cee766d70f2f5 to your computer and use it in GitHub Desktop.
contact_list
1 Kiefer Johnson [email protected]
2 ben byer [email protected]
3 ryan goodbrew [email protected]
4 Steve Henderson [email protected]
5 Don Burks [email protected]
6 Khurram Virani [email protected]
7 Dominos Pizza [email protected]
require 'csv'
# Represents a person in an address book.
# The ContactList class will work with Contact objects instead of interacting with the CSV file directly
class Contact
CONTACT_LIST = ('./contact.csv')
attr_accessor :id, :name, :email
# Creates a new contact object
# @param name [String] The contact's name
# @param email [String] The contact's email address
def initialize(id, name, email)
# TODO: Assign parameter values to instance variables.
@id = id
@name = name
@email = email
end
# Provides functionality for managing contacts in the csv file.
class << self
# Opens 'contacts.csv' and creates a Contact object for each line in the file (aka each contact).
# @return [Array<Contact>] Array of Contact objects
def all
# TODO: Return an Array of Contact instances made from the data in 'contacts.csv'.
total_ids = 0
CSV.foreach(CONTACT_LIST) do |row|
id = row[0]
name = row[1]
email = row[2]
total_ids += 1
puts "#{id}: #{name} (#{email})"
end
puts "---"
puts "#{total_ids} records total"
end
# Creates a new contact, adding it to the csv file, returning the new contact.
# @param name [String] the new contact's name
# @param email [String] the contact's email
def create(name, email)
# TODO: Instantiate a Contact, add its data to the 'contacts.csv' file, and return it
File.open(CONTACT_LIST, 'a+') do |list|
id = list.readlines.size
list << "#{(id + 1).to_s},#{name},#{email}\n"
puts "Contact succesfully created"
end
end
# Find the Contact in the 'contacts.csv' file with the matching id.
# @param id [Integer] the contact id
# @return [Contact, nil] the contact with the specified id. If no contact has the id, returns nil.
def find(id_in)
# TODO: Find the Contact in the 'contacts.csv' file with the matching id.W
message = "not found"
CSV.foreach(CONTACT_LIST) do |row|
id = row[0]
name = row[1]
email = row[2]
if id_in == id
message = "#{id}: #{name} (#{email})"
end
end
puts message
end
# Search for contacts by either name or email.
# @param term [String] the name fragment or email fragment to search for
# @return [Array<Contact>] Array of Contact objects.
def search(term)
# TODO: Select the Contact instances from the 'contacts.csv' file whose name or email attributes contain the search term.
total_ids = 0
CSV.foreach(CONTACT_LIST) do |row|
id = row[0]
name = row[1].downcase
email = row[2].downcase
if /#{term}/ix.match(name) || /#{term}/ix.match(email)
puts "#{id}: #{name} (#{email})"
total_ids += 1
end
end
puts "---"
puts "#{total_ids} records total"
end
end
end
require_relative './contact'
# Interfaces between a user and their contact list. Reads from and writes to standard I/O.
# class ContactList
def search_name_email(term)
Contact.search(term)
end
def find_by_id(id)
Contact.find(id)
end
def show_all_contact
Contact.all
end
def create_contact(name, email)
Contact.create(name, email)
end
if ARGV == []
puts "Here is a list of available commands"
puts "new - Create a new contact"
puts "list - List all contacts"
puts "show - Search contacts"
elsif ARGV[0] == "list"
if ARGV[1]
puts "show single contact"
else
p show_all_contact
end
elsif ARGV[0] == "new"
print "Enter first name and last name of the contact you would like to enter:"
name = STDIN.gets.chomp
print "Enter email address of the contact you would like to enter:"
email = STDIN.gets.chomp
create_contact(name, email)
elsif ARGV[0] == "show"
puts "Enter the ID of the person you would like to seach for:"
id = STDIN.gets.chomp
find_by_id(id)
elsif ARGV[0] == "search"
term = ARGV[1]
search_name_email(term)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment