Class: CoalescingPanda::Workers::ProvisioningMiner
- Inherits:
-
Object
- Object
- CoalescingPanda::Workers::ProvisioningMiner
- Defined in:
- app/models/coalescing_panda/workers/provisioning_miner.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#course_ids ⇒ Object
Returns the value of attribute course_ids.
-
#enrollment_ids ⇒ Object
Returns the value of attribute enrollment_ids.
-
#section_ids ⇒ Object
Returns the value of attribute section_ids.
-
#user_ids ⇒ Object
Returns the value of attribute user_ids.
Instance Method Summary collapse
- #client ⇒ Object
- #delete_records ⇒ Object
- #download_report(report_name, report_params) ⇒ Object
-
#initialize(account_id) ⇒ ProvisioningMiner
constructor
A new instance of ProvisioningMiner.
- #perform ⇒ Object
- #process_csv(csv) ⇒ Object
- #process_zipped_csv_file(zip, file) ⇒ Object
Constructor Details
#initialize(account_id) ⇒ ProvisioningMiner
Returns a new instance of ProvisioningMiner.
8 9 10 11 12 13 14 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 8 def initialize(account_id) @account = Account.find(account_id) @user_ids = []; @course_ids = []; @section_ids = []; @enrollment_ids = []; end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
6 7 8 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6 def account @account end |
#course_ids ⇒ Object
Returns the value of attribute course_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6 def course_ids @course_ids end |
#enrollment_ids ⇒ Object
Returns the value of attribute enrollment_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6 def enrollment_ids @enrollment_ids end |
#section_ids ⇒ Object
Returns the value of attribute section_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6 def section_ids @section_ids end |
#user_ids ⇒ Object
Returns the value of attribute user_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6 def user_ids @user_ids end |
Instance Method Details
#client ⇒ Object
16 17 18 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 16 def client Bearcat::Client.new(prefix: account.api_domain, token: account.api_token) end |
#delete_records ⇒ Object
47 48 49 50 51 52 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 47 def delete_records User.where.not(id: user_ids).destroy_all Course.where.not(id: course_ids).destroy_all Section.where.not(id: section_ids).destroy_all Enrollment.where.not(id: enrollment_ids).destroy_all end |
#download_report(report_name, report_params) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 70 def download_report(report_name, report_params) report = client.start_report(account.canvas_account_id, report_name, report_params) report_id = report['id'] loop do sleep 20 report = client.report_status(account.canvas_account_id, report_name, report_id) break if report['status'] == 'complete' || report['status'] == 'error' end url = "#{account.api_domain}/files#{report['attachment']['url'].split('files').last}" end |
#perform ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 20 def perform Delayed::Worker.logger.debug "Downloading Provisioning Report" params = { 'parameters[enrollments]' => true, 'parameters[users]' => true, 'parameters[courses]' => true, 'parameters[sections]' => true, 'parameters[xlist]' => true, 'parameters[include_deleted]' => true } url = download_report(:provisioning_csv, params) open(url, :allow_redirections => :safe) do |file| begin Delayed::Worker.logger.debug "Processing Provisioning Report" {users: User, courses: Course, sections: Section, enrollments: Enrollment, xlist: Section}.each do |k, v| process_zipped_csv_file(file, "#{k}.csv") do |row| begin id = v.create_from_csv(account, row) self.instance_variable_get("@#{v.to_s.downcase}_ids") << id if id.present? rescue => exception Delayed::Worker.logger.debug exception end end end ensure file.close unless file.nil? end end end |
#process_csv(csv) ⇒ Object
65 66 67 68 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 65 def process_csv(csv) = {:headers => true, :return_headers => false, :header_converters => :symbol} CSV.parse(csv, ) { |row| yield(row) } end |
#process_zipped_csv_file(zip, file) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 54 def process_zipped_csv_file(zip, file) Zip::InputStream.open(zip) do |io| while entry = io.get_next_entry if entry.name == file #io.read process_csv(io.read) { |row| yield(row) } break end end end end |