Skip to content

Commit

Permalink
Merge pull request #19 from UNC-Libraries/excise-derivative-records
Browse files Browse the repository at this point in the history
Excise derivative records into addon gem
  • Loading branch information
ldss-jm authored Jul 31, 2019
2 parents 488e4ba + e2e3d1c commit 8f70e08
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 255 deletions.
2 changes: 0 additions & 2 deletions lib/sierra_postgres_utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ module Sierra

require_relative 'sierra_postgres_utilities/search'
require_relative 'sierra_postgres_utilities/record'

require_relative 'sierra_postgres_utilities/derivative_bib'
end

require_relative 'sierra_postgres_utilities/spec_support'
Expand Down
12 changes: 9 additions & 3 deletions lib/sierra_postgres_utilities/data/records/bib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ def call_number_prefixes

alias cat_date cataloging_date_gmt

# Safe navigation is preferable here as weird circumstances (presumably
# errors) can lead bib records in the db to lack a bib_record_property
# entry. (For record, b100002, we had two record_metadata entries with
# that record_type_code and record_num; one was an ncip record. The
# ncip record had a bib_record_property record, the non-ncip bib did
# not.)
def mat_type
property[:material_code]
property&.material_code
end

# @return [Array<String>] record's location code(s) excepting "multi"
Expand All @@ -82,11 +88,11 @@ def location_codes
end

def best_title
property.best_title
property&.best_title
end

def best_author
property.best_author
property&.best_author
end

# Returns record's imprint.
Expand Down
33 changes: 19 additions & 14 deletions lib/sierra_postgres_utilities/db/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ def results
# Writes results to file.
#
# Formats: tsv, csv, xlsx (xlsx writable on windows only)
# Headers only need to be specified when they differ from
# the columns of the query results.
#
# @param [String] outfile path for outfile
# - for xlsx only: a relative path is relative to user's windows
# home directory, so using an absolute path may be preferable
# @param [Enumerable<#values>] results (default: Sierra::DB.results)
# @param [Array<String>] headers (default: nil)
# @param [Boolean] include_headers (default: true) write headers to file?
# @param [Symbol] format (default: tsv) format of export: :tsv, :csv,
# :xlsx.
def write_results(outfile, results: self.results,
def write_results(outfile, results: self.results, headers: nil,
include_headers: true, format: :tsv)
Sierra::DB::Query.write_results(outfile,
results: results, format: format,
headers: headers,
include_headers: include_headers)
end

Expand Down Expand Up @@ -76,15 +80,11 @@ def self.headers
end

# (see #write_results)
def self.write_results(outfile, results: self.results,
def self.write_results(outfile, results: self.results, headers: nil,
include_headers: true, format: :tsv)
puts 'writing results'
headers =
if include_headers
self.headers
else
''
end
headers ||= self.headers
headers = '' unless include_headers

format = format.to_sym
case format
Expand Down Expand Up @@ -159,12 +159,14 @@ def self.write_xlsx(outfile, results, headers)

# Returns cached email "address book" or reads it from 'email.secret'
# yaml file.
def self.emails
def self.emails(file = 'email.secret')
@emails ||=
begin
YAML.load_file('email.secret')
YAML.load_file(file)
rescue Errno::ENOENT
YAML.load_file(File.join(base_dir, '/email.secret'))
YAML.load_file(File.join(Connection.base_dir, file))
rescue TypeError
YAML.load(file)
end
end

Expand All @@ -176,12 +178,14 @@ def self.emails=(hsh)
# yaml file.
#
# @return [Hash] smtp server connection details (address, port)
def self.smtp
def self.smtp(file = 'smtp.secret')
@smtp ||=
begin
YAML.load_file('smtp.secret')
YAML.load_file(file)
rescue Errno::ENOENT
YAML.load_file(File.join(base_dir, '/smtp.secret'))
YAML.load_file(File.join(Connection.base_dir, file))
rescue TypeError
YAML.load(file)
end
end

Expand All @@ -197,6 +201,7 @@ def self.send_mail(outfile, mail_details, remove_file: false)
Mail.deliver do
from mail_details[:from]
to mail_details[:to]
cc mail_details[:cc]
subject mail_details[:subject]
body mail_details[:body]

Expand Down
140 changes: 0 additions & 140 deletions lib/sierra_postgres_utilities/derivative_bib.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/sierra_postgres_utilities/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sierra
VERSION = '0.3.1'.freeze
VERSION = '0.3.2'.freeze
end
14 changes: 1 addition & 13 deletions spec/data/record/bib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
let(:data) { build(:data_b) }
let(:bib) { newrec(Sierra::Data::Bib, metadata, data) }

let(:property) do
{id: 1076136,
bib_record_id: 420908636160,
best_title: 'Something else : a novel',
bib_level_code: 'm',
material_code: 'a',
publish_year: 1981,
best_title_norm: 'something else a novel',
best_author: 'Fassnidge, Virginia.',
best_author_norm: 'fassnidge virginia'}
end

describe '#bnum' do
it 'returns rnum (including leading-letter and trailing-a)' do
expect(bib.bnum).to eq(bib.rnum)
Expand Down Expand Up @@ -45,7 +33,7 @@

describe '#mat_type' do
it 'returns material type from bib_record_property' do
bib.set_data(:property, property)
bib.set_data(:property, build(:bib_property))
expect(bib.mat_type).to eq('a')
end
end
Expand Down
33 changes: 31 additions & 2 deletions spec/db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ module Sierra
Sierra::DB.write_results(io)
expect(io.string[0..1]).to eq('id')
end

it 'uses passed headers when present' do
Sierra::DB.write_results(io, headers: ['ego'])
expect(io.string[0..2]).to eq('ego')
end
end

context 'with include_headers: false' do
Expand All @@ -35,23 +40,47 @@ module Sierra
end
end

context 'with format: csv (default)' do
context 'with format: csv' do
it 'writes to a csv' do
Sierra::DB.write_results(io, format: :csv)
expect(io.string.each_line.first.split(',').first).to eq('id')
end
end

context ' with format: xlsx (default)' do
context 'with format: xlsx' do
xit 'writes to an xlsx' do
end
end

context 'when passed an array of objects that respond to :values)' do
it 'writes the values to output as rows' do
Sierra::DB.write_results(io, results: [{a: 'foo', b: 'bar'}],
include_headers: false)
expect(io.string[0..6]).to eq("foo\tbar")
end
end

context 'otherwise' do
it 'writes query results' do
Sierra::DB.write_results(io, include_headers: false)
expect(io.string[0..1]).to match(/[0-9]*/)
end
end
end

describe '.mail_results' do
end

describe '.yield_email' do
it 'returns email address in Query.emails for given key' do
Sierra::DB::Query.emails(StringIO.new("default_email: [email protected]\nother_email: [email protected]"))
expect(Sierra::DB.yield_email('other_email')).to eq('[email protected]')
end

it 'returns email address in Query.emails for "default_email"' do
Sierra::DB::Query.emails(StringIO.new("default_email: [email protected]\nother_email: [email protected]"))
expect(Sierra::DB.yield_email).to eq('[email protected]')
end
end
end
end
Loading

0 comments on commit 8f70e08

Please sign in to comment.