Class: RibitData::CategoryParser

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

Constant Summary collapse

CATEGORIES_XML_FILE =
'categories.xml'
CATEGORY_XML_FILE =
'category.xml'

Instance Method Summary collapse

Constructor Details

#initialize(contentDirectory) ⇒ CategoryParser

Returns a new instance of CategoryParser.



659
660
661
# File 'lib/ribit/ribitdata.rb', line 659

def initialize( contentDirectory )
  @contentDir = contentDirectory
end

Instance Method Details

#parse_categoriesObject



664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/ribit/ribitdata.rb', line 664

def parse_categories
  categoriesXML = File.new( File.join( @contentDir, CATEGORIES_XML_FILE ) )
  categories = Array.new
  begin
    doc = REXML::Document.new( categoriesXML )
    doc.elements.each( 'categories/category' ) do |topLevelCategory|
      categories.push( process_category_ele( topLevelCategory ) )
    end
    
    return categories
  ensure
    categoriesXML.close
  end
end

#parse_category(categoryID) ⇒ Object



698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/ribit/ribitdata.rb', line 698

def parse_category( categoryID )
  categoryDir = File.join( @contentDir, categoryID )
  
  # first read category.xml
  categoryXML = File.new( File.join( categoryDir, CATEGORY_XML_FILE ) )
  
  begin    
    return parse_category_doc( categoryID, categoryXML )
  ensure
    categoryXML.close
  end
end

#parse_category_doc(categoryID, categoryXML) ⇒ Object



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/ribit/ribitdata.rb', line 712

def parse_category_doc( categoryID, categoryXML )
    doc = REXML::Document.new( categoryXML )
  shortName = categoryID
  longName = categoryID
  
  doc.elements.each( 'category/short-name' ) do |ele|
    shortName = ele.text  
  end
  
  doc.elements.each( 'category/long-name' ) do |ele|
    longName = ele.text
  end
  
  mainPageID = nil
  doc.elements.each( 'category/main-page-id' ) do |ele|
    mainPageID = ele.text
  end
  
  category = Category.new( categoryID, shortName, longName )
  category.set_main_page_id( mainPageID )
  
  properties = Hash.new
  doc.elements.each("category/properties/property" ) do |property|
      name = property.attributes["name"]
      value = property.attributes["value"]
      category.set_property( name, value )
  end
    
  return category 
end

#process_category_ele(categoryElement) ⇒ Object



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/ribit/ribitdata.rb', line 680

def process_category_ele( categoryElement )
  #puts '########## ' + categoryElement.to_s
  catID = categoryElement.attributes['id']
  #puts '########## ID = ' + catID
  category = parse_category( catID )
  
  # is there subcategories ?
  if ( categoryElement.elements['category'] != nil )
    categoryElement.get_elements( 'category' ).each do |subCategoryEle|
      subCategory = process_category_ele( subCategoryEle )
      subCategory.set_parent_category( category )
    end
  end
  
  return category
end