Skip to content

Instantly share code, notes, and snippets.

@swindsor
Created February 7, 2011 16:17
Show Gist options
  • Save swindsor/814633 to your computer and use it in GitHub Desktop.
Save swindsor/814633 to your computer and use it in GitHub Desktop.
simple quick netflix client using oauth & crack
#!/usr/bin/env ruby
require 'rubygems'
require 'oauth'
require 'crack'
class Netflix
class CatalogTitle
attr_accessor :average_rating, :release_year, :category, :short_title, :title, :id, :runtime, :box_art, :link
def initialize(attributes = {})
attributes.each do |k,v|
if self.respond_to?("#{k}=".to_sym)
self.send("#{k}=".to_sym, v)
end
end
end
def average_rating=(rating)
@average_rating = rating.to_f
end
def release_year=(year)
@release_year = year.to_i
end
def title=(title)
@short_title = title['short']
@title = title['regular']
end
end
def self.consumer
@@consumer ||= OAuth::Consumer.new('CONSUMER_KEY', 'SHARED_SECRET', :site => 'http://api.netflix.com', :scheme => :query_string)
end
def self.catalog_movie(id)
CatalogTitle.new(Crack::XML.parse(self.get("/catalog/titles/movies/#{id}"))['catalog_title'])
end
def self.catalog_titles(params={})
Crack::XML.parse(self.get('/catalog/titles', params))['catalog_titles']['catalog_title'].map do |c|
CatalogTitle.new(c)
end
end
def self.catalog_titles_index
self.get('/catalog/titles/index')
end
private
def self.stringify_keys(h)
Hash[h.map{|k,v| [k.to_s, v.to_s]}]
end
def self.get(path, params={})
self.consumer.request(:get, path, nil, {}, stringify_keys(params)).body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment