DEV Community

James Shipman
James Shipman

Posted on • Edited on

Mod 1 Project

Summary

Mod 1 here at //flatiron school is nearing completion and that means a project; and blogs.
Here are the requirements for the project

  1. Use ActiveRecord
  2. Make use of at least 2 has_many models and 1 belongs_to model
  3. Pull in at least 1 API into our app
  4. App is to run in terminal, nothing too fancy ... yet ;)

Details

The group I am a part of choose to make a recipe builder. The premise is a protein and a vegetable are selected and the app pulls in data from an API to search recipes with those two ingredients. We then can choose that recipe or not. There's functionality to update the protein and vegetable lists as well as save recipes for later.

User stories

The app had to have at least 1 CRUD methods each in the form of user stories.

Create
"As a user, I want to choose a protein and a vegetable so that I can be given a choice of recipes."

Read
"As a user, I want to be able to select from a list of saved recipes."

Update
"As a user, I want to be able to update my protein, vegetable, and recipe list."

Delete
"As a user, I want to be able to remove proteins, vegetables, and recipes from my lists."

Regex "fun"

I took on the task of taking the text dump from the API for recipe instructions and making it display it in a reasonable and repeatable manner. After testing I noticed the text was showing up in only 3 different patterns; this was great as it simplified what I was going to do with regex. Now I am not a regex expert (noob, I'm a noob). Here is what I came up with.

text = recipe_hash["recipes"][0]["instructions"]
i = 1

# each group will make an ordered list
# from the lump of text provided by the API

# newline used lump return
if text.scan(/$/)
  while i < text.scan(/$/).count do
    puts "#{i}. #{text.split(/$/)[i][1..-1]}"
    i += 1
  end 
end 

# ordered list, not formatted lump
if text.scan(/\d\./)
  while i < text.scan(/\d\./).count do
    puts "#{i}. #{text.split(/\d\./)[i]}"
    i += 1
  end
end 

# remove html tags
# replace line items with ordered list number
if text.scan(/<ol>/)
  text_array = text.gsub(/<\/li>|<ol>|<\/ol>|<\/html>|<\/body>/,'').split(/<li>/)
  while i < text_array.length do
    puts "#{i}. #{text_array[i]}"
    i += 1
  end 
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)