Skip to content

Commit

Permalink
Try to create resource_page_url for libguides databases
Browse files Browse the repository at this point in the history
- This is not exposed in their API, and is somehow built from the title of the resource, but does not seem to be consistent.

Connected to #829
  • Loading branch information
maxkadel committed Aug 7, 2024
1 parent 48ffb19 commit 4294b8d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
13 changes: 11 additions & 2 deletions app/models/web_database_list/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ def initialize(json)
def to_csv_row
row = CSV::Row.new([], [])
self.class.field_names.each do |field|
row << [field, instance_variable_get("@#{field}")]
row << [field, send(field)]
end
row
end

def self.field_names
[:id, :name, :description, :alt_names, :url, :friendly_url, :subjects]
[:id, :name, :description, :alt_names, :url, :friendly_url, :subjects, :resource_page_url]
end

def resource_page_url
@resource_page_url ||= URI::HTTPS.build(host: 'libguides.princeton.edu', path: "/az/#{name_to_path}").to_s
end

private
Expand All @@ -35,4 +39,9 @@ def parse(json)
''
end
end

def name_to_path
first_title = name.split(/\; /).first
first_title.tr('^a-zA-Z0-9 ', '').downcase.split(' ').join('-')
end
end
43 changes: 43 additions & 0 deletions spec/fixtures/files/libguides_databases.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,48 @@
"internal_note": "",
"library_review": "",
"alt_names": "Deutsche Literatur des 18. Jahrhunderts Online; Eighteenth Century German Literature Online"
},
{
"id": "74881720",
"name": "Chosŏn ŏllon chŏngbo kiji",
"description": "Chosŏn ŏllon chŏngbo kiji is a full-text database of North Korean newspapers and periodicals: 5 major newspapers (4 in Korean and 1 in English), 20+ periodicals in humanities and social science, 40+ periodicals in science and medicines.",
"url": "https://dprkmedia.com/",
"site_id": "77",
"type_id": "10",
"owner_id": "231",
"az_vendor_id": "0",
"meta": {
"target": "0",
"enable_proxy": "1",
"tn_url": "",
"tn_height": "",
"tn_width": "",
"tn_alt_text": "",
"more_info": "",
"desc_pos": "1"
},
"created": "2014-06-13 02:57:45",
"updated": "2023-01-25 17:29:18",
"slug_id": "74881720",
"enable_hidden": "0",
"az_vendor_name": null,
"subjects": [
{
"id": "74881720",
"site_id": "77",
"parent_id": "0",
"name": "Korean Studies",
"ordering": "0",
"slug_id": "74881720",
"subject_id": "74881720",
"content_id": "74881720",
"featured": "0",
"content_id_count": "1"
}
],
"friendly_url": "https://libguides.princeton.edu/resource/74881720",
"internal_note": "",
"library_review": "",
"alt_names": ""
}
]
27 changes: 23 additions & 4 deletions spec/models/web_database_list/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,45 @@
end
it 'retrieves the alt_names, if present, from the provided json' do
expect(databases.first.alt_names).to eq(nil)
expect(databases.last.alt_names).to eq('Deutsche Literatur des 18. Jahrhunderts Online; Eighteenth Century German Literature Online')
expect(databases.fifth.alt_names).to eq('Deutsche Literatur des 18. Jahrhunderts Online; Eighteenth Century German Literature Online')
end
it 'retrieves the url from the provided json' do
expect(databases.third.url).to eq('http://aabd.chadwyck.com/')
end
it 'retrieves the friendly_url, if present, from the provided json' do
expect(databases.fourth.friendly_url).to eq(nil)
expect(databases.last.friendly_url).to eq('https://libguides.princeton.edu/resource/4856')
expect(databases.fifth.friendly_url).to eq('https://libguides.princeton.edu/resource/4856')
end
it 'retrieves the subjects and concatenates them into a string' do
expect(databases.second.subjects).to eq('African Studies')
expect(databases.third.subjects).to eq('African American Studies;Biographical Sources')
expect(databases.fourth.subjects).to eq('')
end
describe '#resource_page_url' do
it 'generates a resource page url from the title' do
expect(databases.first.resource_page_url).to eq('https://libguides.princeton.edu/az/africa-knowledge-project')
end

it 'removes non A-Z/0-9 characters' do
expect(databases.second.resource_page_url).to eq('https://libguides.princeton.edu/az/africabiborg-africana-periodical-literature')
end

it 'only uses the first part of title with translations' do
expect(databases.fifth.resource_page_url).to eq('https://libguides.princeton.edu/az/18th-century-german-literature-online')
end

it 'uses unaccented characters' do
pending('libguides making sense-it randomly removes a word from the title')
expect(databases[5].resource_page_url).to eq('https://libguides.princeton.edu/az/choson-ollon-kiji')
end
end
end

describe '#to_csv_row' do
it 'creates a CSV::Row with the correct content' do
expect(databases.second.to_csv_row).to eq(
CSV::Row.new(
[:id, :name, :description, :alt_names, :url, :friendly_url, :subjects],
[:id, :name, :description, :alt_names, :url, :friendly_url, :subjects, :resource_page_url],
['2938717',
'AfricaBib.org: Africana Periodical Literature',
'Free index to articles on a wide range of topics relating to all countries in Africa. Some of the databases included '\
Expand All @@ -51,7 +69,8 @@
nil,
'http://www.africabib.org/perio.htm',
'https://libguides.princeton.edu/resource/11553',
'African Studies']
'African Studies',
'https://libguides.princeton.edu/az/africabiborg-africana-periodical-literature']
)
)
end
Expand Down
5 changes: 3 additions & 2 deletions spec/models/web_database_list/databases_feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
nil,
'http://www.africaknowledgeproject.org',
'https://libguides.princeton.edu/resource/5014',
'African Studies']
'African Studies',
'https://libguides.princeton.edu/az/africa-knowledge-project']
end
let(:feed) do
database_list = JSON.parse(File.read(file_fixture('libguides_databases.json')))
Expand All @@ -34,7 +35,7 @@

it 'the CSV file has a header row and one row for each database' do
feed.run
expect(CSV.read(filename).length).to eq(5)
expect(CSV.read(filename).length).to eq(6)
end

it 'the first row of data is correct' do
Expand Down

0 comments on commit 4294b8d

Please sign in to comment.