bonsai-searchkick 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9bf2dab6cc21e84b90a14e6a13ab6458e254afc67f113e26e2796f12474212a
4
+ data.tar.gz: 470591003045ccfa8a9e32ccc0983045d2a8e8747802ecf4aff0daac700e581a
5
+ SHA512:
6
+ metadata.gz: e8360eba2d6511300e7938e8374913df2a98f9e3b9d1a4ffc0b6885d851a5a3e216a196b5e72f19f8e23b9c76e761a1702198d877f2f683799cf1ae56ff9360f
7
+ data.tar.gz: 0d5446066e2f7ecbe21ae7d5bd85e8647136f7b87c1a4b9498607fa06d1d7e19f78ceed67a4680e7ff5e2762ccca4eff27125d8c39168bda3e7bff66f76c50cc
@@ -0,0 +1,3 @@
1
+ if defined?(Rails)
2
+ require File.expand_path('bonsai/searchkick/railtie', File.dirname(__FILE__))
3
+ end
@@ -0,0 +1,105 @@
1
+ module Bonsai
2
+ module Searchkick
3
+ # Use Railties
4
+ class Railtie < ::Rails::Railtie
5
+ attr_reader :url, :port, :filtered_url, :ported_url, :logger
6
+
7
+ # Create an initializer to set up the Searchkick client with the proper URL
8
+ initializer 'setup_searchkick' do
9
+ if @url.present? && @url.is_a?(String) && @url.is_valid_searchkick_url?
10
+ @logger.debug("Bonsai: Initializing default Searchkick client with #{@filtered_url}")
11
+ ENV['ELASTICSEARCH_URL'] = @ported_url
12
+ else
13
+ @logger.debug('Bonsai: Neither BONSAI_URL nor ELASTICSEARCH_URL are set in this environment.')
14
+ @logger.debug('Bonsai: Proceeding with Searchkick default of http://localhost:9200.')
15
+ end
16
+ end
17
+
18
+ # Method to determine what happens when an instance of this class is
19
+ # initialized
20
+ def initialize
21
+
22
+ # Append a method to the String class so we can get a nice way to check
23
+ # that a String is a valid URL. `_searchkick_` is part of the method
24
+ # name to avoid possible collisions.
25
+ String.class_eval do
26
+ def is_valid_searchkick_url?
27
+ uri = URI.parse self
28
+ uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
29
+ rescue URI::InvalidURIError
30
+ false
31
+ end
32
+ end
33
+
34
+ # Class attributes
35
+ @logger = Logger.new(STDOUT)
36
+ @url = ENV['BONSAI_URL'] || ENV['ELASTICSEARCH_URL'] || ''
37
+ @port = searchkick_port
38
+ @ported_url = maybe_add_port
39
+ @filtered_url = filtered_url
40
+ end
41
+
42
+ # Check the URL for a port.
43
+ #
44
+ # Returns
45
+ # * An Integer representing the port, or nil if no port is given.
46
+ #
47
+ def searchkick_port
48
+ return nil unless @url.is_valid_searchkick_url?
49
+
50
+ port = @url[/:([0-9]{0,5}$)/, 1]
51
+ return port.to_i if port.present?
52
+ nil
53
+ end
54
+
55
+ # Append a port to the URL string if necessary.
56
+ #
57
+ # The Elasticsearch-ruby gem that Searchkick uses introduced a bug into
58
+ # its URL-parsing. Essentially, it will *always* assume port 9200,
59
+ # regardless of the protocol used. In other words, if you initialize
60
+ # the client with https://my-cluster.com, it will assume that you mean
61
+ # https://my-cluster.com:9200 and not https://my-cluster.com:443 (the
62
+ # standard port for HTTPS).
63
+ #
64
+ # This method will ensure the URL port is correctly called out when using
65
+ # a Bonsai cluster.
66
+ #
67
+ # Returns
68
+ # * A String for the URL, possibly appended with a Bonsai-supported
69
+ # port.
70
+ #
71
+ def maybe_add_port
72
+ if ENV['BONSAI_URL'].present?
73
+ uri = URI.parse(@url) rescue ''
74
+ if uri.kind_of?(URI::HTTPS) || uri.kind_of?(URI::HTTP)
75
+ port = (uri.kind_of?(URI::HTTPS)) ? 443 : 80
76
+ if !@port.present?
77
+ @logger.debug("Bonsai: Appending port #{port} to the cluster URL for production environment.")
78
+ return "#{@url}:#{port}".gsub('::', ':')
79
+ elsif @port != port
80
+ @logger.debug("Bonsai: Overriding the requested port #{@port} with the standard #{port} for production environment.")
81
+ return "#{@url.rpartition(':').first}:#{port}"
82
+ end
83
+ end
84
+ end
85
+ @url
86
+ end
87
+
88
+ # Filter out the credentials from the logs.
89
+ #
90
+ # This gem outputs some debug messages to indicate that it has
91
+ # successfully loaded and initialized the correct cluster. Cluster URLs
92
+ # can contain user credentials, which we do not want logged anywhere. This
93
+ # method filters out the password from the URL and replaces it with an
94
+ # anonymized value, making it safe to print.
95
+ #
96
+ # Returns
97
+ # * A String for the URL, with the credentials filtered for safe
98
+ # printing.
99
+ #
100
+ def filtered_url
101
+ @ported_url.sub(/:[^:@]+@/, ':FILTERED@')
102
+ end
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bonsai-searchkick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rob Sears
8
+ - Maddie Jones
9
+ - Leo Shue Schuster
10
+ - Nick Zadrozny
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2019-10-30 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: searchkick
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '99'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "<"
28
+ - !ruby/object:Gem::Version
29
+ version: '99'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: '1'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1'
44
+ - !ruby/object:Gem::Dependency
45
+ name: rake
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '11.0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "<"
56
+ - !ruby/object:Gem::Version
57
+ version: '11.0'
58
+ description: |2
59
+ This gem offers a shim to set up Searchkick to work with a Bonsai
60
+ Elasticsearch cluster. The bonsai-searchkick gem automatically sets up the
61
+ Searchkick client correctly so users don't need to worry about configuring it
62
+ in their code or writing an initializer. Further details and documentation
63
+ can be found on this gem's Github repository.
64
+ email:
65
+ - rob@onemorecloud.com
66
+ - maddie@onemorecloud.com
67
+ - leo@onemorecloud.com
68
+ - nick@onemorecloud.com
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - lib/bonsai-searchkick.rb
74
+ - lib/bonsai/searchkick/railtie.rb
75
+ homepage: https://github.com/omc/bonsai-searchkick
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Initialize your Searchkick client with Bonsai.
98
+ test_files: []