Class: NetFlix::Authenticator

Inherits:
Valuable
  • Object
show all
Defined in:
lib/net_flix/authenticator.rb

Instance Method Summary collapse

Methods inherited from Valuable

#attributes, attributes, create_accessor_for, create_setter_for, #deep_duplicate_of, defaults, has_collection, has_value, #initialize

Constructor Details

This class inherits a constructor from Valuable

Instance Method Details

#access_tokenObject



11
12
13
# File 'lib/net_flix/authenticator.rb', line 11

def access_token
  credentials.access_token
end

#add_authentication_parameters!Object



50
51
52
# File 'lib/net_flix/authenticator.rb', line 50

def add_authentication_parameters!
  request.parameters = request.parameters.merge(authentication_parameters)
end

#add_signature!Object



54
55
56
57
# File 'lib/net_flix/authenticator.rb', line 54

def add_signature!
  sign = {'oauth_signature' => Request.encode(signature)}
  request.parameters = request.parameters.merge( sign  )
end

#authentication_parametersObject



40
41
42
43
44
45
46
47
48
# File 'lib/net_flix/authenticator.rb', line 40

def authentication_parameters
  {
  'oauth_consumer_key' => key,
  'oauth_signature_method' => 'HMAC-SHA1',
  'oauth_timestamp' => Time.now.to_i,
  'oauth_nonce' => nonce,
  'oauth_version' => '1.0'
  }
end

#encoded_parametersObject



65
66
67
# File 'lib/net_flix/authenticator.rb', line 65

def encoded_parameters
  Request.encode request.parameter_string
end

#encoded_urlObject



69
70
71
# File 'lib/net_flix/authenticator.rb', line 69

def encoded_url
  Request.encode request.url
end

#keyObject



15
16
17
# File 'lib/net_flix/authenticator.rb', line 15

def key
  credentials.key
end

#require_credentialsObject

Raises:

  • (ArgumentError)


23
24
25
# File 'lib/net_flix/authenticator.rb', line 23

def require_credentials
  raise( ArgumentError, "You must configure your NetFlix API key and secret before using netflix4r.") unless credentials.valid?
end

#secretObject



19
20
21
# File 'lib/net_flix/authenticator.rb', line 19

def secret
  credentials.secret
end

#sign!Object



59
60
61
62
63
# File 'lib/net_flix/authenticator.rb', line 59

def sign!
  require_credentials
  add_authentication_parameters!
  add_signature!
end

#signatureObject



36
37
38
# File 'lib/net_flix/authenticator.rb', line 36

def signature
  Base64.encode64(HMAC::SHA1.digest(signature_key,signature_base_string)).chomp.gsub(/\n/,'')
end

#signature_base_stringObject



27
28
29
30
# File 'lib/net_flix/authenticator.rb', line 27

def signature_base_string
  add_authentication_parameters!
  [request.http_method, encoded_url, encoded_parameters].join('&') 
end

#signature_keyObject



32
33
34
# File 'lib/net_flix/authenticator.rb', line 32

def signature_key
  "#{Request.encode(secret)}&#{Request.encode(access_token)}"
end