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 = nil @session_id = nil @request_id = nil @connected = false 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 |
# File 'lib/net/daap.rb', line 73 def connect log.info("Connecting to #{@server_host}:#{@server_port}") if log @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") if log @connected = true if block_given? yield @dsn disconnect end @dsn end |
#connect_db(&block) ⇒ Object
Connect to the server and yield each database to the caller, then automatically disconnect from the server.
90 91 92 93 94 95 96 97 98 |
# File 'lib/net/daap.rb', line 90 def connect_db(&block) raise ArgumentError if block.nil? connect begin databases.each { |db| block.call(db) } ensure disconnect end end |
#databases ⇒ Object
Returns the databases found on the iTunes server.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/net/daap.rb', line 101 def databases unless @connected errstr = "Not connected, can't fetch databases" log.error(errstr) if log 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
176 177 178 179 180 |
# File 'lib/net/daap.rb', line 176 def disconnect log.info("Disconnecting") if log do_get("logout") @connected = false end |
#do_get(request, &block) ⇒ Object
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 156 157 |
# File 'lib/net/daap.rb', line 123 def do_get(request, &block) log.debug("do_get called") if log 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}") if log 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") if log res.error! end log.debug("Done Fetching url: #{url}") if log 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
159 160 161 162 163 |
# File 'lib/net/daap.rb', line 159 def get_song(request, &block) log.debug("Downloading a song") if log @request_id = @request_id.nil? ? 2 : @request_id + 1 do_get(request, &block) end |
#unpack_listing(listing, &func) ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/net/daap.rb', line 165 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 |