rooftop-algolia_search 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: 2352190bf7c0ae24a264a77bb4a15cacc26fbad2
4
+ data.tar.gz: 169bbb5a16326e5f45c116691730beacf206f34b
5
+ SHA512:
6
+ metadata.gz: b23dcb04f5a86076523e92f3226c7647e99c23b901817d351884db536759c207ef93b5063e2e83b83c2875b171e22155d91ae0e270a09e4387e14f80bb284dd1
7
+ data.tar.gz: 921ef5793173242b07ae075b3745f22333dfd5e2b2c6d5519bdfa446c601377a9f9efcba08c9ad7114f0c1e8be592369d6919783954df102c9dea6a0bda9daa7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rooftop-algolia_search
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ed@errorstudio.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rooftop-algoliasearch.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ed Jones
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,21 @@
1
+ # Rooftop::AlgoliaSearch
2
+ Index and search your Rooftop post types in Algolia.
3
+
4
+ ## Setup
5
+ To set up, you need:
6
+
7
+ * an [algolia.com](https://www.algolia.com) account
8
+ * an API key with permissions to write, update and delete items in the index - note that this [should not be your admin key](https://www.algolia.com/doc/guides/security/api-keys/?language=php#admin-api-key)
9
+ * an API key with permissions only to search
10
+ * your application ID
11
+
12
+ ```ruby
13
+ Rooftop::AlgoliaSearch.configure do |config|
14
+ config.index_api_key = "your index api key"
15
+ config.search_api_key = "your search api key"
16
+ config.application_id = "your application id"
17
+ end
18
+ ```
19
+
20
+ ## Use
21
+ Include `Rooftop::AlgoliaSearch` in your Rooftop models.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ module Rooftop
2
+ module AlgoliaSearch
3
+ module FacetHelper
4
+
5
+ def build_facet_query(hash)
6
+ # takes a hash like this:
7
+ # {
8
+ # foo: ['bar', 'baz'],
9
+ # qux: ['boo']
10
+ #
11
+ # }
12
+
13
+ # returns this
14
+ # [["foo:bar", "foo:baz"],"qux:boo"]
15
+ hash.collect do |k,v|
16
+ if v.is_a?(Array)
17
+ v.collect {|val| "#{k}:#{val}"}
18
+ elsif v.is_a?(String) || v.is_a?(Symbol)
19
+ "#{k}:#{v}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rooftop/algoliasearch"
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,21 @@
1
+ module Rooftop
2
+ module AlgoliaSearch
3
+ class Engine < ::Rails::Engine
4
+
5
+ isolate_namespace Rooftop::AlgoliaSearch
6
+
7
+ initializer "add_helpers" do
8
+ ActiveSupport.on_load(:action_controller) do
9
+ include Rooftop::AlgoliaSearch::FacetHelper
10
+ end
11
+ end
12
+
13
+ rake_tasks do
14
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
15
+ end
16
+
17
+
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,119 @@
1
+ module Rooftop
2
+ module AlgoliaSearch
3
+ module PostIndexing
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+
8
+ base.send :setup_index_name!
9
+
10
+ base.send :after_save, :reindex!
11
+ base.send :after_destroy, :deindex!
12
+
13
+ end
14
+
15
+ module ClassMethods
16
+ attr_reader :search_index_name, :search_index_settings, :search_fields
17
+
18
+ def search_index_name=(name)
19
+ @search_index_name = name
20
+ setup_index_name!
21
+ end
22
+
23
+ def search_index_settings=(settings)
24
+ @search_index_settings ||= settings
25
+ end
26
+
27
+ def setup_index_settings!
28
+ search_index.set_settings(@search_index_settings) unless @search_index_settings.nil?
29
+ end
30
+
31
+ def setup_index_name!
32
+ @search_index_name ||= self.to_s.underscore
33
+ end
34
+
35
+
36
+ def add_search_fields(*fields)
37
+ @search_fields ||= []
38
+ fields.each {|f| @search_fields << f}
39
+ end
40
+
41
+ def add_search_field(field, options = nil)
42
+ @search_fields ||= []
43
+
44
+ @search_fields << [field, options].compact
45
+ end
46
+
47
+ def search_fields
48
+ @search_fields ||= []
49
+ end
50
+
51
+ def reindex_all
52
+ reindex_entities(all.to_a)
53
+ end
54
+
55
+ def reindex_entities(entities)
56
+ entities = Array.wrap(entities)
57
+ search_index.add_objects(entities.collect(&:to_search_index_parameters))
58
+ end
59
+
60
+ alias_method :reindex_entity, :reindex_entities
61
+
62
+ def deindex_entities(entities)
63
+ entities = Array.wrap(entities)
64
+ search_index.delete_objects(entities.collect(&:id))
65
+ end
66
+
67
+ alias_method :deindex_entity, :deindex_entities
68
+
69
+ def clear_index!
70
+ search_index.clear_index
71
+ end
72
+
73
+
74
+ def search_index
75
+ @search_index ||= Algolia::Index.new(@search_index_name)
76
+ end
77
+
78
+ end
79
+
80
+ # Reindex an instance
81
+ def reindex!
82
+ # only reindex things which are published
83
+ if status == 'publish'
84
+ self.class.reindex_entity(self)
85
+ else
86
+ deindex!
87
+ end
88
+ end
89
+
90
+ alias :index! :reindex!
91
+
92
+ def deindex!
93
+ self.class.deindex_entity(self)
94
+ end
95
+
96
+ def to_search_index_parameters
97
+ self.class.search_fields.inject({}) do |hash, fields|
98
+ hash[:objectID] = self.id
99
+
100
+ if fields.last.is_a? Proc
101
+ hash[fields.first] = fields.last.call(self)
102
+ else
103
+ fields.each do |field|
104
+ begin
105
+ hash[field] = self.send(field)
106
+ rescue NoMethodError
107
+ if self.has_field?(field)
108
+ hash[field] = self.fields.send(field)
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ hash
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,20 @@
1
+ module Rooftop
2
+ module AlgoliaSearch
3
+ module PostSearching
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def search(query, opts = {})
13
+ opts = opts.with_indifferent_access
14
+ search_index.search(query, opts).with_indifferent_access
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,49 @@
1
+ namespace :rooftop do
2
+ namespace :algolia_search do
3
+
4
+ desc "Initialise index settings for all models which include Rooftop::AlgoliaSearch"
5
+ task initialize_indexes: :environment do
6
+ puts "Resetting index settings, which might have been overwritten in the Algolia UI - are you sure?"
7
+ puts "Please enter Y, y, N or n"
8
+ STDOUT.flush
9
+ input = STDIN.gets.chomp
10
+ case input.upcase
11
+ when "Y"
12
+ puts "Continuing"
13
+ else
14
+ puts "Cancelled"
15
+ next
16
+ end
17
+ ::Rails.application.eager_load!
18
+ Rooftop::AlgoliaSearch.included_classes.each do |klass|
19
+ puts "Setting index settings for #{klass}"
20
+ klass.setup_index_settings!
21
+ end
22
+ end
23
+
24
+ desc "Index all searchable entities"
25
+ task reindex: :environment do
26
+ puts "Reindexing all - are you sure?"
27
+ puts "Please enter Y, y, N or n"
28
+ STDOUT.flush
29
+ input = STDIN.gets.chomp
30
+ case input.upcase
31
+ when "Y"
32
+ puts "Continuing"
33
+ else
34
+ puts "Cancelled"
35
+ next
36
+ end
37
+
38
+ ::Rails.application.eager_load!
39
+ Rooftop::AlgoliaSearch.included_classes.each do |klass|
40
+ puts "Reindexing #{klass}"
41
+ klass.search_index.clear
42
+ klass.reindex_entities(klass.all)
43
+ puts "Done\n"
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+
@@ -0,0 +1,5 @@
1
+ module Rooftop
2
+ module AlgoliaSearch
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ require 'algoliasearch'
2
+ require "rooftop/algolia_search/version"
3
+ require "rooftop/algolia_search/post_indexing"
4
+ require "rooftop/algolia_search/post_searching"
5
+ if defined?(Rails)
6
+ require "rooftop/algolia_search/engine"
7
+ end
8
+
9
+ module Rooftop
10
+ module AlgoliaSearch
11
+ class << self
12
+ attr_accessor :configuration
13
+ def configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ self.configuration.configure_connection
17
+ end
18
+ end
19
+
20
+ def self.included(base)
21
+ @included_classes ||= []
22
+ @included_classes << base unless @included_classes.include?(base)
23
+ base.include PostIndexing
24
+ base.include PostSearching
25
+ end
26
+
27
+ class Configuration
28
+ attr_accessor :index_api_key,
29
+ :search_api_key,
30
+ :application_id,
31
+ :settings
32
+
33
+ attr_reader :index_connection, :search_connection
34
+
35
+ def configure_connection
36
+ ::Algolia.init( application_id: @application_id,
37
+ api_key: @index_api_key)
38
+ end
39
+ end
40
+
41
+ class Error < StandardError
42
+
43
+ end
44
+
45
+ def self.included_classes
46
+ @included_classes
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rooftop/algolia_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rooftop-algolia_search"
8
+ spec.version = Rooftop::AlgoliaSearch::VERSION
9
+ spec.authors = ["Ed Jones", "Paul Hendrick"]
10
+ spec.email = ["ed@error.agency", "paul@error.agency"]
11
+
12
+ spec.summary = %q{Search your Rooftop models using Algolia from your Ruby applications}
13
+ spec.homepage = "https://github.com/rooftopcms/rooftop-ruby-algolia_search"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_dependency "algoliasearch", "~> 1"
27
+ spec.add_dependency "rooftop", "~> 0.1"
28
+ spec.add_dependency "activesupport", ">= 4.2", "< 6"
29
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rooftop-algolia_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Jones
8
+ - Paul Hendrick
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-10-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.13'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.13'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: algoliasearch
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rooftop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.1'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.1'
70
+ - !ruby/object:Gem::Dependency
71
+ name: activesupport
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '4.2'
77
+ - - "<"
78
+ - !ruby/object:Gem::Version
79
+ version: '6'
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '4.2'
87
+ - - "<"
88
+ - !ruby/object:Gem::Version
89
+ version: '6'
90
+ description:
91
+ email:
92
+ - ed@error.agency
93
+ - paul@error.agency
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - ".gitignore"
99
+ - ".ruby-gemset"
100
+ - ".ruby-version"
101
+ - CODE_OF_CONDUCT.md
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - app/helpers/rooftop/algolia_search/facet_helper.rb
107
+ - bin/console
108
+ - bin/setup
109
+ - lib/rooftop/algolia_search.rb
110
+ - lib/rooftop/algolia_search/engine.rb
111
+ - lib/rooftop/algolia_search/post_indexing.rb
112
+ - lib/rooftop/algolia_search/post_searching.rb
113
+ - lib/rooftop/algolia_search/tasks/algolia_search.rake
114
+ - lib/rooftop/algolia_search/version.rb
115
+ - rooftop-algolia_search.gemspec
116
+ homepage: https://github.com/rooftopcms/rooftop-ruby-algolia_search
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.12
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Search your Rooftop models using Algolia from your Ruby applications
140
+ test_files: []