Class: Clasrip::SQL

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

Defined Under Namespace

Classes: Classification, PEGIRating

Instance Method Summary collapse

Constructor Details

#initialize(sql_url) ⇒ SQL

Returns a new instance of SQL.



51
52
53
54
55
56
57
# File 'lib/clasrip/sql.rb', line 51

def initialize(sql_url)
  sql_url = sql_url.sub("///", "//#{Dir.pwd}/")
  #DataMapper::Logger.new($stdout, :debug)
  DataMapper.setup(:default, sql_url)
  Classification.auto_upgrade!
  PEGIRating.auto_upgrade!
end

Instance Method Details

#add_record(record, type = :classification) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/clasrip/sql.rb', line 59

def add_record(record, type=:classification)
  if type == :classification
    Classification.create(record)
  elsif type == :pegi
    PEGIRating.create(record)
  else
    raise "type not supported"
  end
end

#parse_xml(xml_fn) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/clasrip/sql.rb', line 69

def parse_xml(xml_fn)
  print "Parsing XML... "
  xml = Nokogiri::XML(xml_fn, &:noblanks)
  puts "Done."

  classifications = xml.css("classifications > classification")
  count = 0
  record = Classification.last
  puts "Null record" if record == nil
  wait = (record == nil) ? false : true
  
  puts "Record: #{record.attributes[:title]}" unless record == nil
  print "Finding position... " unless record == nil
  classifications = classifications.drop_while do |i|
    if wait == true
      res = record.attributes[:classification_number] == i.at_css("classification-number").content
      wait = false if res == true
      if wait == false
        puts "Done!"
        return true
      end
    end
    wait
  end
  clen = classifications.length

  classifications.each do |node|
    count += 1
    c = {}
    node.children.each do |child|
      next if child.name == "text"
      name = child.name.gsub("-", "_").to_sym
      if name == :date_of_classification
        date = Date.strptime(child.content, "%Y-%m-%d")
        c[name] = date
      else
        c[name] = child.content
      end
    end
    cls = Classification.create(c)
    print "\r#{count}/#{clen}"
  end
  puts "\nDone!"
end

#to_xml(table) ⇒ Object



114
115
116
# File 'lib/clasrip/sql.rb', line 114

def to_xml(table)
  #stub
end