Created
October 14, 2012 01:08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetflixApi | |
# http://api.netflix.com/catalog/titles/series/70105286 | |
class Response | |
def initialize(item, client) | |
@item = item | |
@client = client | |
end | |
delegate :[], to: :@item | |
def links | |
@links ||= (@item['link'] || []).index_by { |r| r['rel'] } | |
end | |
def url_for(rel) | |
links[rel].try(:[], "href") | |
end | |
def follow(rel) | |
url = url_for rel | |
url && @client.get(url) | |
end | |
def availability | |
response = follow "http://schemas.netflix.com/catalog/titles/format_availability" | |
return if response.blank? | |
response['delivery_formats']['availability'].each_with_object({}) do |format, acc| | |
category = format['category'] | |
from_epoch = format['available_from'] | |
until_epoch = format['available_until'] | |
acc[category['label']] = { | |
available_from: (from_epoch && Time.at(from_epoch)), | |
available_until: (until_epoch && Time.at(until_epoch)), | |
term: category['term'], | |
label: category['label'] | |
} | |
end | |
end | |
def playable_on_instant? | |
available? 'Instant' | |
end | |
def playable_on_bluray? | |
available? 'Blu-ray' | |
end | |
def playable_on_dvd? | |
available? 'DVD' | |
end | |
def available_from(format) | |
availability[format.to_s].try(:[], :available_from) || Time.at(0) | |
end | |
def past_available_from?(format) | |
from = available_from format | |
from.blank? || from < Time.now | |
end | |
def before_available_until?(format) | |
until_time = available_until format | |
until_time.blank? || until_time > Time.now | |
end | |
def available_until(format) | |
availability[format.to_s].try(:[], :available_until) | |
end | |
def available?(format) | |
past_available_from?(format) and before_available_until?(format) | |
end | |
def title | |
titles = self['title'] | |
return unless titles.present? | |
titles['regular'] || titles['short'] | |
end | |
def box_art_url | |
self['box_art'].try :[], 'large' | |
end | |
def average_rating | |
self['average_rating'] | |
end | |
def id | |
self['id'] | |
end | |
def release_year | |
self['release_year'] | |
end | |
def seasons | |
follow 'http://schemas.netflix.com/catalog/titles.seasons' | |
end | |
end | |
def self.default | |
Thread.current[:netflix_api] ||= new | |
end | |
attr_reader :connection | |
def initialize | |
@connection = Faraday.new(url: 'http://api-public.netflix.com') do |faraday| | |
faraday.request :oauth, consumer_key: Settings.netflix_key, consumer_secret: Settings.netflix_secret | |
faraday.request :url_encoded | |
faraday.response :json | |
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP | |
end | |
end | |
def get(uri) | |
wrap connection.get(URI.parse(uri).path, output: 'json').body | |
end | |
def wrap(value) | |
if value.has_key? 'catalog_title' | |
Response.new value['catalog_title'], self | |
elsif value.has_key?('catalog_titles') | |
Array.wrap(value['catalog_titles']['catalog_title']).map { |r| Response.new r, self } | |
else | |
Response.new value, self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment