kathytbui / viewing_party

Starter repo for a backend module 3 project.
0 stars 1 forks source link

Adds feature: Top rated movies appear as search results #37

Closed ajtran303 closed 4 years ago

ajtran303 commented 4 years ago

New Feature: Top Rated Movies

When the logged-in user clicks the 'Find Top Rated Movies' button, they will then see the top 40 movies. Each movie has:

Private Methods

The methods in here all adhere to SRP and can be encapsulated into a different class or classes eventually. For now, I am hiding them away to keep our controller action at a high level of abstraction:

def index
  @movies = top_rated_movies if params[:q] == 'top rated'
end

Exception Handling

I used exception handling to solve the issues I documented regarding the TMDB server response format. It is implemented with rescue statements and guard clauses and a sad view path.

The first rescue handles the edge case where the server is responding with HTML instead of JSON:

def valid_json?(string)
  JSON.parse(string)
rescue JSON::ParserError
  false
end

The second rescue will then assign nil to @movies in the index action.

def top_rated_movies
  ...
rescue NoMethodError
  nil
end

That allows for the sad view path (necessary to pass tests and CI checks):

  <% if @movies.nil? %>
    <% 40.times do %>
      <%= tag.article class: "movie" %>
    <% end %>
  <% else %>
...
ajtran303 commented 4 years ago

Screenshot

Screen Shot 2020-08-20 at 10 32 13 PM