From acdaaa5498e3b232d7c2253d866ba1b4b80ed03f Mon Sep 17 00:00:00 2001 From: kee nam Date: Mon, 18 Dec 2017 10:44:25 -0800 Subject: [PATCH 01/10] Initialized Rails project --- Gemfile.lock | 219 ++++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 107 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..438b97de 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,104 +1,107 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.0.1) - actionpack (= 5.0.1) - nio4r (~> 1.2) + actioncable (5.0.6) + actionpack (= 5.0.6) + nio4r (>= 1.2, < 3.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) + actionmailer (5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.1) - actionview (= 5.0.1) - activesupport (= 5.0.1) + actionpack (5.0.6) + actionview (= 5.0.6) + activesupport (= 5.0.6) rack (~> 2.0) rack-test (~> 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.1) - activesupport (= 5.0.1) + actionview (5.0.6) + activesupport (= 5.0.6) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - active_model_serializers (0.10.6) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.7) actionpack (>= 4.1, < 6) activemodel (>= 4.1, < 6) case_transform (>= 0.2) - jsonapi-renderer (>= 0.1.1.beta1, < 0.2) - activejob (5.0.1) - activesupport (= 5.0.1) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) + activejob (5.0.6) + activesupport (= 5.0.6) globalid (>= 0.3.6) - activemodel (5.0.1) - activesupport (= 5.0.1) - activerecord (5.0.1) - activemodel (= 5.0.1) - activesupport (= 5.0.1) + activemodel (5.0.6) + activesupport (= 5.0.6) + activerecord (5.0.6) + activemodel (= 5.0.6) + activesupport (= 5.0.6) arel (~> 7.0) - activesupport (5.0.1) + activesupport (5.0.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) ansi (1.5.0) arel (7.1.4) - better_errors (2.1.1) + better_errors (2.4.0) coderay (>= 1.0.0) - erubis (>= 2.6.6) + erubi (>= 1.0.0) rack (>= 0.9.0) - binding_of_caller (0.7.2) + bindex (0.5.0) + binding_of_caller (0.7.3) debug_inspector (>= 0.0.1) - builder (3.2.2) - byebug (9.0.6) + builder (3.2.3) + byebug (9.1.0) case_transform (0.2) activesupport - coderay (1.1.1) - coffee-rails (4.2.1) + coderay (1.1.2) + coffee-rails (4.2.2) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.2.x) + railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.0.4) - debug_inspector (0.0.2) - dotenv (2.2.0) - dotenv-rails (2.2.0) - dotenv (= 2.2.0) - railties (>= 3.2, < 5.1) + concurrent-ruby (1.0.5) + crass (1.0.3) + debug_inspector (0.0.3) + dotenv (2.2.1) + dotenv-rails (2.2.1) + dotenv (= 2.2.1) + railties (>= 3.2, < 5.2) + erubi (1.7.0) erubis (2.7.0) execjs (2.7.0) - ffi (1.9.14) - globalid (0.3.7) - activesupport (>= 4.1.0) - httparty (0.15.5) + ffi (1.9.18) + globalid (0.4.1) + activesupport (>= 4.2.0) + httparty (0.15.6) multi_xml (>= 0.5.2) - i18n (0.7.0) - jbuilder (2.6.1) - activesupport (>= 3.0.0, < 5.1) - multi_json (~> 1.2) - jquery-rails (4.2.2) + i18n (0.9.1) + concurrent-ruby (~> 1.0) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + jquery-rails (4.3.1) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - jsonapi-renderer (0.1.2) + jsonapi-renderer (0.2.0) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - loofah (2.0.3) + loofah (2.1.1) + crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.6.4) - mime-types (>= 1.16, < 4) - method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.1.0) - minitest (5.10.1) - minitest-reporters (1.1.12) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.10.3) + minitest-reporters (1.1.19) ansi builder minitest (>= 5.0) @@ -106,58 +109,60 @@ GEM minitest-spec-rails (5.4.0) minitest (~> 5.0) rails (>= 4.1) - multi_json (1.12.1) + multi_json (1.12.2) multi_xml (0.6.0) - nio4r (1.2.1) - nokogiri (1.7.0.1) - mini_portile2 (~> 2.1.0) - pry (0.10.4) + nio4r (2.1.0) + nokogiri (1.8.1) + mini_portile2 (~> 2.3.0) + pry (0.11.3) coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - pry-rails (0.3.4) - pry (>= 0.9.10) - puma (3.6.2) - rack (2.0.1) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + puma (3.11.0) + rack (2.0.3) rack-test (0.6.3) rack (>= 1.0) - rails (5.0.1) - actioncable (= 5.0.1) - actionmailer (= 5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) - activemodel (= 5.0.1) - activerecord (= 5.0.1) - activesupport (= 5.0.1) - bundler (>= 1.3.0, < 2.0) - railties (= 5.0.1) + rails (5.0.6) + actioncable (= 5.0.6) + actionmailer (= 5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) + activemodel (= 5.0.6) + activerecord (= 5.0.6) + activesupport (= 5.0.6) + bundler (>= 1.3.0) + railties (= 5.0.6) sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.2) - activesupport (>= 4.2.0, < 6.0) - nokogiri (~> 1.6) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - railties (5.0.1) - actionpack (= 5.0.1) - activesupport (= 5.0.1) + railties (5.0.6) + actionpack (= 5.0.6) + activesupport (= 5.0.6) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.0.0) - rb-fsevent (0.9.8) - rb-inotify (0.9.7) - ffi (>= 0.5.0) - ruby-progressbar (1.8.1) - sass (3.4.23) - sass-rails (5.0.6) + rake (12.3.0) + rb-fsevent (0.10.2) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.9.0) + sass (3.5.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) - slop (3.6.0) - spring (2.0.0) + spring (2.0.2) activesupport (>= 4.2) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -165,30 +170,30 @@ GEM sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.13) - thor (0.19.4) - thread_safe (0.3.5) - tilt (2.0.5) + thor (0.20.0) + thread_safe (0.3.6) + tilt (2.0.8) turbolinks (5.0.1) turbolinks-source (~> 5) - turbolinks-source (5.0.0) - tzinfo (1.2.2) + turbolinks-source (5.0.3) + tzinfo (1.2.4) thread_safe (~> 0.1) - uglifier (3.0.4) + uglifier (4.0.2) execjs (>= 0.3.0, < 3) - web-console (3.4.0) + web-console (3.5.1) actionview (>= 5.0) activemodel (>= 5.0) - debug_inspector + bindex (>= 0.4.0) railties (>= 5.0) - websocket-driver (0.6.4) + websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) - will_paginate (3.1.5) + websocket-extensions (0.1.3) + will_paginate (3.1.6) PLATFORMS ruby @@ -220,4 +225,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.14.6 + 1.16.0 From 6bff81baa1160af93dcddd995e18b537dd213fa5 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Mon, 18 Dec 2017 12:05:13 -0800 Subject: [PATCH 02/10] Change localhost to 8080 --- config/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 2ac21a62..c3e76843 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,7 +16,7 @@ class Application < Rails::Application config.eager_load_paths << Rails.root.join('lib') config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => 'http://localhost:8081', + 'Access-Control-Allow-Origin' => 'http://localhost:8080', 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") } end From 6d79df55100f1dc7eb78e85b42b158cfbd87a03e Mon Sep 17 00:00:00 2001 From: kee nam Date: Mon, 18 Dec 2017 15:42:18 -0800 Subject: [PATCH 03/10] Added search method and route --- app/controllers/movies_controller.rb | 7 +++++++ config/routes.rb | 1 + 2 files changed, 8 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..72bd0214 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,3 +1,5 @@ +require_dependency '../../lib/movie_wrapper' + class MoviesController < ApplicationController before_action :require_movie, only: [:show] @@ -21,6 +23,11 @@ def show ) end + def search() + results = MovieWrapper.search(params[:query]) + render status: :ok, json: results + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..5076f1e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,5 +9,6 @@ post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" + get "/search/:query", to: "movies#search", as: "search" end From 0b24fa0b0059da740ff9bbc790482cc976822fd9 Mon Sep 17 00:00:00 2001 From: kee nam Date: Mon, 18 Dec 2017 16:01:46 -0800 Subject: [PATCH 04/10] Changed routes and methods back; found out how to use search --- app/controllers/movies_controller.rb | 5 ----- config/routes.rb | 3 --- 2 files changed, 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 72bd0214..1d9fc14a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -23,11 +23,6 @@ def show ) end - def search() - results = MovieWrapper.search(params[:query]) - render status: :ok, json: results - end - private def require_movie diff --git a/config/routes.rb b/config/routes.rb index 5076f1e1..8646c425 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,4 @@ post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" - - get "/search/:query", to: "movies#search", as: "search" - end From 4cff15f1d23c984cdb2d06661b3a0f65cbe1ea59 Mon Sep 17 00:00:00 2001 From: kee nam Date: Tue, 19 Dec 2017 14:38:34 -0800 Subject: [PATCH 05/10] Took out image URL elsif condition --- app/models/movie.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 2d5bd4d3..4e2a4ada 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -12,8 +12,8 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id - MovieWrapper.construct_image_url(orig_value) + # elsif external_id + # MovieWrapper.construct_image_url(orig_value) else orig_value end From b8ac62fcd4e6be138dff9678ccb4819591c13054 Mon Sep 17 00:00:00 2001 From: kee nam Date: Tue, 19 Dec 2017 16:32:03 -0800 Subject: [PATCH 06/10] Creating add to library method; made successful post request --- app/controllers/movies_controller.rb | 13 +++++++++++++ config/routes.rb | 3 +++ lib/movie_wrapper.rb | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 1d9fc14a..2f70972e 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -23,6 +23,19 @@ def show ) end + def create + movie_data = MovieWrapper.getMovie(params[:id]) + puts movie_data + movie = Movie.new do |m| + m.title = movie_data["title"] + m.overview = movie_data["overview"] + m.release_date = movie_data["release_date"] + m.image_url = movie_data["poster_path"] + end + puts movie + movie.save + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index 8646c425..ae07b476 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,4 +8,7 @@ post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" + + # Add movie + post '/movies/add/:id', to: 'movies#create' end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..bb892166 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -20,6 +20,13 @@ def self.search(query) end end + # https://api.themoviedb.org/3/movie/{movie_id}?api_key=<> + def self.getMovie(external_id) + url = BASE_URL + "movie/" + external_id.to_s + "?api_key=" + KEY + response = HTTParty.get(url) + return response + end + private def self.construct_movie(api_result) From 6edc12a3677601d4d4ce16793d43347fbc873ee9 Mon Sep 17 00:00:00 2001 From: kee nam Date: Tue, 19 Dec 2017 16:46:59 -0800 Subject: [PATCH 07/10] Added conditional for adding a movie to the rental library --- app/controllers/movies_controller.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2f70972e..5987a1e1 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -25,15 +25,19 @@ def show def create movie_data = MovieWrapper.getMovie(params[:id]) - puts movie_data - movie = Movie.new do |m| - m.title = movie_data["title"] - m.overview = movie_data["overview"] - m.release_date = movie_data["release_date"] - m.image_url = movie_data["poster_path"] + + if Movie.where(overview: movie_data["overview"]).length > 0 + # add an error message for the user + else + movie = Movie.new do |m| + m.title = movie_data["title"] + m.overview = movie_data["overview"] + m.release_date = movie_data["release_date"] + m.image_url = movie_data["poster_path"] + end + + movie.save end - puts movie - movie.save end private From c81f81a3a743eb586371d243f66afcb403ffae1f Mon Sep 17 00:00:00 2001 From: kee nam Date: Wed, 20 Dec 2017 12:52:25 -0800 Subject: [PATCH 08/10] Changed routing for post method --- app/controllers/movies_controller.rb | 2 ++ config/routes.rb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 5987a1e1..c7f97d53 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -37,6 +37,8 @@ def create end movie.save + + render status: :ok, json: movie end end diff --git a/config/routes.rb b/config/routes.rb index ae07b476..4101d708 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,12 +3,12 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:index, :show, :create], param: :title post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" # Add movie - post '/movies/add/:id', to: 'movies#create' + # post '/movies/add/:id', to: 'movies#create' end From 5512c0a1e988711baa63fc9804375322ab5f7bf8 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 20 Dec 2017 14:03:51 -0800 Subject: [PATCH 09/10] Add gem cors --- Gemfile | 1 + Gemfile.lock | 2 ++ config/initializers/cors.rb | 9 +++++++++ 3 files changed, 12 insertions(+) create mode 100644 config/initializers/cors.rb diff --git a/Gemfile b/Gemfile index d5c7affd..f07314d9 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem 'will_paginate' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri + gem 'rack-cors' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 438b97de..ef9b3414 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,7 @@ GEM pry (>= 0.10.4) puma (3.11.0) rack (2.0.3) + rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) rails (5.0.6) @@ -213,6 +214,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb new file mode 100644 index 00000000..3c9ca6e6 --- /dev/null +++ b/config/initializers/cors.rb @@ -0,0 +1,9 @@ +Rails.application.config.middleware.insert_before 0, "Rack::Cors" do + allow do + origins 'localhost:8080' + + resource '*', + headers: :any, + methods: [:get, :post, :put, :patch, :delete, :options, :head] + end + end From 6ecd06af5ece6a2dcbd27aa81c2d460312f48e6b Mon Sep 17 00:00:00 2001 From: kee nam Date: Wed, 20 Dec 2017 14:23:14 -0800 Subject: [PATCH 10/10] Implemented create method for new movies to the rental library --- app/controllers/movies_controller.rb | 17 ++++++----------- config/routes.rb | 3 --- lib/movie_wrapper.rb | 1 - 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c7f97d53..c1955a58 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,18 +26,9 @@ def show def create movie_data = MovieWrapper.getMovie(params[:id]) - if Movie.where(overview: movie_data["overview"]).length > 0 - # add an error message for the user - else - movie = Movie.new do |m| - m.title = movie_data["title"] - m.overview = movie_data["overview"] - m.release_date = movie_data["release_date"] - m.image_url = movie_data["poster_path"] - end - + if Movie.where(overview: movie_data["overview"]).length == 0 + movie = Movie.new(movie_params) movie.save - render status: :ok, json: movie end end @@ -50,4 +41,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + return params.require(:movie).permit(:title, :overview, :release_date, :image_url) + end end diff --git a/config/routes.rb b/config/routes.rb index 4101d708..3ff1911d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,4 @@ post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" - - # Add movie - # post '/movies/add/:id', to: 'movies#create' end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index bb892166..f6d4c2d0 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -20,7 +20,6 @@ def self.search(query) end end - # https://api.themoviedb.org/3/movie/{movie_id}?api_key=<> def self.getMovie(external_id) url = BASE_URL + "movie/" + external_id.to_s + "?api_key=" + KEY response = HTTParty.get(url)