Skip to content

Commit

Permalink
Merge pull request #85 from sul-dlss/marcxml-endpoint/dor-services-ap…
Browse files Browse the repository at this point in the history
…p#4476
  • Loading branch information
mjgiarlo authored Oct 4, 2023
2 parents 3004444 + 6112115 commit 9a58b55
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 165 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Integration Testing

To test that the gem works against the Folio APIs, run `api_test.rb` via:

```shell
# NOTE: This is bash syntax, YMMV
$ export OKAPI_PASSWORD=$(vault kv get --field=content puppet/application/folio/stage/app_sdr_password)
$ export OKAPI_TENANT=sul
$ export OKAPI_USER=app_sdr
$ export OKAPI_URL=https://okapi-stage.stanford.edu
# NOTE: The args below are a list of MARC files
$ bundle exec ruby ./api_test.rb /path/to/marc/files/test.mrc /another/marc/file/at/foobar.marc
```

Inspect the output and make sure there are no errors.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sul-dlss/folio_client.
38 changes: 38 additions & 0 deletions api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "folio_client"

marc_files = *ARGV

client =
FolioClient.configure(
url: ENV["OKAPI_URL"],
login_params: {
username: ENV["OKAPI_USER"],
password: ENV["OKAPI_PASSWORD"]
},
okapi_headers: {
"X-Okapi-Tenant": ENV["OKAPI_TENANT"],
"User-Agent": "folio_client gem (testing)"
}
)

pp(client.fetch_marc_hash(instance_hrid: "a666"))

puts client.fetch_marc_xml(instance_hrid: "a666")
puts client.fetch_marc_xml(barcode: "20503330279")

records = marc_files.flat_map do |marc_file_path|
MARC::Reader.new(marc_file_path).to_a
end

data_importer =
client.data_import(
records: records,
job_profile_id: "e34d7b92-9b83-11eb-a8b3-0242ac130003",
job_profile_name: "Default - Create instance and SRS MARC Bib"
)

puts data_importer.wait_until_complete
puts data_importer.instance_hrids
18 changes: 13 additions & 5 deletions lib/folio_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/object/blank"
require "faraday"
require "singleton"
require "marc"
require "ostruct"
require "singleton"
require "zeitwerk"

# Load the gem's internal dependencies: use Zeitwerk instead of needing to manually require classes
Expand Down Expand Up @@ -72,10 +73,10 @@ def configure(url:, login_params:, okapi_headers:, timeout: default_timeout)

delegate :config, :connection, :data_import, :default_timeout,
:edit_marc_json, :fetch_external_id, :fetch_hrid, :fetch_instance_info,
:fetch_marc_hash, :get, :has_instance_status?, :http_get_headers,
:http_post_and_put_headers, :interface_details, :job_profiles,
:organization_interfaces, :organizations, :post, :put, to: :instance
end
:fetch_marc_hash, :fetch_marc_xml, :get, :has_instance_status?,
:http_get_headers, :http_post_and_put_headers, :interface_details,
:job_profiles, :organization_interfaces, :organizations, :post, :put, to:
:instance end

attr_accessor :config

Expand Down Expand Up @@ -175,6 +176,13 @@ def fetch_marc_hash(...)
.fetch_marc_hash(...)
end

# @see SourceStorage#fetch_marc_xml
def fetch_marc_xml(...)
SourceStorage
.new(self)
.fetch_marc_xml(...)
end

# @see Inventory#has_instance_status?
def has_instance_status?(...)
Inventory
Expand Down
1 change: 0 additions & 1 deletion lib/folio_client/data_import.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "date"
require "marc"
require "stringio"

class FolioClient
Expand Down
33 changes: 33 additions & 0 deletions lib/folio_client/source_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class FolioClient
# Lookup records in Folio Source Storage
class SourceStorage
FIELDS_TO_REMOVE = %w[001 003].freeze

attr_accessor :client

# @param client [FolioClient] the configured client
Expand All @@ -24,5 +26,36 @@ def fetch_marc_hash(instance_hrid:)

response_hash["sourceRecords"].first["parsedRecord"]["content"]
end

# get marc bib data as MARCXML from folio given an instance HRID
# @param instance_hrid [String] the instance HRID to use for MARC lookup
# @param barcode [String] the barcode to use for MARC lookup
# @return [String] MARCXML string
# @raise [ResourceNotFound]
# @raise [MultipleResourcesFound]
def fetch_marc_xml(instance_hrid: nil, barcode: nil)
raise ArgumentError, "Either a barcode or a Folio instance HRID must be provided" if barcode.nil? && instance_hrid.nil?

instance_hrid ||= client.fetch_hrid(barcode: barcode)

raise ResourceNotFound, "Catalog record not found. HRID: #{instance_hrid} | Barcode: #{barcode}" if instance_hrid.blank?

marc_record = MARC::Record.new_from_hash(
fetch_marc_hash(instance_hrid: instance_hrid)
)
updated_marc = MARC::Record.new
updated_marc.leader = marc_record.leader
marc_record.fields.each do |field|
# explicitly remove all listed tags from the record
next if FIELDS_TO_REMOVE.include?(field.tag)

updated_marc.fields << field
end
# explicitly inject the instance_hrid into the 001 field
updated_marc.fields << MARC::ControlField.new("001", instance_hrid)
# explicitly inject FOLIO into the 003 field
updated_marc.fields << MARC::ControlField.new("003", "FOLIO")
updated_marc.to_xml.to_s
end
end
end
Loading

0 comments on commit 9a58b55

Please sign in to comment.