Skip to content

Commit

Permalink
warn on empty labels
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-butcher committed Mar 18, 2024
1 parent ffd6a4e commit bad238c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ trait MarcElectronicResources extends Logging {
// Return title as Left or linkText as Right
protected def getTitleOrLinkText(
field: MarcField
): Either[String, String] = Left(getLabel(field))
): Try[Either[String, String]] = getLabel(field) match {
case "" =>
Failure(
new Exception(s"could not construct a label from 856 field $field")
)
case label => Success(Left(label))
}

private def toItem(
field: MarcField
Expand All @@ -46,15 +52,21 @@ trait MarcElectronicResources extends Logging {
warn(ctx(exception.getMessage))
None
case Success(url) =>
val label = getTitleOrLinkText(field)
val (title, linkText) = getTitleOrLinkText(field) match {
case Success(label) => (label.left.toOption, label.right.toOption)
case Failure(exception) =>
warn(ctx(exception.getMessage))
(None, None)
}
Some(
toItem(
url = url,
status = status(field),
title = label.left.toOption,
linkText = label.right.toOption
title = title,
linkText = linkText
)
)

}

// We take the URL from subfield ǂu. If subfield ǂu is missing, repeated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import weco.pipeline.transformer.marc_common.transformers.MarcElectronicResource
import weco.pipeline.transformer.sierra.data.SierraMarcDataConversions
import weco.sierra.models.identifiers.TypedSierraRecordNumber
import weco.sierra.models.marc.VarField
import scala.util.{Failure, Success, Try}

// Create items with a DigitalLocation based on the contents of field 856.
//
Expand Down Expand Up @@ -54,35 +55,40 @@ object SierraElectronicResources
// Otherwise, we put it in the item's "title" field.
override def getTitleOrLinkText(
field: MarcField
): Either[String, String] =
): Try[Either[String, String]] =
getLabel(field) match {
case "" => Right("View resource")
case label =>
if (
label.split(" ").length <= 7 &&
label.containsAnyOf("access", "view", "connect")
case "" =>
Failure(
new Exception(s"could not construct a label from 856 field $field")
)
Right(
label
// e.g. "View resource." ~> "View resource"
.stripSuffix(".")
.stripSuffix(":")
// e.g. "view resource" ~> "View resource"
.replaceFirst("^view ", "View ")
// These are hard-coded fixes for a couple of known weird records.
// We could also fix these in the catalogue, but fixing them here
// is cheap and easy.
.replace("VIEW FULL TEXT", "View full text")
.replace("via MyiLibrary", "via MyiLibrary")
.replace("youtube", "YouTube")
.replace("View resource {PDF", "View resource [PDF")
.replace(
"View resource 613.7 KB]",
"View resource [613.7 KB]"
)
case label =>
Success(
if (
label.split(" ").length <= 7 &&
label.containsAnyOf("access", "view", "connect")
)
else
Left(label)
Right(
label
// e.g. "View resource." ~> "View resource"
.stripSuffix(".")
.stripSuffix(":")
// e.g. "view resource" ~> "View resource"
.replaceFirst("^view ", "View ")
// These are hard-coded fixes for a couple of known weird records.
// We could also fix these in the catalogue, but fixing them here
// is cheap and easy.
.replace("VIEW FULL TEXT", "View full text")
.replace("via MyiLibrary", "via MyiLibrary")
.replace("youtube", "YouTube")
.replace("View resource {PDF", "View resource [PDF")
.replace(
"View resource 613.7 KB]",
"View resource [613.7 KB]"
)
)
else
Left(label)
)
}

def apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class SierraElectronicResourcesTest
locations = List(
DigitalLocation(
url = "https://example.org/journal",
linkText = Some("View resource"),
locationType = OnlineResource,
accessConditions = List(
AccessCondition(
Expand Down Expand Up @@ -75,7 +74,6 @@ class SierraElectronicResourcesTest
DigitalLocation(
url = "https://example.org/journal",
locationType = OnlineResource,
linkText = Some("View resource"),
accessConditions = List(
AccessCondition(
method = AccessMethod.ViewOnline,
Expand All @@ -91,7 +89,6 @@ class SierraElectronicResourcesTest
DigitalLocation(
url = "https://example.org/another-journal",
locationType = OnlineResource,
linkText = Some("View resource"),
accessConditions = List(
AccessCondition(
method = AccessMethod.ViewOnline,
Expand Down Expand Up @@ -122,7 +119,6 @@ class SierraElectronicResourcesTest
DigitalLocation(
url = "https://example.org/journal",
locationType = OnlineResource,
linkText = Some("View resource"),
accessConditions = List(
AccessCondition(
method = AccessMethod.ViewOnline,
Expand Down

0 comments on commit bad238c

Please sign in to comment.