tiss2go/app/controllers/crawlers/courses_crawler_controller.rb
Tobias Eidelpes dabda63cbc Remove URL parsing
The fields `courseNr` and `semesterCode` are already parsed by the
controller and do not have to be parsed again when calling the
add_to_fav function.
2020-05-23 15:44:39 +02:00

63 lines
1.9 KiB
Ruby

class Crawlers::CoursesCrawlerController < Crawlers::TissCrawlerController
def show_basic
params[:api] = '/api/search/course/v1.0/quickSearch'
params[:search_parameter] = 'searchterm'
puts params[:search_context]
@host = TissCrawler.get_host
# TissCrawler performs general search over the available courses
@courses = TissCrawler.search(params)
end
def show_detail
params[:api] = '/api/course/'
regex = params[:detail_url].match(/courseNr=(?<number>\S{6})/)
params[:number] = regex[:number]
regex = params[:detail_url].match(/semester=(?<semester>\d{4}[SW])/)
params[:semester] = regex[:semester]
puts params
# TissCrawler fetches the course's detail information
@course = TissCrawler.get_course_details(params)
puts @course
# Get all the org ids into one array
@lecturers_oid = @course['lecturers']['oid']
@lecturers_names = []
if @lecturers_oid != nil
@lecturers_oid.each do |item|
# For each org id get the associated name
puts TissCrawler.get_oid_name(item)
@lecturers_names << (TissCrawler.get_oid_name(item))
end
end
# Host is needed for image rendering
@host = TissCrawler.get_host
end
def add_to_fav
params[:api] = '/api/course/'
@course = TissCrawler.get_course_details(params)
# create stores the object to the db after creation
favorite_hash = {number: @course['courseNumber'],
semester: @course['semesterCode'],
title: @course['title']['de'],
# the user who is currently active
user_id: current_user.id}
if FavoriteCourse.create(favorite_hash).valid?
FavoriteCourse.create(favorite_hash)
flash[:alert] = 'Course added to your favorites!'
else
flash[:alert] = 'Course is already favorited!'
end
redirect_back(fallback_location: search)
end
end