Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
require_relative "lib/movie"
require_relative "lib/api"

def find_movie
puts "OH HAI. Search?"
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
def find_movie(movies_hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sooo, this method breaks a couple of my rules for better code:

  1. it is longer than 4 lines long.
  2. it has a begin/rescue that continues on in the method
  3. it has more than 1 line inside of the begin/rescue

Alternatively, I recommend:

def find_movie(movies_hash)
  movies = fetch_movie
  ###... more code
end

def fetch_movie
  puts "Add a movie you really like"
  movie_title = gets.chop
  begin
    Api.search_by_title(movie_title)
  rescue MovieNotFoundError => e
    puts e
    puts "Error " #...
  end
end

You'd then made the Api raise a MovieNotFoundError if a movie wasn't found

puts "Add a movie you really like..."
movie_title = gets.chop

begin
movies = Api.search_by_title(movie_title)
hash = {movies.title => movies.score}
movies_hash = hash.merge(movies_hash)
rescue StandardError=>e
puts e
puts "Error: Movie '#{movie_title}' not found!"
else
puts "Found: #{movies.title}. Score: #{movies.score}"
count = score = 0
movies_hash.each {|key, value|
count = count + 1
score = score + value
}
end
average = (score/count).to_f
puts "Your Average Taste in Movies: #{average}"
movies_hash
end

find_movie
movies_hash = {}
movies_hash = find_movie(movies_hash)

while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
movies_hash = find_movie(movies_hash)
else
break
end
Expand Down