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

Commit f6946a9

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

11 files changed

+74
-6
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

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
# frozen_string_literal: true
22

3-
require 'rialto/etl/version'
3+
require 'config'
44
require 'rialto/etl/extractors'
55
require 'rialto/etl/transformers'
6+
require 'rialto/etl/version'
67

78
module Rialto
89
# The top-level Rialto::Etl module
910
module Etl
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 = true
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+
39+
Config.load_and_set_settings(
40+
Config.setting_files('config', ENV.fetch('ENV', 'production'))
41+
)
1042
end
1143
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+
allow(Settings.tokens).to receive(:cap).and_return('')
9090
end
9191

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

spec/rialto/etl_spec.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
# frozen_string_literal: true
22

3-
require 'spec_helper'
4-
53
describe Rialto::Etl do
64
it 'has a version number' do
75
expect(Rialto::Etl::VERSION).not_to be nil
86
end
7+
8+
describe 'configuration' do
9+
it 'provides a configuration value for CAP token' do
10+
expect(Settings.tokens.cap).not_to be_empty
11+
end
12+
13+
describe 'overriding values via environment variables' do
14+
let(:overridden_value) { 'dropthebeat' }
15+
16+
before do
17+
ENV['SETTINGS__TOKENS__CAP'] = overridden_value
18+
Settings.reload!
19+
end
20+
21+
it 'works as configured' do
22+
expect(Settings.tokens.cap).to eq overridden_value
23+
end
24+
end
25+
end
926
end

0 commit comments

Comments
 (0)