Check out Cryptid Hunter CLI here.
What we’ve learned so far!
At the Flatiron school we spend 3 weeks working with a specific Mod(ule). A Mod represents the language/type of code we’ll be working with for those 3 weeks. In Mod 1 we learned the basics of Ruby. The first week we jumped into the very basics of Ruby, defining a method, creating an instance of an Object, the different types of data structures we could work with, etc. By week 2 we were building our own classes of Object.
For the layman reading this an- Object is a piece of data that is unique and takes up space in your computer’s memory.
We built relationships between those Objects, meaning we learned how to make sure an Object knows what other Object it can communicate with and how. Finally we learned the magic of Active Record. This took everything we learned how to do laboriously , and gave us the ability to do it quickly and efficiently.
In week 3 we were teamed up as pairs and given the task of creating a Ruby CLI, or Command Line Interface, that either presented the User with the ability to create and manipulate data, i.e. CRUD, or allowed the User to search through and analyze data. My partner Sam Patel and I decided to build a simple blog app that allowed users to create, edit, comment on, and search posts about…. Cryptids and Paranormal encounters. Users could also create a profile, and add to our growing encylopedia of Cryptids.
Read more about CRUD here: https://en.wikipedia.org/wiki/Create,_read,_update_and_delete
The Hunt Begins!
We knew that we had 5 categories: Users, Monsters, Posts, Comments, and Likes. We built out our relationships using ActiveRecord’s shortcuts- :has_many, :belongs_to, and :has_many_through.
A User- has many Posts;
has many Comments;
has many Likes;
has many Monsters through Posts;
A Monster- has many Posts;
A Post- belongs to a User;
belongs to a Monster;
has many Comments;
has many Likes;
A Comment- belongs to a User;
belongs to a Post;
A Like- belongs to a User;
belongs to a Post;
Now that we have our relationships it was time to start building out our User Interface.
Alright, this is where it get’s weird(er)
We had a LOT of different ways to get the same information. You could search for posts by user, or popularity, or monster, or most recent. You could sort Users by rank, or name. And you could even see what Monsters a particular User had written about. I won’t bore you with all the details here, but I want to talk a little about our User Login method.
def user_login
prompt = TTY::Prompt.new
pastel = Pastel.new
result = prompt.ask("What is your name?") do |q|
q.required true
q.validate /\A\w+\Z/
q.modify :capitalize
end
if User.exists?(['name LIKE ?', "%#{result}%"])
user = User.find_by_name(result)
enter_password(user)
puts "Welcome back, #{result}!"
return user
else
puts "Hmmm, I don't see that name..."
yes_or_no = prompt.yes?("Would you like to create an account?")
if yes_or_no == true
create_user(result)
end
end
end
The above code was our login page. Very simple, I wouldn’t want to use this on a banking website, but for our purposes it worked perfectly. First a user was prompted to put in their name.
result = prompt.ask("What is your name?") do |q|
q.required true <-- You MUST enter a name -->
q.validate /\A\w+\Z/ <-- It MUST be all alpha-numeric characters -->
q.modify :capitalize <-- The string passed into the program will be capitalized. i.e 'maddie' will become 'Maddie' -->
end
Then the program will check if that User’s name appears in our User database.
if User.exists?(['name LIKE ?', "%#{result}%"])<--search for the name-->
user = User.find_by_name(result) <--search for the User Instance with that name-->
enter_password(user) <-- run enter_password method -->
puts "Welcome back, #{result}!" <-- If password is accepted output a welcome message -->
return user <-- the User Instance is returned to the program to use inside the app session -->
And if the do exist it will ask for a password
def enter_password(user)
password = @prompt.mask("Password:")
if user.authenticate(password) == true
end
if user.authenticate(password) == false
puts "incorrect password"
user_login
end
If the user does NOT exist
else <-- if User does not exist -->
puts "Hmmm, I don't see that name..."
yes_or_no = prompt.yes?("Would you li.ke to create an account?") <-- Asks user to create account -->
if yes_or_no == true
create_user(result) <-- run create_user method -->
end
end
end
The app will let the User create an account.
def create_user(result)
new_user = User.create(name: result, rank: 0)
puts "Welcome to Cryptid Hunter " + @pastel.red("#{result}!")
create_password(new_user)
new_user.location = @prompt.ask("What city do you live in?")
new_user.bio = @prompt.ask("Tell us a little about yourself!")
new_user.save
new_user
end
If you’d like to see more of our code, please go to our github page and feel free to dive into our code a little more at Cryptid Hunter CLI!

Leave a comment