Skip to content

Commit 344c18d

Browse files
authored
Merge pull request #1183 from DaanVanVugt/feature/han_ingestor
han material ingestor
2 parents 2914c05 + 84fce7b commit 344c18d

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

lib/ingestors/ingestor_factory.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def self.taxila_ingestors
3636
Ingestors::Taxila::DccIngestor,
3737
Ingestors::Taxila::SenseIngestor,
3838
Ingestors::Taxila::VuMaterialIngestor,
39+
Ingestors::Taxila::HanIngestor,
3940
]
4041
end
4142

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'open-uri'
2+
require 'csv'
3+
require 'nokogiri'
4+
5+
module Ingestors
6+
module Taxila
7+
class HanIngestor < Ingestor
8+
def self.config
9+
{
10+
key: 'han_material',
11+
title: 'HAN Materials API',
12+
category: :materials
13+
}
14+
end
15+
16+
def read(url)
17+
begin
18+
process_han(url)
19+
rescue Exception => e
20+
@messages << "#{self.class.name} failed with: #{e.message}"
21+
end
22+
23+
# finished
24+
nil
25+
end
26+
27+
private
28+
29+
def process_han(_url)
30+
url = 'https://www.han.nl/studeren/scholing-voor-werkenden/laboratorium/'
31+
32+
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')
33+
material_page.each_with_index do |el, _idx|
34+
material = OpenStruct.new
35+
material.title = el.css('.card-skinny__content__title').first.text
36+
material.url = "https://www.han.nl#{el.css('.card-skinny__content__buttons > .buttons > .buttons__button > a').first.get_attribute('href')}"
37+
material.description = el.css('.card-skinny__content__body').first.text
38+
add_material(material)
39+
rescue Exception => e
40+
@messages << "Extract event fields failed with: #{e.message}"
41+
end
42+
end
43+
end
44+
end
45+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
class HanIngestorTest < ActiveSupport::TestCase
4+
setup do
5+
@user = users(:regular_user)
6+
@content_provider = content_providers(:portal_provider)
7+
mock_ingestions
8+
mock_timezone # System time zone should not affect test result
9+
end
10+
11+
teardown do
12+
reset_timezone
13+
end
14+
15+
test 'can ingest materials from han' do
16+
source = @content_provider.sources.build(
17+
url: 'https://www.han.nl/studeren/scholing-voor-werkenden/laboratorium/',
18+
method: 'han',
19+
enabled: true
20+
)
21+
22+
ingestor = Ingestors::Taxila::HanIngestor.new
23+
24+
# check materials don't exist
25+
new_title = 'Synthetiseren en Karakteriseren van Moleculen'
26+
new_url = 'https://www.han.nl/opleidingen/module/synthetiseren-karakteriseren-moleculen/'
27+
refute Material.where(title: new_title, url: new_url).any?
28+
29+
# run task
30+
assert_difference('Material.count', 27) do
31+
freeze_time(2019) do
32+
VCR.use_cassette('ingestors/han') do
33+
ingestor.read(source.url)
34+
ingestor.write(@user, @content_provider)
35+
end
36+
end
37+
end
38+
39+
# check event does exist
40+
material = Material.where(title: new_title, url: new_url).first
41+
assert material
42+
assert_equal new_title, material.title
43+
assert_equal new_url, material.url
44+
45+
# check other fields
46+
# 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). ',
47+
# material.description
48+
end
49+
end

test/vcr_cassettes/ingestors/han.yml

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)