Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemify #14

Merged
merged 2 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
*.secret

/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.3.3
before_install: gem install bundler -v 1.14.3
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in sierra-postgres-utilities.gemspec
gemspec
50 changes: 24 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -160,16 +171,3 @@ c.yield_email # => [email protected]
c.yield_email(index: 'default_email') # => [email protected]
c.yield_email(index: 'other_email') # => [email protected]
```

## 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'
```
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -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__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/sierra_postgres_utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
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 = ENV['SIERRA_INIT_CREDS'] || 'prod'
SierraDB.initial_creds(creds)

require_relative 'sierra_postgres_utilities/views'
require_relative 'sierra_postgres_utilities/helpers'
require_relative 'sierra_postgres_utilities/records'
Expand Down
6 changes: 5 additions & 1 deletion lib/sierra_postgres_utilities/records/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 28 additions & 12 deletions lib/sierra_postgres_utilities/sierradb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -93,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)
Expand All @@ -108,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
Expand Down Expand Up @@ -193,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)
Expand Down
3 changes: 3 additions & 0 deletions lib/sierra_postgres_utilities/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module SierraPostgresUtilities
VERSION = '0.2.0'.freeze
end
39 changes: 39 additions & 0 deletions sierra_postgres_utilities.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']

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
30 changes: 30 additions & 0 deletions spec/records/sierra_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down