Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ingestors/ingestor_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def self.taxila_ingestors
Ingestors::Taxila::DccIngestor,
Ingestors::Taxila::SenseIngestor,
Ingestors::Taxila::VuMaterialIngestor,
Ingestors::Taxila::HanIngestor,
]
end

Expand Down
45 changes: 45 additions & 0 deletions lib/ingestors/taxila/han_ingestor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'open-uri'
require 'csv'
require 'nokogiri'

module Ingestors
module Taxila
class HanIngestor < Ingestor
def self.config
{
key: 'han_material',
title: 'HAN Materials API',
category: :materials
}
end

def read(url)
begin
process_han(url)
rescue Exception => e
@messages << "#{self.class.name} failed with: #{e.message}"
end

# finished
nil
end

private

def process_han(_url)
url = 'https://www.han.nl/studeren/scholing-voor-werkenden/laboratorium/'

material_page = Nokogiri::HTML5.parse(open_url(url.to_s, raise: true)).css('#content > .section--cards-skinny > .section-wrapper > .section__content > .cards-skinny > .cards-skinny-wrapper > .cards-skinny__item > .card-skinny > .card-skinny__content')
material_page.each_with_index do |el, _idx|
material = OpenStruct.new
material.title = el.css('.card-skinny__content__title').first.text
material.url = "https://www.han.nl#{el.css('.card-skinny__content__buttons > .buttons > .buttons__button > a').first.get_attribute('href')}"
material.description = el.css('.card-skinny__content__body').first.text
add_material(material)
rescue Exception => e
@messages << "Extract event fields failed with: #{e.message}"
end
end
end
end
end
49 changes: 49 additions & 0 deletions test/unit/ingestors/taxila/han_ingestor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class HanIngestorTest < ActiveSupport::TestCase
setup do
@user = users(:regular_user)
@content_provider = content_providers(:portal_provider)
mock_ingestions
mock_timezone # System time zone should not affect test result
end

teardown do
reset_timezone
end

test 'can ingest materials from han' do
source = @content_provider.sources.build(
url: 'https://www.han.nl/studeren/scholing-voor-werkenden/laboratorium/',
method: 'han',
enabled: true
)

ingestor = Ingestors::Taxila::HanIngestor.new

# check materials don't exist
new_title = 'Synthetiseren en Karakteriseren van Moleculen'
new_url = 'https://www.han.nl/opleidingen/module/synthetiseren-karakteriseren-moleculen/'
refute Material.where(title: new_title, url: new_url).any?

# run task
assert_difference('Material.count', 27) do
freeze_time(2019) do
VCR.use_cassette('ingestors/han') do
ingestor.read(source.url)
ingestor.write(@user, @content_provider)
end
end
end

# check event does exist
material = Material.where(title: new_title, url: new_url).first
assert material
assert_equal new_title, material.title
assert_equal new_url, material.url

# check other fields
# assert_equal 'Software Carpentry lessons introduce basic lab skills for research computing. They cover three core topics: the Unix shell, version control with Git, and a programming language (Python or R). ',
# material.description
end
end
52 changes: 52 additions & 0 deletions test/vcr_cassettes/ingestors/han.yml

Large diffs are not rendered by default.

Loading