class ImdbApi

Constants

BASE_URL

Public Class Methods

new(api_key) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 7
def initialize(api_key)
  @api_key = api_key
  @options = { method: 'get' }
end

Public Instance Methods

coming_soon() click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 29
def coming_soon
  results = call('ComingSoon')
  results['items']
end
on_theaters() click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 24
def on_theaters
  results = call('InTheaters')
  results['items']
end
recommendations(title) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 12
def recommendations(title)
  id = find_id(title)
  results = call('Title', id)
  results['similars'].first(5)
end
trailer(title) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 18
def trailer(title)
  id = find_id(title)
  results = call('Trailer', id)
  results['link']
end

Private Instance Methods

call(endpoint, identifier = nil) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 36
def call(endpoint, identifier = nil)
  url = create_url(endpoint, identifier)
  response = RestClient::Request.new(
    method: @options[:method],
    url: url
  ).execute
  JSON.parse(response.to_str)
end
create_url(endpoint, identifier) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 50
def create_url(endpoint, identifier)
  title = identifier.gsub(' ', '%20') unless identifier.nil?
  "#{BASE_URL}#{endpoint}/#{@api_key}/#{title}"
end
find_id(title) click to toggle source
# File lib/what_to_watch/imdb_api.rb, line 45
def find_id(title)
  id = call('SearchTitle', title)
  id['results'][0]['id']
end