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

Jan & Amy -- Carets #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@

# Ignore Byebug command history file.
.byebug_history

.env
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'coffee-rails', '~> 4.2'

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'rack-cors'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ GEM
pry (>= 0.9.10)
puma (3.6.2)
rack (2.0.1)
rack-cors (1.0.2)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
Expand Down Expand Up @@ -208,6 +209,7 @@ DEPENDENCIES
minitest-spec-rails
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand All @@ -220,4 +222,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.14.6
1.16.0.pre.3
11 changes: 11 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def show
)
end

def create
movie = Movie.new(movie_params)
movie.save
render status: :ok, json: movie

end

private

def require_movie
Expand All @@ -29,4 +36,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
params.require(:movie).permit(:title, :overview, :release_date, :image_url)
end
end
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
end

JSON.parse(File.read('db/seeds/movies.json')).each do |movie_data|
puts "MOVIE DATA: #{movie_data["title"]}"
movies = MovieWrapper.search(movie_data["title"])
movies.first.save unless movies.empty?
end
11 changes: 8 additions & 3 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class MovieWrapper

def self.search(query)
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
# puts url
puts url
response = HTTParty.get(url)
if response["total_results"] == 0
return []
else
puts query
movies = response["results"].map do |result|
self.construct_movie(result)
end
Expand All @@ -27,12 +28,16 @@ def self.construct_movie(api_result)
title: api_result["title"],
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
image_url: (api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil ),
external_id: api_result["id"])
end

def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
if img_name.include?(BASE_IMG_URL)
return img_name
else
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
end
end

end