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.
-
#port ⇒ Object
Returns the value of attribute port.
-
#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 32 |
# File 'lib/lib-ampache.rb', line 23 def initialize(host, user, psw) uri = URI.parse(host) @host = uri.host @path = uri.path @port = uri.port @user = user @psw = psw @token = nil @token = getAuthToken(user, psw) end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def path @path end |
#playlist ⇒ Object
Returns the value of attribute playlist.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def playlist @playlist end |
#port ⇒ Object
Returns the value of attribute port.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def port @port end |
#psw ⇒ Object
Returns the value of attribute psw.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def psw @psw end |
#token ⇒ Object
Returns the value of attribute token.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def token @token end |
#user ⇒ Object
Returns the value of attribute user.
34 35 36 |
# File 'lib/lib-ampache.rb', line 34 def user @user end |
Instance Method Details
#albums(artist) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/lib-ampache.rb', line 83 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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lib-ampache.rb', line 72 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
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lib-ampache.rb', line 57 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, port) 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lib-ampache.rb', line 37 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/lib-ampache.rb', line 93 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 |