Class: Metalink4

Inherits:
Object
  • Object
show all
Defined in:
lib/metalink4.rb

Overview

Describes the base Metalink 4 file as specified by rfc5854 If served, it should have a header of application/metalink4+xml en.wikipedia.org/wiki/Metalink

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Metalink4

Options: files, published, updated, origin, origin_dynamic



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/metalink4.rb', line 448

def initialize(opts = {})
  opts = opts.inject({}){ |r, (k,v)| r[k.to_sym] = v; r }

  self.files = []
  
  opts.fetch(:files, []).each do |file|
    self.files << Metalink4File.new(file)
  end

  self.published = opts.fetch(:published, nil)
  self.updated = opts.fetch(:updated, nil)
  self.origin = opts.fetch(:origin, nil)
  self.origin_dynamic = opts.fetch(:origin_dynamic, false)

  self.xml = Builder::XmlMarkup.new(indent: 2)
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



439
440
441
# File 'lib/metalink4.rb', line 439

def files
  @files
end

#originObject

Returns the value of attribute origin.



439
440
441
# File 'lib/metalink4.rb', line 439

def origin
  @origin
end

#origin_dynamicObject

Returns the value of attribute origin_dynamic.



439
440
441
# File 'lib/metalink4.rb', line 439

def origin_dynamic
  @origin_dynamic
end

#publishedObject

Returns the value of attribute published.



439
440
441
# File 'lib/metalink4.rb', line 439

def published
  @published
end

#updatedObject

Returns the value of attribute updated.



439
440
441
# File 'lib/metalink4.rb', line 439

def updated
  @updated
end

#xmlObject

Returns the value of attribute xml.



439
440
441
# File 'lib/metalink4.rb', line 439

def xml
  @xml
end

Class Method Details

.read(potential_file_path) ⇒ Object

Read a .meta4 to internal model. Takes either a file path or XML sting.

Will import any provided hashes, so nothing needs to be calculated.



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
606
# File 'lib/metalink4.rb', line 538

def self.read(potential_file_path)

  begin
    if File.exist?(potential_file_path)
      doc = File.open(potential_file_path) { |f| Nokogiri::XML(f) }
    elsif potential_file_path.is_a?(String)
      doc = Nokogiri::XML(potential_file_path)
    end
  rescue
    raise "%s Not an XML File" % potential_file_path
  end

  ret = Metalink4.new
  ret.published = doc.at("metalink > published").content 
  ret.updated = doc.at("metalink > updated").content rescue nil
  ret.origin = doc.at("metalink > origin").content rescue nil
  ret.origin_dynamic = doc.at("metalink > origin[dynamic]").content == "true" rescue false

  doc.search("metalink > file").each do |file|
  
    ret.files << Metalink4File.new

    ret.files.last.local_path = file.attr("name")

    ret.files.last.copyright = file.at("copyright").content rescue nil
    ret.files.last.description = file.at("description").content rescue nil
    ret.files.last.identity = file.at("identity").content rescue nil
    ret.files.last.language = file.at("language").content rescue nil
    ret.files.last. = file.at("logo").content rescue nil
    ret.files.last.os = file.at("os").content rescue nil
    ret.files.last.publisher_name = file.at("publisher[name]").content rescue nil
    ret.files.last.publisher_url = file.at("publisher[url]").content rescue nil
    ret.files.last.signature = file.at("signature").content rescue nil
    ret.files.last.version = file.at("version").content rescue nil
    
    (file.search("hash") - file.search("pieces > hash")).each do |hash|
      ret.files.last.hashes << Metalink4FileHash.new
      ret.files.last.hashes.last.hash_value = hash.content rescue nil
      ret.files.last.hashes.last.hash_type = hash.attr("type") rescue nil
    end

    ret.files.last.piece_size = file.at("pieces").attr("length").to_i rescue nil
    piece_type = file.at("pieces").attr("type") rescue nil


    ret.files.last.hashes  ||= []
    file.search("pieces > hash").each_with_index do |hash, hash_piece_index|
      ret.files.last.hashes << Metalink4FileHash.new
      ret.files.last.hashes.last.hash_value = (hash.content rescue nil)
      ret.files.last.hashes.last.hash_type = piece_type
      ret.files.last.hashes.last.piece = hash_piece_index
    end
    
    file.search("url").each do |url|
      ret.files.last.urls << Metalink4FileUrl.new
      ret.files.last.urls.last.url = url.content rescue nil
      ret.files.last.urls.last.location = url.attr("location") rescue nil
      ret.files.last.urls.last.priority = url.attr("priority") rescue nil
    end
    
    file.search("metaurl").each do |metaurl|
      ret.files.last.urls << Metalink4FileUrl.new
      ret.files.last.urls.last.url = metaurl.content rescue nil
      ret.files.last.urls.last.priority = metaurl.attr("priority") rescue nil
    end
  end
  
  ret
end

Instance Method Details

#renderObject

Render to XML, returns string.

Checksums are calculated at this point. ONLY sha-256 is calculated.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/metalink4.rb', line 514

def render

  raise "files not specified" if self.files.empty?

  self.xml.instruct! :xml, version: "1.0", encoding: "UTF-8"
  self.xml.metalink( xmlns: "urn:ietf:params:xml:ns:metalink" ) do |metalink|
  
    metalink.generator("Sudrien/metalink-ruby 0.2.0") #may
    metalink.origin( self.origin, dynamic: self.origin_dynamic ) if self.origin
    
    metalink.published( self.published.strftime('%FT%T%:z') ) if self.published
    metalink.updated( self.updated.strftime('%FT%T%:z') ) if self.updated
  
    self.files.each do |file|
      file.render(self, metalink)
    end
  end
  self.xml.target!
end