Class: Net::DAAP::Client
- Inherits:
-
Object
- Object
- Net::DAAP::Client
- Defined in:
- lib/net/daap.rb
Overview
Synopsis
The Client class interacts with the iTunes server to fetch lists of songs, databases, and playlists. Each Client class can have many Database, and each Database can have many Playlist, and many Song. See DAAP for a code example.
Instance Attribute Summary collapse
-
#dmap ⇒ Object
readonly
Returns the value of attribute dmap.
-
#log ⇒ Object
Returns the value of attribute log.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to the iTunes server.
-
#connect_db(&block) ⇒ Object
Connect to the server and yield each database to the caller, then automatically disconnect from the server.
-
#databases ⇒ Object
Returns the databases found on the iTunes server.
-
#disconnect ⇒ Object
Disconnects from the DAAP server.
- #do_get(request, &block) ⇒ Object
- #get_song(request, &block) ⇒ Object
-
#initialize(server_host, parameters = {}) {|_self| ... } ⇒ Client
constructor
Create a new Client and pass in the host where the client will connect.
- #unpack_listing(listing, &func) ⇒ Object
Constructor Details
#initialize(server_host, parameters = {}) {|_self| ... } ⇒ Client
Create a new Client and pass in the host where the client will connect.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/net/daap.rb', line 58 def initialize(server_host, parameters = {}) params = { :port => 3689, :password => nil }.merge(parameters) @server_host = server_host @server_port = params[:port] @password = params[:password] @validator = nil @log = Logger.new(nil) @session_id = nil @request_id = nil @connected = nil yield self if block_given? end |
Instance Attribute Details
#dmap ⇒ Object (readonly)
Returns the value of attribute dmap.
55 56 57 |
# File 'lib/net/daap.rb', line 55 def dmap @dmap end |
#log ⇒ Object
Returns the value of attribute log.
54 55 56 |
# File 'lib/net/daap.rb', line 54 def log @log end |
Instance Method Details
#connect ⇒ Object
Connects to the iTunes server. This method should be called right after construction. See DAAP for an example.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/net/daap.rb', line 73 def connect @log.info("Connecting to #{@server_host}:#{@server_port}") @http_client = Net::HTTP.start(@server_host, @server_port) find_validator @dmap = Net::DAAP::DMAP.new(:daap => self) load_server_info @log.info("Now connected") @connected = 1 if block_given? yield @dsn disconnect @connected = 0 end @dsn end |
#connect_db(&block) ⇒ Object
Connect to the server and yield each database to the caller, then automatically disconnect from the server.
91 92 93 94 95 96 |
# File 'lib/net/daap.rb', line 91 def connect_db(&block) raise ArgumentError if block.nil? connect databases.each { |db| block.call(db) } disconnect end |
#databases ⇒ Object
Returns the databases found on the iTunes server.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/net/daap.rb', line 99 def databases unless @connected errstr = "Not connected, can't fetch databases" @log.error(errstr) raise errstr end listings = @dmap.find(do_get("databases"), "daap.serverdatabases/dmap.listing") # FIXME check the value of listing @databases = [] unpack_listing(listings) do |value| db = Database.new( value.merge(:daap => self) ) if block_given? yield db else @databases << db end end @databases end |
#disconnect ⇒ Object
Disconnects from the DAAP server
174 175 176 177 |
# File 'lib/net/daap.rb', line 174 def disconnect @log.info("Disconnecting") do_get("logout") end |
#do_get(request, &block) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/net/daap.rb', line 121 def do_get(request, &block) @log.debug("do_get called") url = String.new('/' + request) if @session_id url += url =~ /\?/ ? "&" : "?" url += "session-id=#{@session_id}" end #if @revision && request != "logout" # url += "&revision-number=#{@revision}" #end @log.debug("Fetching url: #{url}") req = Net::HTTP::Get.new(url, request_headers(url)) req.basic_auth('iTunes_4.6', @password) if ! @password.nil? res = @http_client.request(req) do |response| response.read_body(&block) end case res when Net::HTTPSuccess else @log.error("This DAAP Server requires a password") res.error! end @log.debug("Done Fetching url: #{url}") content_type = res.header['content-type'] if request !~ /(?:\/items\/\d+\.|logout)/ && content_type !~ /dmap/ raise "Broken response" end res.body end |
#get_song(request, &block) ⇒ Object
157 158 159 160 161 |
# File 'lib/net/daap.rb', line 157 def get_song(request, &block) @log.debug("Downloading a song") @request_id = @request_id.nil? ? 2 : @request_id + 1 do_get(request, &block) end |
#unpack_listing(listing, &func) ⇒ Object
163 164 165 166 167 168 169 170 171 |
# File 'lib/net/daap.rb', line 163 def unpack_listing(listing, &func) listing.each do |item| record = Hash.new item[1].each do |pair_ref| record[pair_ref[0]] = pair_ref[1] end yield record end end |