kubrick 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -1
- data/README.md +42 -0
- data/kubrick.gemspec +7 -4
- data/lib/kubrick.rb +10 -42
- data/lib/kubrick/authenticator.rb +68 -0
- data/lib/kubrick/errors.rb +5 -2
- data/lib/kubrick/movie.rb +25 -0
- data/lib/kubrick/netflix_api.rb +22 -0
- data/lib/kubrick/version.rb +1 -1
- metadata +26 -10
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Kubrick
|
2
|
+
## A simple Ruby wrapper for the Netflix REST API
|
3
|
+
|
4
|
+
---
|
5
|
+
|
6
|
+
Kubrick is a Ruby wrapper for the Netflix REST API. It is nowhere near ready for use.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
1. Go to the [Netflix API keys website](http://developer.netflix.com/apps/mykeys) and sign up for an account if you haven't done so yet. Register an application.
|
10
|
+
|
11
|
+
2. Start the OAuth authentication process. Kubrick's OAuth setup is designed to be as simple as possible. Start by initializing the `Authenticator` class and passing in your consumer key and secret:
|
12
|
+
|
13
|
+
authenticator = Kubrick::Authenticator.new("consumer key", "consumer secret")
|
14
|
+
|
15
|
+
Next, get a request token. Note that you must save this temporarily somehow as you will need it after you redirect your user to Netflix's login page.
|
16
|
+
|
17
|
+
request_token = authenticator.get_request_token
|
18
|
+
|
19
|
+
Pass the output of that method and your callback URL into `login_url` to get the URL you should redirect your user to:
|
20
|
+
|
21
|
+
redirect_url = authenticator.login_url(request_token, "callback_url")
|
22
|
+
|
23
|
+
When your user returns, pass your request token (the one you had to save earlier) into `get_access_token`.
|
24
|
+
|
25
|
+
access_token = authenticator.get_access_token(request_token)
|
26
|
+
|
27
|
+
MAKE SURE TO SAVE THE ACCESS TOKEN. It won't change unless the user revokes access to your application.
|
28
|
+
|
29
|
+
You can now make calls by passing in the access token details and your consumer details. Like this:
|
30
|
+
|
31
|
+
m = Kubrick::Movie.new(access_token, {:oauth_consumer_key => "consumer_key", :oauth_consumer_secret => "consumer_secret"})
|
32
|
+
m.find_by_title("2001: A Space Odyssey")
|
33
|
+
|
34
|
+
## Warning!
|
35
|
+
|
36
|
+
Kubrick is by no means ready for use. It's not even close to complete. If you want to use it, go ahead, but be warned that the API's may change by surprise.
|
37
|
+
|
38
|
+
## Copyright, blah blah blah
|
39
|
+
|
40
|
+
Do whatever the heck you want with this. Fork it, rewrite, sell it as your own. I don't care. But *please* retain credit to me and any contributors.
|
41
|
+
|
42
|
+
© Copyright 2012 J-P Teti.
|
data/kubrick.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["J-P Teti"]
|
9
9
|
s.email = ["roboteti@gmail.com"]
|
10
10
|
s.homepage = ""
|
11
|
-
s.summary =
|
12
|
-
s.description =
|
11
|
+
s.summary = %q{Netflix REST API wrapper}
|
12
|
+
s.description = %q{A Ruby wrapper for the Netflix REST API.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "kubrick"
|
15
15
|
|
@@ -18,6 +18,9 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
|
22
|
-
s.
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "simple_oauth"
|
24
|
+
s.add_runtime_dependency "faraday"
|
25
|
+
s.add_runtime_dependency "crack"
|
23
26
|
end
|
data/lib/kubrick.rb
CHANGED
@@ -1,44 +1,12 @@
|
|
1
|
-
|
1
|
+
module Kubrick
|
2
|
+
NETFLIX_API_ROOT = "api.netflix.com"
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'simple_oauth'
|
6
|
+
require 'faraday'
|
7
|
+
require 'crack'
|
2
8
|
require File.expand_path("../kubrick/version", __FILE__)
|
3
9
|
require File.expand_path("../kubrick/errors", __FILE__)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class Movie
|
9
|
-
@svc = OData::Service.new NETFLIX_API_ROOT
|
10
|
-
|
11
|
-
def self.find_by_title(title)
|
12
|
-
if title.nil?
|
13
|
-
raise Errors::KubrickError "Movie.find_by_title: title cannot be nil."
|
14
|
-
else
|
15
|
-
@svc.Titles.filter("Name eq '#{title}'")
|
16
|
-
movie = @svc.execute
|
17
|
-
JSON.parse(movie.to_json) # I know, this is horrible, but I couldn't get it to work any other way.
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.find_by_director(name)
|
22
|
-
if name.nil?
|
23
|
-
raise Errors::KubrickError "Movie.find_by_director: name cannot be nil"
|
24
|
-
end
|
25
|
-
@svc.People.filter("Name eq '#{name}'").expand('TitlesDirected')
|
26
|
-
person = @svc.execute
|
27
|
-
parsed = JSON.parse(person.to_json)
|
28
|
-
parsed[0]['TitlesDirected']
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class Person
|
33
|
-
@svc = OData::Service.new NETFLIX_API_ROOT
|
34
|
-
|
35
|
-
def self.find_by_name(name)
|
36
|
-
if name.nil?
|
37
|
-
raise Errors::KubrickError "Person.find_by_name: name cannot be nil"
|
38
|
-
end
|
39
|
-
@svc.People.filter("Name eq '#{name}'").expand('TitlesDirected')
|
40
|
-
person = @svc.execute
|
41
|
-
JSON.parse(person.to_json)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
10
|
+
require File.expand_path("../kubrick/authenticator", __FILE__)
|
11
|
+
require File.expand_path("../kubrick/netflix_api", __FILE__)
|
12
|
+
require File.expand_path("../kubrick/movie", __FILE__)
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Kubrick
|
2
|
+
class Authenticator
|
3
|
+
attr_reader :key, :secret
|
4
|
+
|
5
|
+
def initialize(key, secret)
|
6
|
+
@key = key
|
7
|
+
@secret = secret
|
8
|
+
@conn = Faraday.new(:url => "http://#{NETFLIX_API_ROOT}") do |builder|
|
9
|
+
builder.adapter :net_http
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_request_token
|
14
|
+
headers_g = SimpleOAuth::Header.new(:get, "http://#{NETFLIX_API_ROOT}/oauth/request_token", nil, {:consumer_key => @key, :consumer_secret => @secret})
|
15
|
+
headers = headers_g.signed_attributes
|
16
|
+
request = @conn.get do |req|
|
17
|
+
req.url '/oauth/request_token'
|
18
|
+
req.params['oauth_consumer_key'] = headers[:oauth_consumer_key]
|
19
|
+
req.params['oauth_nonce'] = headers[:oauth_nonce]
|
20
|
+
req.params['oauth_signature_method'] = headers[:oauth_signature_method]
|
21
|
+
req.params['oauth_timestamp'] = headers[:oauth_timestamp]
|
22
|
+
req.params['oauth_version'] = headers[:oauth_version]
|
23
|
+
req.params['oauth_signature'] = headers[:oauth_signature]
|
24
|
+
end
|
25
|
+
if request.status == 200
|
26
|
+
step1 = request.body.split("&")
|
27
|
+
processed_response = {}
|
28
|
+
for item in step1
|
29
|
+
q = item.split("=")
|
30
|
+
processed_response[:"#{q[0]}"] = CGI::unescape(q[1])
|
31
|
+
end
|
32
|
+
processed_response
|
33
|
+
else
|
34
|
+
raise Kubrick::Errors::AuthenticatorError.new "get_request_token: #{request.body}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def login_url(oauth_values = {}, callback_url)
|
39
|
+
"https://api-user.netflix.com/oauth/login?oauth_token=#{oauth_values[:oauth_token]}&oauth_consumer_key=#{@key}&application_name=#{oauth_values[:application_name]}&oauth_callback=#{CGI::escape(callback_url)}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_access_token(oauth_values = {})
|
43
|
+
headers_g = SimpleOAuth::Header.new(:get, "http://#{NETFLIX_API_ROOT}/oauth/access_token", nil, {:consumer_key => @key, :token => oauth_values[:oauth_token], :consumer_secret => @secret, :token_secret => oauth_values[:oauth_token_secret]})
|
44
|
+
headers = headers_g.signed_attributes
|
45
|
+
request = @conn.get do |req|
|
46
|
+
req.url '/oauth/access_token'
|
47
|
+
req.params['oauth_consumer_key'] = headers[:oauth_consumer_key]
|
48
|
+
req.params['oauth_nonce'] = headers[:oauth_nonce]
|
49
|
+
req.params['oauth_signature_method'] = headers[:oauth_signature_method]
|
50
|
+
req.params['oauth_timestamp'] = headers[:oauth_timestamp]
|
51
|
+
req.params['oauth_token'] = headers[:oauth_token]
|
52
|
+
req.params['oauth_version'] = headers[:oauth_version]
|
53
|
+
req.params['oauth_signature'] = headers[:oauth_signature]
|
54
|
+
end
|
55
|
+
if request.status == 200
|
56
|
+
step1 = request.body.split("&")
|
57
|
+
processed_response = {}
|
58
|
+
for item in step1
|
59
|
+
q = item.split("=")
|
60
|
+
processed_response[:"#{q[0]}"] = CGI::unescape(q[1])
|
61
|
+
end
|
62
|
+
processed_response
|
63
|
+
else
|
64
|
+
raise Kubrick::Errors::AuthenticatorError.new "get_access_token: #{request.body}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/kubrick/errors.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module Kubrick
|
2
2
|
module Errors
|
3
|
-
class KubrickError <
|
4
|
-
end
|
3
|
+
class KubrickError < Exception; end
|
4
|
+
class AuthenticatorError < KubrickError; end
|
5
|
+
class HTTPError < KubrickError; end
|
6
|
+
class LimitError < KubrickError; end
|
7
|
+
class APIError < KubrickError; end
|
5
8
|
end
|
6
9
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kubrick
|
2
|
+
class Movie < NetflixAPI
|
3
|
+
def find_by_title(title)
|
4
|
+
headers_g = SimpleOAuth::Header.new(:get, "http://#{NETFLIX_API_ROOT}/catalog/titles", {'term' => CGI::escape(title)}, {:consumer_key => @oauth_consumer_key, :consumer_secret => @oauth_consumer_secret, :token => @oauth_token, :token_secret => @oauth_token_secret})
|
5
|
+
headers = headers_g.signed_attributes
|
6
|
+
request = @conn.get do |req|
|
7
|
+
req.url '/catalog/titles'
|
8
|
+
req.params['oauth_consumer_key'] = headers[:oauth_consumer_key]
|
9
|
+
req.params['oauth_nonce'] = headers[:oauth_nonce]
|
10
|
+
req.params['oauth_signature_method'] = headers[:oauth_signature_method]
|
11
|
+
req.params['oauth_timestamp'] = headers[:oauth_timestamp]
|
12
|
+
req.params['oauth_token'] = headers[:oauth_token]
|
13
|
+
req.params['oauth_version'] = headers[:oauth_version]
|
14
|
+
req.params['term'] = CGI::escape(title)
|
15
|
+
req.params['oauth_signature'] = headers[:oauth_signature]
|
16
|
+
end
|
17
|
+
if request.status == 200
|
18
|
+
response = Crack::XML.parse(request.body)
|
19
|
+
response["catalog_titles"]["catalog_title"]
|
20
|
+
else
|
21
|
+
raise Kubrick::Errors::APIError.new "Movie.find_by_title: #{request.body}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Kubrick
|
2
|
+
class NetflixAPI
|
3
|
+
def initialize(oauth, oauth_consumer)
|
4
|
+
@oauth_consumer_key = oauth_consumer[:oauth_consumer_key]
|
5
|
+
@oauth_consumer_secret = oauth_consumer[:oauth_consumer_secret]
|
6
|
+
@oauth_token = oauth[:oauth_token]
|
7
|
+
@user_id = oauth[:user_id]
|
8
|
+
@oauth_token_secret = oauth[:oauth_token_secret]
|
9
|
+
@conn = Faraday.new(:url => "http://#{NETFLIX_API_ROOT}") do |builder|
|
10
|
+
builder.adapter :net_http
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def say_hello
|
15
|
+
"Hello, Dave."
|
16
|
+
end
|
17
|
+
|
18
|
+
def wheres_johnny
|
19
|
+
"Here's Johnny!"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/kubrick/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubrick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: simple_oauth
|
16
|
+
requirement: &6263710 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *6263710
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: faraday
|
27
|
+
requirement: &6263500 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,8 +32,19 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
-
|
35
|
+
version_requirements: *6263500
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: crack
|
38
|
+
requirement: &6263290 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *6263290
|
47
|
+
description: A Ruby wrapper for the Netflix REST API.
|
37
48
|
email:
|
38
49
|
- roboteti@gmail.com
|
39
50
|
executables: []
|
@@ -41,11 +52,16 @@ extensions: []
|
|
41
52
|
extra_rdoc_files: []
|
42
53
|
files:
|
43
54
|
- .gitignore
|
55
|
+
- CHANGELOG.md
|
44
56
|
- Gemfile
|
57
|
+
- README.md
|
45
58
|
- Rakefile
|
46
59
|
- kubrick.gemspec
|
47
60
|
- lib/kubrick.rb
|
61
|
+
- lib/kubrick/authenticator.rb
|
48
62
|
- lib/kubrick/errors.rb
|
63
|
+
- lib/kubrick/movie.rb
|
64
|
+
- lib/kubrick/netflix_api.rb
|
49
65
|
- lib/kubrick/version.rb
|
50
66
|
homepage: ''
|
51
67
|
licenses: []
|
@@ -70,5 +86,5 @@ rubyforge_project: kubrick
|
|
70
86
|
rubygems_version: 1.8.15
|
71
87
|
signing_key:
|
72
88
|
specification_version: 3
|
73
|
-
summary: Netflix
|
89
|
+
summary: Netflix REST API wrapper
|
74
90
|
test_files: []
|