Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover movies page #2

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem 'faraday'
gem 'figaro'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand All @@ -71,4 +73,5 @@ group :test do
gem "launchy"
gem "shoulda-matchers"
gem "simplecov"
gem 'webmock'
end
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ GEM
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
bindex (0.8.1)
bootsnap (1.17.0)
Expand All @@ -85,13 +86,23 @@ GEM
xpath (~> 3.2)
coderay (1.1.3)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
crass (1.0.6)
date (3.3.4)
diff-lcs (1.5.0)
docile (1.4.0)
erubi (1.12.0)
faraday (2.7.12)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
figaro (1.2.0)
thor (>= 0.14.0, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.0.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (1.2.3)
Expand Down Expand Up @@ -216,6 +227,7 @@ GEM
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
shoulda-matchers (5.3.0)
activesupport (>= 5.2.0)
simplecov (0.22.0)
Expand Down Expand Up @@ -247,6 +259,10 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webmock (3.19.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -262,6 +278,8 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
capybara
faraday
figaro
importmap-rails
jbuilder
launchy
Expand All @@ -278,6 +296,7 @@ DEPENDENCIES
turbo-rails
tzinfo-data
web-console
webmock

RUBY VERSION
ruby 3.2.2p53
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/discover_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DiscoverController < ApplicationController
def index

end
end
18 changes: 18 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class MoviesController < ApplicationController
def index
if params[:Movie_Title].present?
facade = MovieFacade.new
@results = facade.search_movies(params[:Movie_Title])
else
facade = MovieFacade.new
@results = facade.top_movies
end
end

def show
facade = MovieFacade.new
@data = facade.movie_details(params[:id])
@data2 = facade.movie_cast(params[:id])
@data3 = facade.movie_reviews(params[:id])
end
end
34 changes: 34 additions & 0 deletions app/facades/movie_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class MovieFacade
def top_movies
service = MovieService.new
data = service.top_movies[:results]
limit = data[0..20]
@results = limit.map do |movie_data|
MoviePoro.new(movie_data)
end
end

def search_movies(title)
service = MovieService.new
data = service.search_movies(title)[:results]
limit = data[0..20]
@results = limit.map do |movie_data|
MoviePoro.new(movie_data)
end
end

def movie_details(id)
service = MovieService.new
service.movie_details(id)
end

def movie_cast(id)
service = MovieService.new
service.movie_cast(id)
end

def movie_reviews(id)
service = MovieService.new
service.movie_reviews(id)
end
end
15 changes: 15 additions & 0 deletions app/poros/movie_poro.rb
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mod 1 making a comeback!

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class MoviePoro
attr_reader :id, :title, :vote_average, :runtime, :genres, :overview, :cast, :results, :poster_path

def initialize(data)
@id = data[:id]
@title = data[:title]
@vote_average = data[:vote_average]
@runtime = data[:runtime]
@genres = data[:genres]
@overview = data[:overview]
@cast = data[:cast]
@results = data[:results]
@poster_path = data[:poster_path]
end
end
32 changes: 32 additions & 0 deletions app/services/movie_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class MovieService
def get_url(url)
response = conn.get(url)
JSON.parse(response.body, symbolize_names: true)
end

def top_movies
get_url('/3/movie/popular')
end

def search_movies(title)
get_url("/3/search/movie?query=#{title}")
end

def movie_details(id)
get_url("/3/movie/#{id}")
end

def movie_cast(id)
get_url("/3/movie/#{id}/credits")
end

def movie_reviews(id)
get_url("/3/movie/#{id}/reviews")
end

def conn
Faraday.new(url: "https://api.themoviedb.org") do |faraday|
faraday.params['api_key'] = Rails.application.credentials.tmdb[:key]
end
end
end
9 changes: 9 additions & 0 deletions app/views/discover/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= button_to 'Discover Top Rated Movies', "/users/#{params[:user_id]}/movies", method: :get %>

<%= form_with url: "/users/#{params[:user_id]}/movies" , method: :get, local: true do |f| %>

<%= f.label :Movie_Title %>
<%= f.text_field :Movie_Title %>

<%= f.submit 'Search by Movie Title' %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Movie Results </h1>
<% @results.each do |result| %>
<ul class="movie_result">
<li><%= link_to "#{result.title}", "/users/#{params[:user_id]}/movies/#{result.id}" %></li>
<li><%= result.vote_average %></li>
</ul>
<% end %>
5 changes: 5 additions & 0 deletions config/routes.rb
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice advanced routing!

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

# Defines the root path route ("/")
# root "articles#index"

resources :users, only: [:new, :create, :show] do
resources :discover, only: [:index]
resources :movies, only: [:index, :show]
end
end
1 change: 1 addition & 0 deletions db/migrate/20231128001844_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def change
create_table :users do |t|
t.string :name
t.string :password_digest
t.string :email

t.timestamps
end
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions spec/features/users/discover/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe 'Discover Movies Page' do
before(:each) do
load_test_data
end

it 'When a user can get to movie results through movie title search' do

visit "/users/#{@user1.id}/discover"

expect(page).to have_button("Discover Top Rated Movies")
expect(page).to have_button("Search by Movie Title")

fill_in 'Movie_Title', with: "Batman"
# click_button "Search by Movie Title"

# expect(current_path).to eq("/users/#{@user1.id}/movies")
end

it 'When a user can get to movie results through top rated movies button' do

visit "/users/#{@user1.id}/discover"

expect(page).to have_button("Discover Top Rated Movies")
expect(page).to have_button("Search by Movie Title")

# click_button "Discover Top Rated Movies"

# expect(current_path).to eq("/users/#{@user1.id}/movies")
end
end
57 changes: 57 additions & 0 deletions spec/features/users/movies/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rails_helper'

RSpec.describe 'Movie Results' do
before(:each) do
load_test_data
end
describe 'happy path' do
it 'Get top 20 rated movies' do
json_response = File.read('spec/fixtures/top_twenty_movies.json')
stub_request(:get, "https://api.themoviedb.org/3/movie/popular?api_key=#{Rails.application.credentials.tmdb[:key]}").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Faraday v2.7.12'
}).
to_return(status: 200, body: json_response, headers: {})

visit "/users/#{@user1.id}/discover"

expect(page).to have_button("Discover Top Rated Movies")
expect(page).to have_button("Search by Movie Title")

click_button "Discover Top Rated Movies"

expect(page.status_code).to eq 200

expect(page).to have_link("Trolls Band Together")
expect(page).to have_content(7.215)
end

it 'Get searched movies' do
visit "/users/#{@user1.id}/discover"

expect(page).to have_button("Discover Top Rated Movies")
expect(page).to have_button("Search by Movie Title")

json_response = File.read('spec/fixtures/batman_movies.json')
stub_request(:get, "https://api.themoviedb.org/3/search/movie?api_key=#{Rails.application.credentials.tmdb[:key]}&query=Batman").
with(
headers: {
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Faraday v2.7.12'
}).
to_return(status: 200, body: json_response, headers: {})

fill_in 'Movie_Title', with: "Batman"
click_button "Search by Movie Title"

expect(page.status_code).to eq 200

expect(page).to have_link("Batman Begins")
expect(page).to have_content(7.2)
end
end
end
Empty file.
Loading