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

Returns a new instance of API.



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

def initialize(network)
  @network = network
end

Instance Attribute Details

#networkObject

Returns the value of attribute network.



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

def network
  @network
end

Instance Method Details

#basic_authObject



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

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

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



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

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

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



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

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

#listen_keyObject



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

def listen_key
  Config.listen_key
end

#logged_in?Boolean

Returns:



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

def logged_in?
  session_key and listen_key
end

#login(username, password) ⇒ Object



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

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



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

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

#session(username, password) ⇒ Object



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

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

#session_keyObject



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

def session_key
  Config.session_key
end