Skip to content

Commit

Permalink
Gemify.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldss-jm committed Jan 23, 2019
1 parent e21b2f7 commit 43707a4
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 43 deletions.
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
7 changes: 1 addition & 6 deletions lib/sierra_postgres_utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
34 changes: 23 additions & 11 deletions lib/sierra_postgres_utilities/sierradb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
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

0 comments on commit 43707a4

Please sign in to comment.