forked from AdaGold/video-store-consumer-api
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmovie_wrapper.rb
40 lines (34 loc) · 1.13 KB
/
movie_wrapper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class MovieWrapper
BASE_URL = "https://api.themoviedb.org/3/"
KEY = ENV["MOVIEDB_KEY"]
BASE_IMG_URL = "https://image.tmdb.org/t/p/"
DEFAULT_IMG_SIZE = "w185"
DEFAULT_IMG_URL = "http://lorempixel.com/185/278/"
def self.search(query)
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
# puts url
response = HTTParty.get(url)
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
return movies
end
end
private
def self.construct_movie(api_result)
puts "api image: #{api_result["poster_path"]}"
Movie.new(
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),
external_id: api_result["id"])
end
def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
end
end