From e21b2f7fdb735835769b1fb65f5418294e28af5c Mon Sep 17 00:00:00 2001 From: ldss-jm Date: Thu, 10 Jan 2019 12:27:43 -0500 Subject: [PATCH 1/2] permit setting db creds before initial connection --- lib/sierra_postgres_utilities.rb | 13 ++++++++ .../records/record.rb | 6 +++- lib/sierra_postgres_utilities/sierradb.rb | 6 +++- lib/sierra_postgres_utilities/version.rb | 3 ++ spec/records/sierra_record_spec.rb | 30 +++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/sierra_postgres_utilities/version.rb diff --git a/lib/sierra_postgres_utilities.rb b/lib/sierra_postgres_utilities.rb index cd4c2c4..fa2891d 100644 --- a/lib/sierra_postgres_utilities.rb +++ b/lib/sierra_postgres_utilities.rb @@ -8,7 +8,20 @@ require_relative '../ext/marc/datafield' require_relative '../ext/marc/controlfield' + + require_relative 'sierra_postgres_utilities/sierradb' +# As it loads, sierra-postgres-utilities connects to the DB to prepare some +# queries, etc. Defining SIERRA_INIT_CREDS before loading sierra-postgres-utilities +# allows that initial connection to use the specified credentials +creds = + if defined? SIERRA_INIT_CREDS + SIERRA_INIT_CREDS + else + 'prod' + end +SierraDB.initial_creds(creds) + require_relative 'sierra_postgres_utilities/views' require_relative 'sierra_postgres_utilities/helpers' require_relative 'sierra_postgres_utilities/records' diff --git a/lib/sierra_postgres_utilities/records/record.rb b/lib/sierra_postgres_utilities/records/record.rb index c77f043..205ddce 100644 --- a/lib/sierra_postgres_utilities/records/record.rb +++ b/lib/sierra_postgres_utilities/records/record.rb @@ -165,7 +165,11 @@ def vf_codes end def deleted? - true if record_metadata.deletion_date_gmt + true if record_metadata['deletion_date_gmt'] + end + + def invalid? + true if record_id.nil? end # Returns rec creation date diff --git a/lib/sierra_postgres_utilities/sierradb.rb b/lib/sierra_postgres_utilities/sierradb.rb index d283f9f..86acf1d 100644 --- a/lib/sierra_postgres_utilities/sierradb.rb +++ b/lib/sierra_postgres_utilities/sierradb.rb @@ -17,7 +17,11 @@ module SierraDB # to allow almost all views to be read with SierraDB.view_name # e.g. SierraDB.branch_myuser - def self.conn(creds: 'prod') + def self.initial_creds(creds = nil) + @initial_creds ||= creds + end + + def self.conn(creds: initial_creds || 'prod') @conn ||= make_connection(creds: creds) end diff --git a/lib/sierra_postgres_utilities/version.rb b/lib/sierra_postgres_utilities/version.rb new file mode 100644 index 0000000..cbf173e --- /dev/null +++ b/lib/sierra_postgres_utilities/version.rb @@ -0,0 +1,3 @@ +module SierraPostgresUtilities + VERSION = '0.2.0'.freeze +end diff --git a/spec/records/sierra_record_spec.rb b/spec/records/sierra_record_spec.rb index 726c5bb..13469d6 100644 --- a/spec/records/sierra_record_spec.rb +++ b/spec/records/sierra_record_spec.rb @@ -8,6 +8,7 @@ def set_attr(obj, attr, value) RSpec.describe SierraRecord do let(:rec) { SierraRecord.new(rnum: 'b1841152a', rtype: 'b') } let(:del_rec) { SierraRecord.new(rnum: 'b6780003a', rtype: 'b') } + let(:invalid_rec) { SierraRecord.new(rnum: 'b1111111111a', rtype: 'b')} describe '#deleted?' do @@ -22,6 +23,35 @@ def set_attr(obj, attr, value) expect(rec.deleted?).to be_falsey end end + + context 'record is invalid' do + it 'returns falsey' do + expect(invalid_rec.deleted?).to be_falsey + end + end + end + + describe '#invalid?' do + + context 'record never existed' do + it 'returns boolean true' do + expect(invalid_rec.invalid?).to be true + end + end + + context 'undeleted record exists' do + it 'returns falsey' do + expect(rec.invalid?).to be_falsey + end + end + + context 'deleted record exists' do + it 'returns falsey' do + expect(del_rec.invalid?).to be_falsey + end + end + + end describe '.vf_codes' do From 43707a4656766f5ef163a8c259777171537881a7 Mon Sep 17 00:00:00 2001 From: ldss-jm Date: Wed, 23 Jan 2019 11:27:44 -0500 Subject: [PATCH 2/2] Gemify. --- .gitignore | 13 ++++++ .rspec | 2 + .travis.yml | 5 +++ Gemfile | 4 ++ README.md | 50 +++++++++++------------ Rakefile | 6 +++ bin/console | 14 +++++++ bin/setup | 8 ++++ lib/sierra_postgres_utilities.rb | 7 +--- lib/sierra_postgres_utilities/sierradb.rb | 34 ++++++++++----- sierra_postgres_utilities.gemspec | 39 ++++++++++++++++++ 11 files changed, 139 insertions(+), 43 deletions(-) create mode 100644 .rspec create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 Rakefile create mode 100644 bin/console create mode 100644 bin/setup create mode 100644 sierra_postgres_utilities.gemspec diff --git a/.gitignore b/.gitignore index bceb371..c92f286 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,14 @@ *.secret + +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..8c18f1a --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--format documentation +--color diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d6ef4d9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +sudo: false +language: ruby +rvm: + - 2.3.3 +before_install: gem install bundler -v 1.14.3 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ee0252a --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in sierra-postgres-utilities.gemspec +gemspec diff --git a/README.md b/README.md index 3d9c2c8..0b359ae 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ __NOTE: Some sites may have iii setups that store different data in different pl ### Interact with bib (and other) records ```ruby -require_relative 'lib/sierra_postgres_utilities.rb' +require 'sierra_postgres_utilities' bnum = 'b9256886a' bib = SierraBib.new(bnum) @@ -105,17 +105,15 @@ return any of ```bib```'s entries in those two views. ## SETUP -* Clone or download a copy. -* ```gem install mail``` -* ```gem install pg``` -* ```gem install marc``` +* git clone https://github.com/UNC-Libraries/sierra-postgres-utilities +* cd sierra-postgres-utilities +* bundle install +* bundle exec rake install * supply the Sierra postgres credentials per the below * optionally supply smtp server address ### Credentials -#### Stored in file - Create a yaml file in the base directory like so: ```yaml @@ -126,20 +124,33 @@ user: myusername password: mypassword ``` -If you name the file ```sierra_prod.secret``` it will be the default connection. +Store the creds in a file ```sierra_prod.secret``` in the +current working directory or the base directory of sierra_postgres_utilities. +Creds from this file will be used as the default connection. + +Alternately, specify a credential file location as an environment variable, e.g.: -Use some other file with ```SierraDB.connect_as(creds: filename)``` +```bash +SIERRA_INIT_CREDS=my/path/file.yaml irb +``` -Set a test server connection in a file named ```sierra_test.secret```. Use it with ```SierraDB.connect_as(creds: 'test')``` +or set the file location in ruby: -#### Passed as argument +```ruby +# File location +ENV['SIERRA_INIT_CREDS'] = 'my/path/file.yaml' +require 'sierra_postgres_utilities' +``` -Pass the connection info (host, port, dbname, user, password) in a hash: ```SierraDB.connect_as(creds: cred_hash)``` +Once connected to the Sierra DB, you can close the connection and reconnect +under alternate creds using: +- ```SierraDB.connect_as(creds: filename)```, or +- ```SierraDB.connect_as(creds: cred_hash)``` ### SMTP connection / email address storage Define an smtp connection (that does not require authentication) if you'll use this to send emails. -Create ```smtp.secret``` in the base directory: +Create ```smtp.secret``` in the working directory: ```yaml address: smtp.example.com @@ -160,16 +171,3 @@ c.yield_email # => user@example.com c.yield_email(index: 'default_email') # => user@example.com c.yield_email(index: 'other_email') # => other_user@example.com ``` - -## Loading into other scripts - -This isn't a gem. It isn't getting installed and can have varying paths, so we've been keeping the sierra-postgres-utilities folder and the folders for scripts dependent on sierra-postgres-utilities in the same directory, so: - -* .../code/sierra-postgres-utilities/.git -* .../code/dependent_repo/dependent_thing.rb - -and then in dependent_thing.rb doing: - -```ruby -require_relative '../sierra-postgres-utilities/lib/sierra_postgres_utilities.rb' -``` diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..b7e9ed5 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/bin/console b/bin/console new file mode 100644 index 0000000..9960dc4 --- /dev/null +++ b/bin/console @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "sierra_postgres_utilities" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100644 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/sierra_postgres_utilities.rb b/lib/sierra_postgres_utilities.rb index fa2891d..c6a84f3 100644 --- a/lib/sierra_postgres_utilities.rb +++ b/lib/sierra_postgres_utilities.rb @@ -14,12 +14,7 @@ # As it loads, sierra-postgres-utilities connects to the DB to prepare some # queries, etc. Defining SIERRA_INIT_CREDS before loading sierra-postgres-utilities # allows that initial connection to use the specified credentials -creds = - if defined? SIERRA_INIT_CREDS - SIERRA_INIT_CREDS - else - 'prod' - end +creds = ENV['SIERRA_INIT_CREDS'] || 'prod' SierraDB.initial_creds(creds) require_relative 'sierra_postgres_utilities/views' diff --git a/lib/sierra_postgres_utilities/sierradb.rb b/lib/sierra_postgres_utilities/sierradb.rb index 86acf1d..523d737 100644 --- a/lib/sierra_postgres_utilities/sierradb.rb +++ b/lib/sierra_postgres_utilities/sierradb.rb @@ -97,7 +97,12 @@ def self.mail_results(outfile, mail_details, remove_file: false) end def self.emails - @emails ||= YAML.load_file(File.join(base_dir, '/email.secret')) + @emails ||= + begin + YAML.load_file('email.secret') + rescue Errno::ENOENT + YAML.load_file(File.join(base_dir, '/email.secret')) + end end def self.yield_email(index = nil) @@ -112,24 +117,26 @@ def self.base_dir # Connects to SierraDB using creds from specified YAML file or given hash. # # Possible specified credentials: - # 'prod' : uses sierra_prod.secret in base directory # creds for prod db - # 'test' : uses sierra_test.secret in base directory # creds for test db - # [filename] : reads secrets from file specified. First looks for file in - # secrets dir, and looks for file in current dir if that fails + # 'prod' : uses sierra_prod.secret in pwd or base directory; creds for prod db + # 'test' : uses sierra_test.secret in pwd or base directory; creds for test db + # [filename] : reads secrets from file specified. First looks in pwd, then + # looks in base directory # [somehash]: accepts a hash containing the credentials def self.make_connection(creds:) if creds == 'prod' - @cred = YAML.load_file(File.join(base_dir, '/sierra_prod.secret')) + creds = 'sierra_prod.secret' elsif creds == 'test' - @cred = YAML.load_file(File.join(base_dir, '/sierra_test.secret')) - elsif creds.is_a?(Hash) + creds = 'sierra_test.secret' + end + + if creds.is_a?(Hash) @cred = creds else begin - @cred = YAML.load_file(File.join(base_dir, creds)) + @cred = YAML.load_file(creds) rescue Errno::ENOENT begin - @cred = YAML.load_file(creds) + @cred = YAML.load_file(File.join(base_dir, creds)) rescue Errno::ENOENT raise('Connection credentials invalid or not found.') end @@ -197,7 +204,12 @@ def self.write_xlsx(outfile, results, headers) end def self.smtp - @smtp ||= YAML.load_file(File.join(base_dir, '/smtp.secret')) + @smtp ||= + begin + YAML.load_file('smtp.secret') + rescue Errno::ENOENT + YAML.load_file(File.join(base_dir, '/smtp.secret')) + end end def self.send_mail(outfile, mail_details, remove_file: false) diff --git a/sierra_postgres_utilities.gemspec b/sierra_postgres_utilities.gemspec new file mode 100644 index 0000000..73e040b --- /dev/null +++ b/sierra_postgres_utilities.gemspec @@ -0,0 +1,39 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'sierra_postgres_utilities/version' + +Gem::Specification.new do |spec| + spec.name = 'sierra_postgres_utilities' + spec.version = SierraPostgresUtilities::VERSION + spec.authors = ['ldss-jm', 'Kristina Spurgin'] + spec.email = ['ldss-jm@users.noreply.github.com'] + + spec.summary = 'Connects to iii Sierra postgres DB and provides ' \ + 'logic/utilities to interact with Sierra records in ruby.' + spec.homepage = "https://github.com/UNC-Libraries/sierra-postgres-utilities" + + # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' + # to allow pushing to a single host or delete this section to allow pushing to any host. + if spec.respond_to?(:metadata) + spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" + else + raise 'RubyGems 2.0 or newer is required to protect against ' \ + 'public gem pushes.' + end + + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_development_dependency 'bundler', '~> 1.14' + spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'rspec', '~> 3.0' + + spec.add_runtime_dependency 'mail', '~> 2.6' + spec.add_runtime_dependency 'marc', '~> 1.0' + spec.add_runtime_dependency 'pg', '~> 1.1' +end