Class: Rdio::BaseApi

Inherits:
Object show all
Defined in:
lib/rdio/base.rb

Overview


Basis for making web-service calls and constructing the values. Subclasses should declare the api by calling ‘call’, ‘return_object’, and ‘create_object’


Direct Known Subclasses

Api

Constant Summary collapse

PATH =
'/1/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, secret = nil) ⇒ BaseApi

string string

key and secret can be ‘nil’ because users can now set the access_token directly



267
268
269
270
271
# File 'lib/rdio/base.rb', line 267

def initialize(key=nil,secret=nil)
  @oauth = RdioOAuth.new key,secret
  @access_token_auth = nil
  @access_token_no_auth = nil
end

Instance Attribute Details

#oauthObject (readonly)

Returns the value of attribute oauth.



260
261
262
# File 'lib/rdio/base.rb', line 260

def oauth
  @oauth
end

Instance Method Details

#access_token=(token) ⇒ Object

Token -> Void

Sets both unauthorized and authorized tokens to token



290
291
292
293
# File 'lib/rdio/base.rb', line 290

def access_token=(token)
  @access_token_auth = token
  @access_token_no_auth = token
end

#authorizeObject

Forces authorization



333
334
335
# File 'lib/rdio/base.rb', line 333

def authorize
  access_token true
end

#call(method, args, requires_auth = false) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/rdio/base.rb', line 295

def call(method,args,requires_auth=false)
  #
  # Convert object with keys just to use their keys
  #
  args = Rdio::convert_args args
  if Rdio::log_methods
    Rdio::log "Called method: #{method}(#{args}) : auth=#{requires_auth}"
  end
  new_args = {}
  new_args['method'] = method
  args.each do |k,v|
    new_args[k] = v.to_k.to_s
  end
  url = PATH
  if Rdio::log_posts
    Rdio::log "Post to url=#{url} method=#{method} args=#{args}"
  end
  #
  # For backwards compatibility the response may have a 'body'
  # attribute or a tuple could be returned.  Handle both cases.
  #
  resp,data = access_token(requires_auth).post url,new_args
  begin
    return resp.body
  rescue
  end
  return data
end

#get_pinObject



282
283
284
# File 'lib/rdio/base.rb', line 282

def get_pin
  @oauth.get_pin
end

#get_pin=(get_pin) ⇒ Object

(string -> string) -> (string -> string)

Sets the function that will return a pin given an authorization url for the contained RdioOAuth instance



278
279
280
# File 'lib/rdio/base.rb', line 278

def get_pin=(get_pin)
  @oauth.get_pin = get_pin
end

#return_object(type, method, args, requires_auth = false) ⇒ Object



324
325
326
327
328
329
330
# File 'lib/rdio/base.rb', line 324

def return_object(type,method,args,requires_auth=false)
  json = call method,args,requires_auth
  if Rdio::log_json
    Rdio::log json
  end
  create_object type,json
end