From 23681a2d2c5f11f3f86ec7cba8089fb561f37e45 Mon Sep 17 00:00:00 2001 From: Maja Graonic Date: Mon, 18 Jun 2018 15:09:54 -0700 Subject: [PATCH 1/5] added API tokena and key --- Gemfile.lock | 2 +- app/controllers/movies_controller.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..22779079 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,7 @@ GEM slop (~> 3.4) pry-rails (0.3.4) pry (>= 0.9.10) - puma (3.6.2) + puma (3.11.4) rack (2.0.1) rack-test (0.6.3) rack (>= 1.0) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..d523d5f1 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,8 @@ def show ) end + # TODO: create method and route + private def require_movie From 4ad2090d323e8ebd1bdfeb95a3a35530fa4e3f51 Mon Sep 17 00:00:00 2001 From: Maja Graonic Date: Tue, 19 Jun 2018 13:59:14 -0700 Subject: [PATCH 2/5] reconfigured api --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/movies_controller.rb | 14 ++++++++++++++ config/application.rb | 11 +++++++---- config/routes.rb | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 009415af..7f16215c 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end +gem 'rack-cors', require: 'rack/cors' gem 'awesome_print' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 22779079..fcb76c2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,7 @@ GEM pry (>= 0.9.10) puma (3.11.4) rack (2.0.1) + rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -210,6 +211,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index d523d5f1..6c49cd73 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,17 @@ def show ) end + def create + # see if movie already exists in library (Movie.find_by(external_id: params[:external_id])) + + movie = Movie.new(movie_params) + if movie.save + render json: { id: movie.id}, status: :ok + else + render json: { errors: movie.errors.messages }, status: :bad_request + end + end + # TODO: create method and route private @@ -31,4 +42,7 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + def movie_params + params.permit(:title, :overview, :release_date, :image_url, :inventory) + end end diff --git a/config/application.rb b/config/application.rb index cc803322..d8432a5d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,9 +15,12 @@ class Application < Rails::Application #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') - config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => '*', - 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") - } + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', headers: :any, methods: [:get, :post, :patch, :delete, :options] + end + end + end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..8eff1ff6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ 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" From d2e4a3e59f44ff7d6ec2682dc011be8dfdc87899 Mon Sep 17 00:00:00 2001 From: Maja Graonic Date: Thu, 21 Jun 2018 10:15:30 -0700 Subject: [PATCH 3/5] added validations to movie model --- app/controllers/movies_controller.rb | 4 ++-- app/models/movie.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6c49cd73..f3e3e9ab 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -23,7 +23,7 @@ def show def create # see if movie already exists in library (Movie.find_by(external_id: params[:external_id])) - + movie = Movie.new(movie_params) if movie.save render json: { id: movie.id}, status: :ok @@ -43,6 +43,6 @@ def require_movie end end def movie_params - params.permit(:title, :overview, :release_date, :image_url, :inventory) + params.permit(:title, :overview, :release_date, :image_url, :inventory, :external_id) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..9387e18e 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :external_id, uniqueness: true + # validate :exists?, on: :create def available_inventory self.inventory - Rental.where(movie: self, returned: false).length @@ -16,4 +18,15 @@ def image_url orig_value end end + + private + # def exists? + # if Movie.find_by(external_id: self.external_id) + # return render status: :bad_request, json: { + # errors: { + # rental: ["#{params[:title]} is already in the library"] + # } + # } + # end + # end end From 65175522399cfdbe96bf95ee7d3b4ced962150e6 Mon Sep 17 00:00:00 2001 From: Maja Graonic Date: Thu, 21 Jun 2018 16:06:14 -0700 Subject: [PATCH 4/5] fixed url reconstrucition issue --- app/controllers/movies_controller.rb | 4 ++-- app/models/movie.rb | 14 ++------------ lib/movie_wrapper.rb | 2 ++ 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index f3e3e9ab..68c8791c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,9 +22,9 @@ def show end def create - # see if movie already exists in library (Movie.find_by(external_id: params[:external_id])) - movie = Movie.new(movie_params) + puts "Movie params: #{movie_params}" + puts "Movie image url: #{movie.image_url}" if movie.save render json: { id: movie.id}, status: :ok else diff --git a/app/models/movie.rb b/app/models/movie.rb index 9387e18e..234abb6e 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -10,23 +10,13 @@ def available_inventory def image_url orig_value = read_attribute :image_url + puts "image_url method orig_value: #{orig_value}" if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id + elsif external_id && (orig_value[0] != "h") MovieWrapper.construct_image_url(orig_value) else orig_value end end - - private - # def exists? - # if Movie.find_by(external_id: self.external_id) - # return render status: :bad_request, json: { - # errors: { - # rental: ["#{params[:title]} is already in the library"] - # } - # } - # end - # end end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..d0cb3f78 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -13,6 +13,7 @@ def self.search(query) if response["total_results"] == 0 return [] else + puts "original results from external query: #{response["results"][0]}" movies = response["results"].map do |result| self.construct_movie(result) end @@ -23,6 +24,7 @@ def self.search(query) private def self.construct_movie(api_result) + puts "api image: #{api_result["poster_path"]}" Movie.new( title: api_result["title"], overview: api_result["overview"], From 7996fd4389be2559f22f203951676d5a43789990 Mon Sep 17 00:00:00 2001 From: Maja Graonic Date: Thu, 21 Jun 2018 16:14:38 -0700 Subject: [PATCH 5/5] reconfigured image reconstruction solution --- app/models/movie.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 234abb6e..6830d46e 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -13,10 +13,16 @@ def image_url puts "image_url method orig_value: #{orig_value}" if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id && (orig_value[0] != "h") + elsif external_id && !url_valid?(orig_value) MovieWrapper.construct_image_url(orig_value) else orig_value end end + +private + def url_valid?(url) + url = URI.parse(url) rescue false + url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS) + end end