Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit be781e3

Browse files
committed
Use the config gem to manage configuration
Fixes #14
1 parent 30b40ce commit be781e3

11 files changed

+65
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
/spec/reports/
99
/tmp/
1010
/spec/sensitive-fixtures/
11+
config/settings.local.yml
12+
config/settings/*.local.yml

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ Run `bin/extract` to run a named extractor and print output to STDOUT:
3535
$ bin/extract -s StanfordResearchers
3636
{"count":10,"firstPage":true,"lastPage":false,"page":1,"totalCount":29089,"totalPages":2909,"values":[{"administrativeAppointments":[...
3737

38-
Note: if you need to run any of the extractors that inherit from `AbstractStanfordExtractor`, you will first need to obtain a token for the CAP API and set the `CAP_TOKEN` environment variable in your session.
38+
Note: if you need to run any of the extractors that inherit from `AbstractStanfordExtractor`, you will first need to obtain a token for the CAP API and set the `Settings.tokens.cap` value to this token. To set this value, either set an environment variable named `SETTINGS__TOKENS__CAP` or add the value for this to `config/settings.local.yml` (which is ignored under version control and should never be checked in), like so:
39+
40+
```yaml
41+
tokens:
42+
cap: 'foobar'
43+
```
3944
4045
### Transform
4146
@@ -48,6 +53,10 @@ Run `bin/transform` to run a named transformer, based on [Traject](https://githu
4853

4954
TBD
5055

56+
## Configuration
57+
58+
Rialto::Etl uses the [config gem](https://github.com/railsconfig/config) to manage configuration, allowing for flexible variation of configs between environments and hosts. By default, the gem assumes it is running in the `'production'` environment and will look for its configurations per the [config gem documentation](https://github.com/railsconfig/config#accessing-the-settings-object). To explicitly set the environment to `test` or `development`, set an environment variable named `ENV`.
59+
5160
## Help
5261

5362
$ bin/extract -h

config/settings.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# These settings can also be provided as environment variables, in the form, e.g.:
2+
# SETTINGS__TOKENS__CAP=foobar
3+
#
4+
# The mapping is described in `./lib/rialto/etl/configuration.rb`.
5+
6+
tokens:
7+
cap: 'dummyvalue'

config/settings/development.yml

Whitespace-only changes.

config/settings/production.yml

Whitespace-only changes.

config/settings/test.yml

Whitespace-only changes.

lib/rialto/etl.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# frozen_string_literal: true
22

3-
require 'rialto/etl/version'
3+
require 'config'
4+
require 'rialto/etl/configuration'
45
require 'rialto/etl/extractors'
56
require 'rialto/etl/transformers'
7+
require 'rialto/etl/version'
68

79
module Rialto
810
# The top-level Rialto::Etl module

lib/rialto/etl/configuration.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module Rialto
4+
module Etl
5+
# Hold configuration for Rialto::Etl
6+
module Configuration
7+
Config.load_and_set_settings(
8+
Config.setting_files('config', ENV.fetch('ENV', 'production'))
9+
)
10+
11+
Config.setup do |config|
12+
# Name of the constant exposing loaded settings
13+
config.const_name = 'Settings'
14+
15+
# Load environment variables from the `ENV` object and override any
16+
# settings defined in files.
17+
config.use_env = false
18+
19+
# Define ENV variable prefix deciding which variables to load into
20+
# config.
21+
config.env_prefix = 'SETTINGS'
22+
23+
# What string to use as level separator for settings loaded from ENV
24+
# variables. Default value of '.' works well with Heroku, but you might
25+
# want to change it for example for '__' to easy override settings from
26+
# command line, where using dots in variable names might not be allowed
27+
# (eg. Bash).
28+
config.env_separator = '__'
29+
30+
# Ability to process variables names:
31+
# * nil - no change
32+
# * :downcase - convert to lower case
33+
config.env_converter = :downcase
34+
35+
# Parse numeric values as integers instead of strings.
36+
config.env_parse_values = true
37+
end
38+
end
39+
end
40+
end

lib/rialto/etl/extractors/abstract_stanford_extractor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def auth_client
8686
end
8787

8888
def auth_code
89-
@auth_code ||= Base64.strict_encode64("sul:#{ENV['CAP_TOKEN']}")
89+
@auth_code ||= Base64.strict_encode64("sul:#{Settings.tokens.cap}")
9090
end
9191
end
9292
end

rialto-etl.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = ['lib']
2020

21+
spec.add_dependency 'config'
2122
spec.add_dependency 'faraday'
2223
spec.add_dependency 'httpclient'
2324
spec.add_dependency 'rdf'

spec/extractors/abstract_stanford_extractor_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
describe '#auth_code' do
8888
before do
89-
allow(ENV).to receive(:[]).with('CAP_TOKEN').and_return('')
89+
Settings.tokens.cap = ''
9090
end
9191

9292
subject(:code) { described_class.new.send(:auth_code) }

0 commit comments

Comments
 (0)