Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def courses
- courses = get_xml(@courses_uri).xpath('//COURSE')
- courses.map do |xml|
- puts xml
- course = xml.to_h.map do |k, v|
- case k
- when :credmin then [:min_credits, v]
- when :credmax then [:max_credits, v]
- when :num then [:number, v]
- when :name then [:name, v.titleize]
- when :dept then [:department, { code: v }]
- else nil
- end
- end
- #course.to_h.compact!
- #course[:sections] = extract_sections xml
- #course
- end
- end
- private
- def get_xml uri
- Nokogiri::XML(@http_client.get(uri).body)
- end
- def extract_sections course
- course.xpath('SECTION').map do |xml|
- section = xml.to_h.map do |k, v|
- case k
- when :num then [:name, v]
- when :crn then [:crn, v]
- when :seats then [:seats, v]
- when :students then [:seats_taken, v]
- else nil
- end
- end
- course.to_h.compact!
- #section[:instructors] = []
- #section[:periods_day] = []
- #section[:periods_start] = []
- #section[:periods_end] = []
- #section[:periods_type] = []
- xml.xpath('PERIOD').each do |pxml|
- section[:instructors].concat(pxml[:instructor].strip.split(/\//))
- pxml.xpath('DAY').each do |dxml|
- dxml.each do |day|
- section[:periods_day] << day_xml.text.to_i + 1
- section[:periods_start] << pxml[:start]
- section[:periods_end] << pxml[:end]
- section[:periods_type] << pxml[:type]
- end
- end
- end
- section[:num_periods] = section[:periods_day].count
- section[:instructors].uniq!.delete 'Staff'
- section
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement