From 01724989a017f72cdbf5ae7ee10178347c8a3cea Mon Sep 17 00:00:00 2001 From: Tom Johnson Date: Wed, 24 Jan 2018 19:16:13 -0800 Subject: [PATCH] Add importer configuration for creator and file_path `RecordImporter` needs to know which `User` to treat as the creator for imported objects, and where on the file system to look for referenced file names. These configuration options are added to `.env`, `config/importer.yml`, and `Importer.config`. They are passed to `RecordImporter` by default on creation. This work could be extended to allow overriding the configuration in the import rake task by allowing that task to build a custom `RecordImporter`. For now, the supporeted configuration is through `.env`. Connected to #170. --- .env | 3 +++ app/importers/importer.rb | 24 +++++++++++++++++++++++- app/importers/mahonia_record_importer.rb | 4 ++-- config/importer.yml | 15 +++++++++++++++ spec/importers/importer_spec.rb | 17 +++++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 config/importer.yml diff --git a/.env b/.env index 1834b25..2822548 100644 --- a/.env +++ b/.env @@ -3,6 +3,9 @@ DATACITE_HOST='"https://mds.datacite.org"' DATACITE_LOGIN=DATACITE.DCE DATACITE_PASSWORD=YOUR_PASSWORD_HERE DATACITE_PREFIX=10.24354 +IMPORTER_USER='import_user@example.com' +IMPORTER_USER_PASS=password +IMPORTER_FILE_PATH='spec/fixtures/' OKCOMPUTER_LOGIN=radiohead_dev OKCOMPUTER_PASSWORD=th0m_y0rke SIDEKIQ_CONCURRENCY=5 diff --git a/app/importers/importer.rb b/app/importers/importer.rb index d41bd5a..11e99a3 100644 --- a/app/importers/importer.rb +++ b/app/importers/importer.rb @@ -1,6 +1,28 @@ # frozen_string_literal: true class Importer < Darlingtonia::Importer - def initialize(parser:, record_importer: MahoniaRecordImporter.new) + ## + # @return [Hash] + def self.config + Rails.application.config_for(:importer) + end + + def initialize(parser:, record_importer: default_record_importer) super end + + def config + self.class.config + end + + private + + def default_creator + User + .first_or_create!(email: config['username'], password: config['password']) + end + + def default_record_importer + MahoniaRecordImporter.new(creator: default_creator, + file_path: config['file_path']) + end end diff --git a/app/importers/mahonia_record_importer.rb b/app/importers/mahonia_record_importer.rb index 17acb34..532216d 100644 --- a/app/importers/mahonia_record_importer.rb +++ b/app/importers/mahonia_record_importer.rb @@ -15,8 +15,8 @@ class MahoniaRecordImporter < Darlingtonia::RecordImporter # @param file_path [String] # @param creator [User] def initialize(**opts) - self.creator = opts.delete(:creator) { User.first_or_create!(email: 'import_user@example.com', password: 'password') } - self.file_path = opts.delete(:file_path) { 'spec/fixtures/' } + self.creator = opts.delete(:creator) || raise(ArgumentError) + self.file_path = opts.delete(:file_path) || raise(ArgumentError) super end diff --git a/config/importer.yml b/config/importer.yml new file mode 100644 index 0000000..bc31f98 --- /dev/null +++ b/config/importer.yml @@ -0,0 +1,15 @@ +--- +development: + username: <%= ENV['IMPORTER_USER'] %> + password: <%= ENV['IMPORTER_USER_PASS'] %> + file_path: <%= ENV['IMPORTER_FILE_PATH'] %> + +test: + username: <%= ENV['IMPORTER_USER'] %> + password: <%= ENV['IMPORTER_USER_PASS'] %> + file_path: <%= ENV['IMPORTER_FILE_PATH'] %> + +production: + username: <%= ENV['IMPORTER_USER'] %> + password: <%= ENV['IMPORTER_USER_PASS'] %> + file_path: <%= ENV['IMPORTER_FILE_PATH'] %> diff --git a/spec/importers/importer_spec.rb b/spec/importers/importer_spec.rb index 6869366..e694451 100644 --- a/spec/importers/importer_spec.rb +++ b/spec/importers/importer_spec.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true require 'rails_helper' + RSpec.describe Importer do subject(:importer) { described_class.new(parser: parser) } let(:file) { File.open('spec/fixtures/example.csv') } @@ -27,6 +28,22 @@ expect(Etd.where(title: "Unicode Description").count).to eq 1 end + describe '.config' do + it 'has configuration attributes' do + expect(described_class.config).to include('username' => an_instance_of(String), + 'password' => an_instance_of(String), + 'file_path' => an_instance_of(String)) + end + end + + describe '#config' do + it 'has configuration attributes' do + expect(importer.config).to include('username' => an_instance_of(String), + 'password' => an_instance_of(String), + 'file_path' => an_instance_of(String)) + end + end + context "With a BePress exported csv", :clean do description = <<-HERE A far ultraviolet (UV) spectroscopic ellipsometer system working up to 9 eV has been developed and applied to characterize high-K- dielectric materials. These materials have been gaining greater attention as possible substitutes for Si02 as gate dielectrics in aggressively scaled silicon devices. The optical properties of representative high-K bulk crystalline, epitaxial, and amorphous films, were investigated with far UV spectroscopic ellipsometry and some by visible-near UV optical transmission measurements. Optical dielectric functions and optical band gap energies for these materials are obtained from these studies. The spectroscopic data and results provide information that is needed to select viable alternative dielectric candidate materials with adequate band gaps, and conduction and valence band offset energies for this application, and additionally to provide an optical metrology for gate dielectric films on silicon substrates. For materials with anisotropic structure such as single crystal DySC03and GdSc03 an analysis process was developed to determine the optical constant tensor.