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 CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ .DS_Store
@@ -0,0 +1,9 @@
1
+ # v0.1.1
2
+ ## Feb. 20, 2012
3
+ ---
4
+ - Complete rewrite. Updated to use OAuth and be REST, not catalog API wrapper (eventually).
5
+
6
+ # v0.0.1
7
+ ## Feb. 11, 2012
8
+ ---
9
+ - First release
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in kubrick.gemspec
4
- gemspec
4
+ gemspec
@@ -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.
@@ -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 = "Netflix Catalog API wrapper"
12
- s.description = "A Ruby wrapper for the Netflix Catalog API."
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
- s.add_runtime_dependency "httparty"
22
- s.add_runtime_dependency "ruby_odata"
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
@@ -1,44 +1,12 @@
1
- require 'ruby_odata'
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
- module Kubrick
6
- NETFLIX_API_ROOT = 'http://odata.netflix.com/Catalog'
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
@@ -1,6 +1,9 @@
1
1
  module Kubrick
2
2
  module Errors
3
- class KubrickError < StandardError
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
@@ -1,3 +1,3 @@
1
1
  module Kubrick
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
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.0.1
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-11 00:00:00.000000000 Z
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httparty
16
- requirement: &8568450 !ruby/object:Gem::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: *8568450
24
+ version_requirements: *6263710
25
25
  - !ruby/object:Gem::Dependency
26
- name: ruby_odata
27
- requirement: &8567570 !ruby/object:Gem::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: *8567570
36
- description: A Ruby wrapper for the Netflix Catalog API.
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 Catalog API wrapper
89
+ summary: Netflix REST API wrapper
74
90
  test_files: []