Class: AudioAddict::API

Inherits:
Object
  • Object
show all
Includes:
Cache, HTTParty
Defined in:
lib/audio_addict/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cache

#cache, #cache_dir, #cache_dir!, #cache_life, #cache_life!

Constructor Details

#initialize(network) ⇒ API



13
14
15
# File 'lib/audio_addict/api.rb', line 13

def initialize(network)
  @network = network
end

Instance Attribute Details

#networkObject

Returns the value of attribute network.



11
12
13
# File 'lib/audio_addict/api.rb', line 11

def network
  @network
end

Instance Method Details

#basic_authObject



42
43
44
# File 'lib/audio_addict/api.rb', line 42

def basic_auth
  http.basic_auth 'streams', 'diradio'
end

#delete(path, args = {}) ⇒ Object



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

def delete(path, args = {})
  response http.delete "/#{network}/#{path}", headers: headers, body: args
end

#get(path, args = {}) ⇒ Object



26
27
28
# File 'lib/audio_addict/api.rb', line 26

def get(path, args = {})
  response http.get "/#{network}/#{path}", headers: headers, body: args
end

#listen_keyObject



56
57
58
# File 'lib/audio_addict/api.rb', line 56

def listen_key
  Config.listen_key
end

#logged_in?Boolean



38
39
40
# File 'lib/audio_addict/api.rb', line 38

def logged_in?
  session_key and listen_key
end

#login(username, password) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/audio_addict/api.rb', line 17

def (username, password)
  session = session(username, password)
  Config.session_key = session['key']
  Config.listen_key = session['member']['listen_key']
  Config.email = session['member']['email']
  Config.premium = session['member']['user_type'] == 'premium'
  Config.save
end

#post(path, args = {}) ⇒ Object



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

def post(path, args = {})
  response http.post "/#{network}/#{path}", headers: headers, body: args
end

#session(username, password) ⇒ Object



46
47
48
49
50
# File 'lib/audio_addict/api.rb', line 46

def session(username, password)
  params = { member_session: { username: username, password: password } }
  basic_auth
  response http.post "/#{network || 'di'}/member_sessions", body: params
end

#session_keyObject



52
53
54
# File 'lib/audio_addict/api.rb', line 52

def session_key
  Config.session_key
end