Skip to content

Instantly share code, notes, and snippets.

@Haumer
Last active August 20, 2020 20:12
Show Gist options
  • Select an option

  • Save Haumer/56c77ecbcc45d88c7b94ecac485dc72d to your computer and use it in GitHub Desktop.

Select an option

Save Haumer/56c77ecbcc45d88c7b94ecac485dc72d to your computer and use it in GitHub Desktop.
imdb api for Nextbinge
require 'uri'
require 'net/http'
require 'openssl'
require 'json'
class IMDB
attr_reader :data
BASE_URL = URI('https://imdb-internet-movie-database-unofficial.p.rapidapi.com/search/').freeze
def initialize(movie:)
@movie = movie
@url = url
@response = api_response
@data = set_movie_data
end
def url
BASE_URL + @movie.to_s
end
def api_response
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(url)
request['x-rapidapi-host'] = 'imdb-internet-movie-database-unofficial.p.rapidapi.com'
request['x-rapidapi-key'] = 'dkFMjOW36JmshaQjczgek92fjz4jp15oZXcjsn2N2fvhWKjgC7'
response = http.request(request)
JSON.parse(response.read_body)
end
def movie_found?
@response['titles'].any?
end
def set_movie_data
return [] unless movie_found?
@response['titles'].map do |match|
movie_data = Struct.new(:title, :image, :id)
movie_data.new(
match['title'],
match['image'],
match['id']
)
end
end
end
imdb = IMDB.new(movie: 'inception')
imdb.data
#=> [#<struct title="Inception", image="https://[...]", id="tt1375666">, [...]]
imdb.data.first.title
#=> "Inception"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment