Class: Netflix::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/netflix/client.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_access_token = nil, user_access_secret = nil) ⇒ Client

attr_accessor :oauth_consumer, :oauth_access_token



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/netflix/client.rb', line 11

def initialize(user_access_token=nil, user_access_secret=nil)
  @oauth_consumer = OAuth::Consumer.new(Client.consumer_key, Client.consumer_secret,
    :site => "http://api.netflix.com", 
    :request_token_url => "http://api.netflix.com/oauth/request_token", 
    :access_token_url => "http://api.netflix.com/oauth/access_token", 
    :authorize_url => "https://api-user.netflix.com/oauth/login")
  if user_access_token && user_access_secret
    @oauth_access_token = oauth_access_token(user_access_token, user_access_secret)
    #automatically determine if error should be thrown based on response codes
    @oauth_access_token.extend(ResponseErrorDecorator)
  elsif !!user_access_token ^ !!user_access_secret
    raise ArgumentError 'Must specify both user_access_token and user_access_secret if specifying either'
  end
end

Class Attribute Details

.app_nameObject

Returns the value of attribute app_name.



6
7
8
# File 'lib/netflix/client.rb', line 6

def app_name
  @app_name
end

.consumer_keyObject

Returns the value of attribute consumer_key.



6
7
8
# File 'lib/netflix/client.rb', line 6

def consumer_key
  @consumer_key
end

.consumer_secretObject

Returns the value of attribute consumer_secret.



6
7
8
# File 'lib/netflix/client.rb', line 6

def consumer_secret
  @consumer_secret
end

Instance Method Details

#handle_oauth_callback(request_token, oauth_verifier) ⇒ Object



55
56
57
# File 'lib/netflix/client.rb', line 55

def handle_oauth_callback(request_token, oauth_verifier)
  access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)
end

#oauthObject

launches the Netflix OAuth page, and asks for the pin this is interactive (i.e. irb or commandline)



36
37
38
39
40
41
42
43
44
45
# File 'lib/netflix/client.rb', line 36

def oauth
  request_token = @oauth_consumer.get_request_token
  authorize_url = request_token.authorize_url(:oauth_consumer_key => 
    Netflix::Client.consumer_key)
  Launchy.open(authorize_url)
  puts "Go to browser, a page has been opened to establish oauth"
  printf "Pin from Netflix:"
  pin = gets.chomp
  access_token = request_token.get_access_token(:oauth_verifier => pin)
end

#oauth_via_callback(callback_url) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/netflix/client.rb', line 47

def oauth_via_callback(callback_url)
  request_token = @oauth_consumer.get_request_token(:oauth_callback => 
    callback_url, :application_name => Netflix::Client.app_name)
  authorize_url = request_token.authorize_url(:oauth_consumer_key => 
    Netflix::Client.consumer_key)
  [request_token, authorize_url]
end

#user(user_id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/netflix/client.rb', line 26

def user(user_id)
  if @oauth_access_token
    @user ||= User.new(@oauth_access_token, user_id)
  else
    raise "Must instantiate client with user auth token/secret"
  end
end