Class: AmpacheRuby
- Inherits:
-
Object
- Object
- AmpacheRuby
- Defined in:
- lib/lib-ampache.rb
Overview
Class is initialized with Hostname, user and password An auth token is requested on class initialization
To get the artist list from database you can call the method artists(nil) and you’ll get an array of AmpacheArtists.
To get albums from an artist you can use artist_instance.albums or ampache_ruby.instance.albums(artist_instance)
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#playlist ⇒ Object
Returns the value of attribute playlist.
-
#psw ⇒ Object
Returns the value of attribute psw.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #albums(artist) ⇒ Object
-
#artists(name = nil) ⇒ Object
retrive artists lists from database, name is an optional filter.
-
#callApiMethod(method, args = {}) ⇒ Object
generic api method call.
-
#getAuthToken(user, psw) ⇒ Object
tryies to obtain an auth token.
-
#initialize(host, user, psw) ⇒ AmpacheRuby
constructor
A new instance of AmpacheRuby.
- #songs(album) ⇒ Object
Constructor Details
#initialize(host, user, psw) ⇒ AmpacheRuby
Returns a new instance of AmpacheRuby.
23 24 25 26 27 28 29 30 31 |
# File 'lib/lib-ampache.rb', line 23 def initialize(host, user, psw) uri = URI.parse(host) @host = uri.host @path = uri.path @user = user @psw = psw @token = nil @token = getAuthToken(user, psw) end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def path @path end |
#playlist ⇒ Object
Returns the value of attribute playlist.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def playlist @playlist end |
#psw ⇒ Object
Returns the value of attribute psw.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def psw @psw end |
#token ⇒ Object
Returns the value of attribute token.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def token @token end |
#user ⇒ Object
Returns the value of attribute user.
33 34 35 |
# File 'lib/lib-ampache.rb', line 33 def user @user end |
Instance Method Details
#albums(artist) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/lib-ampache.rb', line 82 def albums(artist) albums = [] args = {'filter' => artist.uid.to_s} doc = callApiMethod("artist_albums", args) doc.xpath("//album").each do |a| albums << AmpacheAlbum.new(self, a['id'], a.at("name").content, artist) end return albums end |
#artists(name = nil) ⇒ Object
retrive artists lists from database, name is an optional filter
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lib-ampache.rb', line 71 def artists(name = nil) args = {} args = {'filter' => name.to_s} if name # artist search artists = [] doc = callApiMethod("artists", args) doc.xpath("//artist").each do |a| artists << AmpacheArtist.new(self, a['id'], a.at("name").content) end return artists end |
#callApiMethod(method, args = {}) ⇒ Object
generic api method call
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lib-ampache.rb', line 56 def callApiMethod(method, args={}) begin args['auth'] ||= token if token url = path + "/server/xml.server.php?action=#{method}&#{args.keys.collect { |k| "#{k}=#{args[k]}" }.join('&')}" response = Net::HTTP.get_response(host, url) return Nokogiri::XML(response.body) rescue Errno::ECONNREFUSED => e warn "Ampache closed with the following error" warn e. exit end end |
#getAuthToken(user, psw) ⇒ Object
tryies to obtain an auth token
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lib-ampache.rb', line 36 def getAuthToken(user, psw) begin action= "handshake" # auth string key = Digest::SHA2.new << psw time = Time.now.to_i.to_s psk = Digest::SHA2.new << (time + key.to_s) args = {'auth' => psk, 'timestamp'=> time, 'version' => '350001', 'user' => user} doc = callApiMethod(action, args); return doc.at("auth").content rescue Exception => e warn "" warn "token not valid or expired, check your username and password" warn "" end end |
#songs(album) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/lib-ampache.rb', line 92 def songs(album) songs = [] args = {'filter' => album.uid.to_s} doc = callApiMethod("album_songs", args) doc.xpath("//song").each do |s| songs << AmpacheSong.new(self, s['id'], s.at("title").content, album.artist, album, s.at("url").content) end return songs end |