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.
-
#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) {|_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) {|_self| ... } ⇒ Client
Create a new Client and pass in the host where the client will connect.
57 58 59 60 61 62 63 64 65 |
# File 'lib/net/daap.rb', line 57 def initialize(server_host) @server_host = server_host @server_port = 3689 @validator = nil @log = Logger.new(nil) @session_id = nil @request_id = nil yield self if block_given? end |
Instance Attribute Details
#dmap ⇒ Object (readonly)
Returns the value of attribute dmap.
54 55 56 |
# File 'lib/net/daap.rb', line 54 def dmap @dmap end |
#log ⇒ Object
Returns the value of attribute log.
53 54 55 |
# File 'lib/net/daap.rb', line 53 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/net/daap.rb', line 69 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 |
#databases ⇒ Object
Returns the databases found on the iTunes server.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/net/daap.rb', line 86 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
149 150 151 152 |
# File 'lib/net/daap.rb', line 149 def disconnect @log.info("Disconnecting") do_get("logout") end |
#do_get(request, &block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/net/daap.rb', line 108 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}") res = @http_client.get(url, request_headers(url), nil, &block) @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
132 133 134 135 136 |
# File 'lib/net/daap.rb', line 132 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
138 139 140 141 142 143 144 145 146 |
# File 'lib/net/daap.rb', line 138 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 |