Class: Api

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer, token = nil) ⇒ Api

Returns a new instance of Api.



30
31
32
33
# File 'lib/api.rb', line 30

def initialize(consumer, token=nil)
  @consumer = consumer
  @token = token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object (private)



78
79
80
# File 'lib/api.rb', line 78

def method_missing(method, *params)
  call(method.to_s, params[0])
end

Instance Attribute Details

#consumerObject

the consumer and token can be accessed



28
29
30
# File 'lib/api.rb', line 28

def consumer
  @consumer
end

#tokenObject

the consumer and token can be accessed



28
29
30
# File 'lib/api.rb', line 28

def token
  @token
end

Instance Method Details

#begin_authentication(callback_url) ⇒ Object



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

def begin_authentication(callback_url)
  # request a request token from the server
  response = signed_post('http://api.rdio.com/oauth/request_token',
                         {'oauth_callback' => callback_url})
  # parse the response
  parsed = CGI.parse(response)
  # save the token
  @token = [parsed['oauth_token'][0], parsed['oauth_token_secret'][0]]
  # return an URL that the user can use to authorize this application
  return parsed['login_url'][0] + '?oauth_token=' + parsed['oauth_token'][0]
end

#call(method, params = {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/api.rb', line 57

def call(method, params={})
  # make a copy of the dict
  params = params.clone
  # put the method in the dict
  params['method'] = method
  # call to the server and parse the response
  return JSON.load(signed_post('http://api.rdio.com/1/', params))
end

#complete_authentication(verifier) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/api.rb', line 47

def complete_authentication(verifier)
  # request an access token
  response = signed_post('http://api.rdio.com/oauth/access_token',
                         {'oauth_verifier' => verifier})
  # parse the response
  parsed = CGI.parse(response)
  # save the token
  @token = [parsed['oauth_token'][0], parsed['oauth_token_secret'][0]]
end