Class: Freedb::FetchDisk
- Inherits:
-
Object
- Object
- Freedb::FetchDisk
- Defined in:
- lib/freedb.rb
Overview
:nodoc:
Instance Method Summary collapse
- #close ⇒ Object
- #gets ⇒ Object
-
#initialize(basedir, win_format) ⇒ FetchDisk
constructor
A new instance of FetchDisk.
- #send_cmd(cmd, args) ⇒ Object
Constructor Details
#initialize(basedir, win_format) ⇒ FetchDisk
Returns a new instance of FetchDisk.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/freedb.rb', line 511 def initialize(basedir, win_format) raise(FreedbError, "#{basedir} is not a directory") if not File.directory?(basedir) @basedir = File.(basedir) @win = win_format @res = ["201"] # used to store results to avoid rescanning files when getting result # hash key if the name of the category @temp_results = {} # storing full directory and name of differents categories ( each # represented by a directory) @categs = Dir.entries(@basedir).collect do |d| dir = File.join(@basedir, d) if File.directory?(dir) and d !~ /^\.\.?$/ @temp_results[d] = [] [dir, d] end end @categs.compact! end |
Instance Method Details
#close ⇒ Object
611 |
# File 'lib/freedb.rb', line 611 def close; end |
#gets ⇒ Object
607 608 609 |
# File 'lib/freedb.rb', line 607 def gets @res.shift + "\n" end |
#send_cmd(cmd, args) ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/freedb.rb', line 533 def send_cmd(cmd, args) case cmd when "hello" a = args.split(" ") @res << "200 Hello and welcome #{a[0]}@#{a[1]} running #{a[2]} #{a[3]}." when "query" discid = args.split(" ")[0] match = [] #################### #WINDOWS DB FORMAT #################### if @win good_line = "#FILENAME=#{discid}\n" find_files_win(discid).each do |f, categ| content = [] catch(:finish) do started = false File.foreach(f) do |line| if line == good_line started = true elsif started if line =~ /^#FILENAME=/ match << categ + " " + discid + " " + disc_name(content) @temp_results[categ] = content throw(:finish) end content << line end end end end #################### #CLASSIC DB FORMAT #################### else @categs.each do |dir, categ| filename = File.join(dir, discid) #if file exists, we've got a match if File.file?(filename) @temp_results[categ] = File.readlines(filename) match << categ + " " + discid + " " + disc_name(@temp_results[categ]) end end end if match.size == 1 @res << "200 #{match[0]}" elsif match.size > 1 @res << "211 Multiple match found" match.each { |m| @res << m } @res << "." else @res << "202 No match found" end when "read" categ, discid = args.split(" ") @res << "210 #{categ} #{discid}" if @win lines = @temp_results[categ] else filename = File.join(@basedir, categ, discid) lines = File.readlines(filename) end @res.concat(lines.collect { |l| l.chop }) @res << "." when "proto" @res << "200 CDDB protocol level: current #{PROTO_LEVEL}, supported #{PROTO_LEVEL}" else @res << "501" #$stderr.puts "#{self.class} unsupported command #{cmd}" end end |