common_indexer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7bf05a42fb84387c676ec66151e7521157a318f57415d212d03e715794bf7179
4
+ data.tar.gz: 6f6d6a1bd2d3b73fb880d1a4da047c0008428e4f6262d252b9e5fb1342fadac7
5
+ SHA512:
6
+ metadata.gz: f7900c1a950f4cc0b45b2d93468d4f082f7aa7dc52106a3381d43afac16344eb2c45892e204806d7c062e13819e7c49e8ee2dea59fb063a147017a7b88670541
7
+ data.tar.gz: 119b0bd45cef5f97c1c249ccd7650cc2f797380668088032f69a4a7d2350d23cd8002ea78bfd9f69525674080ad5da92676ea0ec1fc51170092102efedc85b9b
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ inherit_gem:
2
+ bixby: bixby_default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.5.1
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'spec/**/*'
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.2
6
+ cache: bundler
7
+ env:
8
+ global:
9
+ - environment=test
10
+ script:
11
+ - 'bundle exec rake'
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in common_indexer.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,15 @@
1
+ ##########################################################################
2
+ # Copyright 2017 Northwestern University Libraries
3
+ # Additional copyright may be held by others, as reflected in the commit log
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # CommonIndexer
2
+ [![Build Status](https://travis-ci.com/nulib/common-indexer.svg?branch=master)](https://travis-ci.com/nulib/common-indexer)
3
+
4
+ Indexes metadata into a central AWS Elasticsearch instance. The gem indexes based on a hash returned by the method `#to_common_index` defined in your model using an `after_save` hook. The indexer also sanitizes the input by only allowing pre-configured hash keys.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'common_indexer', github: 'nulib/common-indexer', branch: 'master'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install common_indexer
21
+
22
+ ## Configuration
23
+
24
+ Add `common_indexer` to the appropriate Rails config file (e.g. `config/settings.yml`, `config/settings/development.yml`):
25
+
26
+ ```yml
27
+ # config/settings/development.yml
28
+ common_indexer:
29
+ endpoint: http://localhost:9201/ # default 'http://localhost:9200'
30
+ index_name: new-index # default 'common'
31
+ allowed_keys: # default ['title']
32
+ - title
33
+ - creator
34
+ ```
35
+
36
+ Add an initializer to configure the CommonIndexer gem with the app settings:
37
+
38
+ ```ruby
39
+ # config/initializers/common_indexer_config.rb
40
+ ::CommonIndexer.config do |config|
41
+ config.endpoint = Settings.common_indexer.endpoint
42
+ config.index_name = Settings.common_indexer.index_name
43
+ config.allowed_keys = Settings.common_indexer.allowed_keys
44
+ end
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Include the CommonIndexer into your model that you want to index with `include ::CommonIndexer::Base`, and define a `#to_common_index` method in that model. `#to_common_index` should return a hash with metadata key/values that conform to the allowed keys defined in the local configuration or fall back to the defaults.
50
+
51
+ ```ruby
52
+ class Example < ActiveFedora::Base
53
+ include ::CommonIndexer::Base
54
+
55
+ property :title, ::RDF::URI('http://example.org/ns#title')
56
+ property :creator, ::RDF::URI('http://example.org/ns#creator')
57
+
58
+ def to_common_index
59
+ {
60
+ title: title,
61
+ creator: creator
62
+ }
63
+ end
64
+ end
65
+ ```
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ 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).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/common_indexer.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ RuboCop::RakeTask.new(:rubocop) do |task|
10
+ task.fail_on_error = true
11
+ end
12
+
13
+ task :ci do
14
+ Rake::Task[:rubocop].invoke
15
+ Rake::Task[:spec].invoke
16
+ end
17
+
18
+ task default: :ci
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'common_indexer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'common_indexer/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'common_indexer'
9
+ spec.version = CommonIndexer::VERSION
10
+ spec.authors = ['Michael Klein', 'Brendan Quinn']
11
+ spec.email = ['mbklein@gmail.com', 'brendan-quinn@northwestern.edu']
12
+
13
+ spec.summary = 'Common Indexing mixin for NUL Digital Collections'
14
+ spec.description = 'Common Indexing mixin for NUL Digital Collections'
15
+ spec.homepage = 'https://github.com/nulib/common-indexer'
16
+ spec.license = 'Apache2'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'active-fedora'
24
+ spec.add_dependency 'activesupport'
25
+ spec.add_dependency 'typhoeus'
26
+ spec.add_dependency 'elasticsearch'
27
+ spec.add_development_dependency 'bixby'
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'pry-byebug'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec'
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'typhoeus'
4
+ require 'typhoeus/adapters/faraday'
5
+ require 'elasticsearch'
6
+ require 'common_indexer/version'
7
+ require 'common_indexer/base'
8
+ require 'common_indexer/config'
9
+
10
+ module CommonIndexer # :nodoc:
11
+ class Error < StandardError; end
12
+
13
+ def self.client
14
+ @client ||= Elasticsearch::Client.new(hosts: config.endpoint)
15
+ end
16
+
17
+ def self.index_name
18
+ config.index_name
19
+ end
20
+
21
+ def self.sanitize(input)
22
+ input.select do |key, _value|
23
+ config.allowed_keys.include?(key.to_s)
24
+ end
25
+ end
26
+
27
+ def self.config
28
+ (@config ||= Config.new).tap { |c| yield c if block_given? }
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/concern'
3
+
4
+ module CommonIndexer # :nodoc:
5
+ module Base
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ after_save :update_common_index
10
+ end
11
+
12
+ # Override to_common_index in the including model and return a hash of common index attributes
13
+ def to_common_index
14
+ raise CommonIndexer::Error, "Index Error: #{self.class.name} does not implement #to_common_index!"
15
+ end
16
+
17
+ def update_common_index
18
+ CommonIndexer.client.index index: CommonIndexer.index_name,
19
+ id: id,
20
+ type: self.class.name.underscore,
21
+ body: CommonIndexer.sanitize(to_common_index)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommonIndexer
4
+ DEFAULT_KEYS = ['title'].freeze
5
+ DEFAULT_ENDPOINT = 'http://localhost:9200/'
6
+ DEFAULT_INDEX_NAME = 'common'
7
+
8
+ Config = Struct.new(:endpoint, :index_name, :allowed_keys) do
9
+ def initialize(endpoint = DEFAULT_ENDPOINT, index_name = DEFAULT_INDEX_NAME, allowed_keys = DEFAULT_KEYS)
10
+ super
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommonIndexer
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: common_indexer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Klein
8
+ - Brendan Quinn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active-fedora
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: typhoeus
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: elasticsearch
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bixby
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: bundler
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.16'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.16'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry-byebug
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rake
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '10.0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '10.0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ description: Common Indexing mixin for NUL Digital Collections
141
+ email:
142
+ - mbklein@gmail.com
143
+ - brendan-quinn@northwestern.edu
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - ".rubocop.yml"
151
+ - ".travis.yml"
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - common_indexer.gemspec
159
+ - lib/common_indexer.rb
160
+ - lib/common_indexer/base.rb
161
+ - lib/common_indexer/config.rb
162
+ - lib/common_indexer/version.rb
163
+ homepage: https://github.com/nulib/common-indexer
164
+ licenses:
165
+ - Apache2
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.7.3
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Common Indexing mixin for NUL Digital Collections
187
+ test_files: []