lib/lib-ampache.rb in ruby-ampache-0.0.6 vs lib/lib-ampache.rb in ruby-ampache-0.0.7
- old
+ new
@@ -3,10 +3,21 @@
require 'open-uri'
require 'digest/sha2'
require File.join(File.dirname(__FILE__),'lib-ampache')
+=begin
+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 issue
+artist_instance.albums or ampache_ruby.instance.albums(artist_instance)
+=end
class AmpacheRuby
def initialize(host,user,psw)
uri = URI.parse(host)
@@ -17,11 +28,12 @@
@token = nil
@token = getAuthToken(user,psw)
end
attr_accessor :host, :path, :user, :psw, :token, :playlist
-
+
+ # tryies to obtain an auth token
def getAuthToken(user,psw)
action= "handshake"
# auth string
key = Digest::SHA2.new << psw
time = Time.now.to_i.to_s
@@ -30,18 +42,20 @@
args = { 'auth' => psk, 'timestamp'=> time, 'version' => '350001', 'user' => user}
doc = callApiMethod(action,args);
return doc.at("auth").content
end
-
+
+ # generic api method call
def callApiMethod( method, args={})
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)
end
-
+ # retrive artists lists from database,
+ # name is an optional filter
def artists(name = nil)
args = {}
args = { 'filter' => name.to_s } if name # artist search
artists = []
doc = callApiMethod("artists",args)