Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.85 KB | None | 0 0
  1. def courses
  2.       courses = get_xml(@courses_uri).xpath('//COURSE')
  3.       courses.map do |xml|
  4.         puts xml
  5.         course = xml.to_h.map do |k, v|
  6.           case k
  7.           when :credmin then [:min_credits, v]
  8.           when :credmax then [:max_credits, v]
  9.           when :num     then [:number, v]
  10.           when :name    then [:name, v.titleize]
  11.           when :dept    then [:department, { code: v }]
  12.           else nil
  13.           end
  14.         end
  15.         #course.to_h.compact!
  16.         #course[:sections] = extract_sections xml
  17.         #course
  18.       end
  19.     end
  20.  
  21.     private
  22.  
  23.     def get_xml uri
  24.       Nokogiri::XML(@http_client.get(uri).body)
  25.     end
  26.  
  27.     def extract_sections course
  28.       course.xpath('SECTION').map do |xml|
  29.         section = xml.to_h.map do |k, v|
  30.           case k
  31.           when :num       then [:name, v]
  32.           when :crn       then [:crn, v]
  33.           when :seats     then [:seats, v]
  34.           when :students  then [:seats_taken, v]
  35.           else nil
  36.           end
  37.         end
  38.         course.to_h.compact!
  39.         #section[:instructors]   = []
  40.         #section[:periods_day]   = []
  41.         #section[:periods_start] = []
  42.         #section[:periods_end]   = []
  43.         #section[:periods_type]  = []
  44.         xml.xpath('PERIOD').each do |pxml|
  45.           section[:instructors].concat(pxml[:instructor].strip.split(/\//))
  46.           pxml.xpath('DAY').each do |dxml|
  47.             dxml.each do |day|
  48.               section[:periods_day]   << day_xml.text.to_i + 1
  49.               section[:periods_start] << pxml[:start]
  50.               section[:periods_end]   << pxml[:end]
  51.               section[:periods_type]  << pxml[:type]
  52.             end
  53.           end
  54.         end
  55.         section[:num_periods] = section[:periods_day].count
  56.         section[:instructors].uniq!.delete 'Staff'
  57.         section
  58.       end
  59.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement