Module: Rdio

Defined in:
lib/rdio.rb,
lib/rdio/api.rb,
lib/rdio/base.rb,
lib/rdio/call.rb,
lib/rdio/oauth.rb,
lib/rdio/types.rb,
lib/rdio/datatypes.rb,
lib/rdio/simple_om.rb,
lib/rdio/simple_rdio.rb

Overview

third party

Defined Under Namespace

Classes: ActivityStream, Album, AlbumData, Api, ApiObj, Artist, ArtistData, BaseApi, BaseObj, Boolean, Call, JSONObj, OM, Playlist, PlaylistData, RdioOAuth, Search, SimpleRdio, TODO, Track, TrackData, User, UserData

Constant Summary collapse

VERSION =
'0.0.1'
RDIO_KEY =
'RDIO_KEY'
RDIO_SECRET =
'RDIO_SECRET'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.debugObject

Returns the value of attribute debug.



17
18
19
# File 'lib/rdio.rb', line 17

def debug
  @debug
end

.log_couldnt_find_symbolsObject

Specific setting to turn off warnings like the following:

Couldn't find symbol: radio_key => rr31531 for type: Rdio::Artist

during tests, because we see it constantly and isn’t really a problem. By default it is true, but it is turned off for all tests.



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

def log_couldnt_find_symbols
  @log_couldnt_find_symbols
end

.log_fillObject

Returns the value of attribute log_fill.



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

def log_fill
  @log_fill
end

.log_jsonObject

Returns the value of attribute log_json.



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

def log_json
  @log_json
end

.log_methodsObject

Returns the value of attribute log_methods.



20
21
22
# File 'lib/rdio.rb', line 20

def log_methods
  @log_methods
end

.log_postsObject

Returns the value of attribute log_posts.



22
23
24
# File 'lib/rdio.rb', line 22

def log_posts
  @log_posts
end

.log_symbolsObject

Returns the value of attribute log_symbols.



21
22
23
# File 'lib/rdio.rb', line 21

def log_symbols
  @log_symbols
end

.loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/rdio.rb', line 18

def logger
  @logger
end

.symbols_to_typesObject

Override this to declare how certain attributes are constructed. This is done at the end of types.rb.



132
133
134
# File 'lib/rdio/base.rb', line 132

def symbols_to_types
  @symbols_to_types
end

Class Method Details

.add_to_array(arr, str) ⇒ Object

Adds ‘str’ to the array or string ‘arr’



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdio/base.rb', line 64

def add_to_array(arr,str)
  if arr == nil
    return [str.to_s]
  end
  if arr == ''
    return [str.to_s]
  end
  if arr.is_a? Array
    return arr + [str.to_s]
  end
  return arr.to_s + ',' + str.to_s
end

.apiObject

Returns the shared Rdio::Api instance. If it hasn’t been set, we try to construct it from thte environment



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rdio.rb', line 78

def self.api
  return @api if @api
  key = ENV[RDIO_KEY]
  secret = ENV[RDIO_SECRET]
  if key and secret
    @api = init key,secret
  else
    msg = <<HERE
Could not construct an Api instance.  You must first call init(key,secret)
or set the following environmental variables:
- #{RDIO_KEY}
- #{RDIO_SECRET}
HERE
    raise Exception.new msg
  end
  return @api
end

.camel2underscores(s) ⇒ Object

string -> string

Converts camel-case string to underscore-delimited one.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdio/base.rb', line 50

def camel2underscores(s)
  return s if not s
  return s if s == ''
  def decapitilize(s)
    s[0,1].downcase + s[1,s.length-1].to_s
  end
  s = decapitilize s 
  while s.match /([A-Z]+)/
    s = s.gsub /#{$1}/,'_'+ decapitilize($1)
  end
  s
end

.convert_args(args) ⇒ Object

hash -> hash

Uses the value of ‘to_k’ for all the iput hash values in the result hash. This is used to make sure that the arguments passed to create urls use keys for the values of objects like Artist, Track, etc. Also, we use the simple name of classes.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rdio/base.rb', line 32

def convert_args(args)
  return nil if not args
  res = {}
  args.each do |k,v|
    if v.is_a? Array
      v = keys v
    else
      v = v.to_k
    end
    res[k.to_s] = v
  end
  return res
end

.init(key, secret) ⇒ Object

String String -> Api

Initializes and returns the shared Api instance and overwrites the existing one.



58
59
60
# File 'lib/rdio.rb', line 58

def self.init(key,secret)
  @api = Api.new key,secret
end

.init_with_token(token) ⇒ Object

AccessToken => Api

Initializes and returns the shared Api instance from the passed in access_token instance



67
68
69
# File 'lib/rdio.rb', line 67

def self.init_with_token(token)
  @api = Api.from_token token
end

.keys(objs) ⇒ Object

array -> string

Creates a ‘,’-separated string of the value of ‘to_k’ from all the values in ‘objs’. We also remove the nils from the input array.



82
83
84
85
86
87
# File 'lib/rdio/base.rb', line 82

def keys(objs)
  if objs.instance_of? String
    objs = objs.split /,/
  end
  (not objs) ? '' : objs.compact.map {|x| x.to_k}.join(',')
end

.log(str) ⇒ Object



34
35
36
# File 'lib/rdio.rb', line 34

def log(str)
  logger.debug { str }
end

.resetObject

Resets shared API to nil



72
73
74
# File 'lib/rdio.rb', line 72

def self.reset
  @api = nil
end

.to_o(v) ⇒ Object

object -> value

Converts v which is probably a string, to a primitive value, so we can have primitives other than strings as attributes of BaseObjs.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rdio/base.rb', line 94

def to_o(v)
  if v == nil
    return nil
  end
  s = v.to_s
  if not s
    return nil
  end
  if s == 'nil'
    return nil
  end
  if s =~ /^\d+$/
    return s.to_i
  end
  if s =~ /^\d+\.?\d*$/
    return s.to_f
  end
  if s == 'true'
    return true
  end
  if s == 'false'
    return false
  end
  if s =~ /^\[.*\]$/
    s = s.gsub /^\[/,''
    s = s.gsub /\]$/,''
    return s.split(',').map {|x| Rdio::to_o x}
  end
  return s
end

.versionObject



49
50
51
# File 'lib/rdio.rb', line 49

def self.version
  VERSION
end

Instance Method Details

#decapitilize(s) ⇒ Object



53
54
55
# File 'lib/rdio/base.rb', line 53

def decapitilize(s)
  s[0,1].downcase + s[1,s.length-1].to_s
end