dmao-generic-ingesters 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ff720462095074f70e061c80005f6e7bb969962
4
+ data.tar.gz: 2c989fb7953deaa1d73c82e0ac23fa9e473acd25
5
+ SHA512:
6
+ metadata.gz: eeffc66009751b507fd0985c95f9b5d7fd16d88a354fa213c5e1fed07d0d7f87645adf7217a857cb62a53c9c7cdb03eef25a6383319a31b6337bcaf6a57ceeee
7
+ data.tar.gz: c04021beee5c6db06d5025e0f11100c62656fa7d7d8e0752868e191f318b02ea559026fe1f0fff8f32b77552a11faeb80a239f09e0b42856267f95e443264b64
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rbenv-gemsets
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dmao-generic-ingesters.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Lancaster University Library
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # DMAO Generic Ingesters
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'dmao-generic-ingesters'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dmao-generic-ingesters
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lulibrary/DMAO-Generic-Ingesters.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "generic/ingesters"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dmao/ingesters/generic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dmao-generic-ingesters"
8
+ spec.version = DMAO::Ingesters::Generic::VERSION
9
+ spec.authors = ["Stephen Robinson"]
10
+ spec.email = ["library.dit@lancaster.ac.uk"]
11
+
12
+ spec.summary = %q{Generic ingesters for DMA Online}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/lulibrary/DMAO-Generic-Ingesters"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "dmao_api", "~> 0.1.0"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ spec.add_development_dependency "mocha", "~> 1.1"
39
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
40
+ spec.add_development_dependency "byebug"
41
+ end
@@ -0,0 +1,15 @@
1
+ module DMAO
2
+ module Ingesters
3
+ module Errors
4
+
5
+ class EmptyAttributes < StandardError
6
+
7
+ def initialize(msg="Attributes need to be specified.")
8
+ super(msg)
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module DMAO
2
+ module Ingesters
3
+ module Errors
4
+
5
+ class GenericIngester < StandardError
6
+
7
+ def initialize(msg="Calling ingest in generic ingester is not allowed")
8
+ super(msg)
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module DMAO
2
+ module Ingesters
3
+ module Errors
4
+
5
+ class IngestOrganisationUnitError < StandardError
6
+
7
+ def initialize(msg="Error ingesting organisation unit.")
8
+ super(msg)
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module DMAO
2
+ module Ingesters
3
+ module Errors
4
+
5
+ class LinkOrganisationUnitError < StandardError
6
+
7
+ def initialize(msg="Error linking organisation unit.")
8
+ super(msg)
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,124 @@
1
+ require 'dmao/ingesters/errors/generic_ingester'
2
+ require 'dmao/ingesters/errors/empty_attributes'
3
+ require 'dmao/ingesters/errors/ingest_organisation_unit_error'
4
+ require 'dmao/ingesters/errors/link_organisation_unit_error'
5
+ require 'dmao_api'
6
+ require 'time'
7
+
8
+ module DMAO
9
+ module Ingesters
10
+ module Generic
11
+
12
+ class OrganisationIngester
13
+
14
+ def initialize(api_url=nil, api_token=nil, institution_id=nil)
15
+ DMAO::API.configure do |config|
16
+ config.base_url = api_url
17
+ config.api_token = api_token
18
+ config.institution_id = institution_id
19
+ end
20
+ end
21
+
22
+ def ingest
23
+ raise DMAO::Ingesters::Errors::GenericIngester.new("Calling ingest on generic organisation ingester is not allowed.")
24
+ end
25
+
26
+ def ingest_organisation_unit attributes={}
27
+
28
+ raise DMAO::Ingesters::Errors::EmptyAttributes.new("Cannot ingest organisation unit without attributes.") if attributes.nil? || attributes.empty?
29
+
30
+ begin
31
+
32
+ org_unit = DMAO::API::OrganisationUnit.find_by_system_uuid attributes[:system_uuid]
33
+
34
+ current_modified_at = Time.parse(org_unit.system_modified_at)
35
+ new_modified_at = Time.parse(attributes[:system_modified_at])
36
+
37
+ return false if current_modified_at >= new_modified_at
38
+
39
+ update_organisation_unit org_unit.id, attributes
40
+
41
+ rescue DMAO::API::Errors::OrganisationUnitNotFound
42
+ add_organisation_unit attributes
43
+ rescue DMAO::API::Errors::InvalidResponseLength
44
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Multiple organisation units returned for system uuid #{attributes[:system_uuid]}.")
45
+ end
46
+
47
+ end
48
+
49
+ def link_child_to_parent child_system_uuid, parent_system_uuid
50
+
51
+ child_unit = find_unit_or_raise_link_error child_system_uuid, "Organisation unit not found for child system uuid #{child_system_uuid}"
52
+ parent_unit = find_unit_or_raise_link_error parent_system_uuid, "Organisation unit not found for parent system uuid #{parent_system_uuid}"
53
+
54
+ attributes = {
55
+ parent_id: parent_unit.id
56
+ }
57
+
58
+ update_organisation_unit child_unit.id, attributes
59
+
60
+ end
61
+
62
+ private
63
+
64
+ def add_organisation_unit attributes
65
+
66
+ begin
67
+ DMAO::API::OrganisationUnit.create attributes
68
+ rescue DMAO::API::Errors::InstitutionNotFound
69
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Institution not found, cannot ingest organisation unit to non-existent institution")
70
+ rescue DMAO::API::Errors::InvalidParentId
71
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent")
72
+ rescue DMAO::API::Errors::InvalidOrganisationUnit => e
73
+ parse_unprocessable_errors(e.errors)
74
+ end
75
+
76
+ end
77
+
78
+ def update_organisation_unit id, attributes
79
+
80
+ begin
81
+
82
+ DMAO::API::OrganisationUnit.update id, attributes
83
+
84
+ rescue DMAO::API::Errors::OrganisationUnitNotFound
85
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Organisation unit not found, cannot update organisation unit that does not exist")
86
+ rescue DMAO::API::Errors::InvalidParentId
87
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Parent ID not found in DMA Online, cannot link organisation unit to non-existent parent")
88
+ rescue DMAO::API::Errors::InvalidOrganisationUnit => e
89
+ parse_unprocessable_errors(e.errors)
90
+ end
91
+
92
+ end
93
+
94
+ def parse_unprocessable_errors errors
95
+
96
+ error_messages = ""
97
+
98
+ errors.each_with_index do |(k, v), index|
99
+ v.each_with_index do |msg, msg_index|
100
+ error_messages += "#{k} - #{msg}"
101
+ error_messages += ", " unless msg_index == v.size - 1
102
+ end
103
+ error_messages += ", " unless index == errors.size - 1
104
+ end
105
+
106
+ raise DMAO::Ingesters::Errors::IngestOrganisationUnitError.new("Invalid organisation unit details, #{error_messages}")
107
+
108
+ end
109
+
110
+ def find_unit_or_raise_link_error unit_system_uuid, error_message
111
+
112
+ begin
113
+ DMAO::API::OrganisationUnit.find_by_system_uuid unit_system_uuid
114
+ rescue DMAO::API::Errors::OrganisationUnitNotFound
115
+ raise DMAO::Ingesters::Errors::LinkOrganisationUnitError.new(error_message)
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,7 @@
1
+ module DMAO
2
+ module Ingesters
3
+ module Generic
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require "dmao/ingesters/generic/version"
2
+
3
+ module DMAO
4
+ module Ingesters
5
+ module Generic
6
+
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dmao-generic-ingesters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Robinson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dmao_api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - library.dit@lancaster.ac.uk
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - bin/console
124
+ - bin/setup
125
+ - dmao-generic-ingesters.gemspec
126
+ - lib/dmao/ingesters/errors/empty_attributes.rb
127
+ - lib/dmao/ingesters/errors/generic_ingester.rb
128
+ - lib/dmao/ingesters/errors/ingest_organisation_unit_error.rb
129
+ - lib/dmao/ingesters/errors/link_organisation_unit_error.rb
130
+ - lib/dmao/ingesters/generic.rb
131
+ - lib/dmao/ingesters/generic/organisation_ingester.rb
132
+ - lib/dmao/ingesters/generic/version.rb
133
+ homepage: https://github.com/lulibrary/DMAO-Generic-Ingesters
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ allowed_push_host: https://rubygems.org
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.5.1
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Generic ingesters for DMA Online
158
+ test_files: []