Skip to content

Instantly share code, notes, and snippets.

@Unayung Unayung/service.rb
Created Jul 21, 2017

Embed
What would you like to do?
General Interface for crawler
class LearningPlatformService
include ColorgyCrawler::DateMixin
def initialize(user)
@user = user
account = user.learning_platform_datum.account
password = Base64.decode64(Base64.decode64(user.learning_platform_datum.password))
lp_klass = LearningPlatform.find!(user.organization_code)
@lp_instance = lp_klass.new(account, password)
@lp_data = @user.learning_platform_datum
@imported_list = []
@failed_list = []
@existed_list = []
end
def can_login
@lp_instance.can_login
end
def get_course_list
courses = @lp_instance.get_course_list
end
def get_announcement_list
announcements = @lp_instance.get_announcement_list
end
def get_file_list
files = @lp_instance.get_file_list
end
def get_grade_list
grades = @lp_instance.get_grade_list
end
def new_data_available?
result = []
courses = @user.enroll_courses
courses.each do |course|
matched_course = course.matched_course_datum
user_matched_course_datum = UserMatchedCourseDatum.find_by(user_id: @user.id, matched_course_datum_id: matched_course.id)
if matched_course.updated_at > user_matched_course_datum.updated_at
result << {course_id: course.id, new_data_available: true}
else
result << {course_id: course.id, new_data_available: false}
end
end
result
end
def import_announcement_list
@new_announcement_size = 0
@exist_announcement_size = 0
ActiveRecord::Base.transaction do
announcement_list = get_announcement_list
raise StandardError, 'No announcement data!' if !announcement_list || announcement_list.empty?
announcement_list.each do |announcement|
create_announcement_to_matched_course_announcement(announcement)
end
[1, @new_announcement_size, @exist_announcement_size, ""]
end
rescue Exception => e
[2, @new_announcement_size, @exist_announcement_size, e]
end
def match_course_list
calendar = Calendar.find_by(owner_type: 'Organization', owner_id: @user.organization.id, default: true)
ActiveRecord::Base.transaction do
list = get_course_list
raise StandardError, 'No course data import!' if !list || list.empty?
list.each do |lp_course|
import_list_to_user(lp_course, @user, calendar, @lp_data)
end
if @user.reload.course_not_founds.blank?
[1, @imported_list, @failed_list, @existed_list, ""]
else
[2, @imported_list, @failed_list, @existed_list, ""]
end
end
rescue Exception => e
[3, [], [], [], e]
end
private
def find_db_course_by_data(data, calendar)
sql = if data[:course_lecturer].present?
<<-sql
data -> 'course_year' = ? AND
data -> 'course_term' = ? AND
data -> 'course_lecturer' LIKE ? AND
data -> 'course_code' LIKE ?
sql
else
<<-sql
data -> 'course_year' = ? AND
data -> 'course_term' = ? AND
data -> 'course_code' LIKE ?
sql
end
where_params = if data[:course_lecturer].present?
[sql, current_year.to_s, current_term.to_s, "%#{data[:course_lecturer]}%", "%#{data[:course_code]}%"]
else
[sql, current_year.to_s, current_term.to_s, "%#{data[:course_code]}%"]
end
course = Course.where(calendar_id: calendar.id).where("name LIKE ?", "%#{data[:course_name]}%").where(*where_params).root.last
end
def find_matched_course_by_data(data)
if data[:course_lecturer].present?
matched_course = MatchedCourseDatum.find_by(lp_course_name: data[:course_name], lp_course_code: data[:course_code], lp_course_lecturer: data[:course_lecturer])
else
matched_course = MatchedCourseDatum.find_by(lp_course_name: data[:course_name], lp_course_code: data[:course_code])
end
end
def import_list_to_user(course_data, user, calendar, learning_platform_datum)
course = find_db_course_by_data(course_data, calendar)
if course.present?
if !user.enroll_course_ids.include?(course.id)
user.enroll_courses << course
@imported_list << course_data
m = MatchedCourseDatum.create(course_id: course.id, lp_course_name: course_data[:course_name],
lp_course_code: course_data[:course_code],
lp_course_lecturer: course_data[:course_lecturer],
lp_course_url: course_data[:course_url])
UserMatchedCourseDatum.create(user_id: user.id, matched_course_datum_id: m.id)
else
@existed_list << course_data
end
else
if user.course_not_founds.find_by(course_data).nil?
course_not_found = user.course_not_founds.new(course_data)
course_not_found.learning_platform_datum_id = learning_platform_datum.id
course_not_found.save
@failed_list << course_data
end
end
end
def create_announcement_to_matched_course_announcement(announcement_data)
matched_course = find_matched_course_by_data(announcement_data)
course = matched_course.course
announcement_data[:course_announcement].each do |a|
announcement = course.matched_course_announcements.where(matched_id: matched_course.id, url: a[:url], published_at: a[:published_at]).first_or_initialize
announcement.title = a[:title]
announcement.content = a[:content]
announcement.published_at = a[:published_at]
if announcement.new_record?
matched_course.touch
@new_announcement_size += 1
else
@exist_announcement_size += 1
end
announcement.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.