search-kit 0.0.1

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.
@@ -0,0 +1,31 @@
1
+ require 'forwardable'
2
+
3
+ module SearchKit
4
+ # The SearchKit logger, handled in its own class mainly for the purpose
5
+ # of allowing the daemonized process to quickly and cleanly reinitialize its
6
+ # connection to logfiles even after its process has been decoupled.
7
+ #
8
+ class Logger
9
+ extend Forwardable
10
+
11
+ attr_reader :logger
12
+
13
+ def_delegators :logger, :info, :warn
14
+
15
+ def initialize
16
+ environment = SearchKit.config.app_env
17
+
18
+ loginfo = [
19
+ SearchKit.config.app_dir,
20
+ SearchKit.config.log_dir,
21
+ "service-layer-#{environment}.log"
22
+ ]
23
+
24
+ logpath = File.join(*loginfo)
25
+ default = ::Logger.new(logpath, "daily")
26
+
27
+ @logger = SearchKit.config.logger || default
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,44 @@
1
+ require 'ansi'
2
+
3
+ module SearchKit
4
+ # The goal of the Messaging module is to provide an easy to include internal
5
+ # interface which will allow a SearchKit gem to dutifully log and provide
6
+ # output of what it's up to and how it may be doing.
7
+ #
8
+ module Messaging
9
+ def info(message)
10
+ Message.new(message).info
11
+ end
12
+
13
+ def warning(message)
14
+ Message.new(message).warn
15
+ end
16
+
17
+ private
18
+
19
+ # Most of the logic for the Messaging module exists in this (not so)
20
+ # private class. This lets more complex handling of message logic enter
21
+ # into the module gracefully, for example silence or logging level.
22
+ #
23
+ class Message
24
+ attr_reader :env, :feedback, :message
25
+
26
+ def initialize(message)
27
+ @env = SearchKit.config.app_env.to_s.ansi(:magenta)
28
+ @feedback = SearchKit.config.verbose
29
+ @message = message
30
+ end
31
+
32
+ def warn
33
+ Kernel.warn("--> [ #{env} ]: #{message.ansi(:red)}") if feedback
34
+ SearchKit.logger.warn message
35
+ end
36
+
37
+ def info
38
+ Kernel.puts("--> [ #{env} ]: #{message.ansi(:cyan)}") if feedback
39
+ SearchKit.logger.info message
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,29 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ module SearchKit
6
+ class Search
7
+ autoload :CLI, 'search_kit/search/cli'
8
+
9
+ attr_reader :connection
10
+
11
+ def initialize
12
+ @connection ||= SearchKit::Client.connection
13
+ end
14
+
15
+ def search(slug, options)
16
+ params = { data: { type: "searches", attributes: options } }
17
+ response = connection.post(slug, params)
18
+
19
+ body = JSON.parse(response.body, symbolize_names: true)
20
+
21
+ fail Errors::BadRequest if response.status == 400
22
+ fail Errors::IndexNotFound if response.status == 404
23
+ fail Errors::Unprocessable if response.status == 422
24
+
25
+ body
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require 'thor'
2
+
3
+ module SearchKit
4
+ class Search
5
+ class CLI < Thor
6
+ autoload :Actions, 'search_kit/search/cli/actions'
7
+
8
+ include Messaging
9
+
10
+ namespace :search
11
+
12
+ desc "search SLUG PHRASE", "Search an index".ansi(:cyan, :bold)
13
+ option :filters, aliases: ['-f'], type: :hash, required: false
14
+ option :display, aliases: ['-d'], type: :array, required: false
15
+ def search(slug, phrase)
16
+ search = Actions::Search.perform(
17
+ client: client,
18
+ phrase: phrase,
19
+ slug: slug
20
+ )
21
+
22
+ info "Searching `#{slug}` for titles matching `#{phrase}`:"
23
+ info " - Found #{search.results} titles in #{search.time}ms"
24
+
25
+ display = options.fetch('display', [])
26
+
27
+ search.documents.each do |document|
28
+ if display.any?
29
+ fields = display.map { |field| document.get(field) }
30
+ info " -- #{fields.join(' | ')} | score: #{document.score}"
31
+ else
32
+ info " -- #{document.id} | score: #{document.score}"
33
+ end
34
+ end
35
+ end
36
+
37
+ no_commands do
38
+ alias_method :s, :search
39
+ end
40
+
41
+ private
42
+
43
+ def client
44
+ @client ||= Client.new
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,104 @@
1
+ require 'thor'
2
+
3
+ module SearchKit
4
+ class Search
5
+ class CLI < Thor
6
+ module Actions
7
+ class Search
8
+ def self.perform(options = {})
9
+ new(options).perform
10
+ end
11
+
12
+ include Messaging
13
+
14
+ attr_reader :client, :slug, :phrase
15
+
16
+ def initialize(options = {})
17
+ @client = options.fetch(:client)
18
+ @slug = options.fetch(:slug)
19
+ @phrase = options.fetch(:phrase, nil)
20
+ end
21
+
22
+ def perform
23
+ SearchResult.new(search_response)
24
+ rescue Errors::IndexNotFound
25
+ warning "No resource for `#{slug}` found"
26
+ rescue Errors::BadRequest
27
+ warning "Some request parameters were not supplied"
28
+ rescue Errors::Unprocessable
29
+ warning "Search request unprocessable"
30
+ rescue Faraday::ConnectionFailed
31
+ warning "No running service found"
32
+ end
33
+
34
+ private
35
+
36
+ def search_response
37
+ client.search(slug, phrase: phrase)
38
+ end
39
+
40
+ class SearchResult
41
+ attr_reader :attributes
42
+
43
+ def initialize(response)
44
+ @attributes = response.fetch(:data, {}).fetch(:attributes, {})
45
+ end
46
+
47
+ def documents
48
+ list = attributes.fetch(:documents, [])
49
+ Documents.new(list)
50
+ end
51
+
52
+ def time
53
+ attributes.fetch(:time)
54
+ end
55
+
56
+ def results
57
+ attributes.fetch(:results)
58
+ end
59
+
60
+ class Documents
61
+ class Document
62
+ class AttributeNotFound < StandardError; end
63
+
64
+ attr_reader :attributes
65
+
66
+ def initialize(document_data)
67
+ @attributes = document_data.fetch(:attributes, {})
68
+ end
69
+
70
+ def id
71
+ get(:id)
72
+ end
73
+
74
+ def get(field)
75
+ attributes.fetch(field.to_sym)
76
+ rescue KeyError
77
+ fail AttributeNotFound, field
78
+ end
79
+
80
+ def score
81
+ get(:score)
82
+ end
83
+ end
84
+
85
+ include Enumerable
86
+
87
+ attr_reader :contents
88
+
89
+ def initialize(raw_list = [])
90
+ @contents = raw_list.map { |item| Document.new(item) }
91
+ end
92
+
93
+ def each(&block)
94
+ contents.each(&block)
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,20 @@
1
+ module SearchKit
2
+ def self.gem_version
3
+ Gem::Version.new(version_string)
4
+ end
5
+
6
+ def self.version_string
7
+ VERSION::STRING
8
+ end
9
+
10
+ # A convenience measure for easy and quick-to-read version changes and lookup.
11
+ #
12
+ module VERSION
13
+ MAJOR = 0
14
+ MINOR = 0
15
+ TINY = 1
16
+ PRE = nil
17
+
18
+ STRING = [ MAJOR, MINOR, TINY, PRE ].compact.join(".")
19
+ end
20
+ end
data/scripts/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "search_kit"
5
+ require "pry"
6
+
7
+ Pry.start
@@ -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 'search_kit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "search-kit"
8
+ spec.version = SearchKit.version_string
9
+ spec.authors = ["Joseph McCormick"]
10
+ spec.email = ["esmevane@gmail.com"]
11
+
12
+ spec.summary = %q{Client gem for search kit}
13
+ spec.description = %q{Build simple and powerful search quickly}
14
+ spec.homepage = "https://github.com/qbox-io/search-kit"
15
+ spec.license = "MIT"
16
+
17
+ spec.bindir = "bin"
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |file|
21
+ file.match(%r{^(test|spec|features)/})
22
+ end
23
+
24
+ spec.executables = spec.files.grep(%r{^bin/}) do |file|
25
+ File.basename(file)
26
+ end
27
+
28
+ spec.add_dependency "ansi", "~> 1.5"
29
+ spec.add_dependency "faraday", "~> 0.9"
30
+ spec.add_dependency "thor", "~> 0.19"
31
+ spec.add_dependency "i18n", "~> 0.7"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.8"
34
+ spec.add_development_dependency "pry", "~> 0.10"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.3"
37
+ spec.add_development_dependency "rack-test", "~> 0.6"
38
+ spec.add_development_dependency "simplecov", "~> 0.10"
39
+ spec.add_development_dependency "ruby-prof", "~> 0.15"
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,236 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search-kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joseph McCormick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ansi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.19'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.19'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-test
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.6'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.6'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.10'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.10'
153
+ - !ruby/object:Gem::Dependency
154
+ name: ruby-prof
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.15'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '0.15'
167
+ description: Build simple and powerful search quickly
168
+ email:
169
+ - esmevane@gmail.com
170
+ executables:
171
+ - search-kit
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".rubocop.yml"
178
+ - ".ruby-version"
179
+ - ".travis.yml"
180
+ - CODE_OF_CONDUCT.md
181
+ - Gemfile
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - bin/search-kit
186
+ - config/locales/en.yml
187
+ - lib/search_kit.rb
188
+ - lib/search_kit/cli.rb
189
+ - lib/search_kit/client.rb
190
+ - lib/search_kit/configuration.rb
191
+ - lib/search_kit/documents.rb
192
+ - lib/search_kit/documents/cli.rb
193
+ - lib/search_kit/errors.rb
194
+ - lib/search_kit/events.rb
195
+ - lib/search_kit/events/cli.rb
196
+ - lib/search_kit/events/cli/complete.rb
197
+ - lib/search_kit/events/cli/list.rb
198
+ - lib/search_kit/events/cli/pending.rb
199
+ - lib/search_kit/events/cli/publish.rb
200
+ - lib/search_kit/events/cli/status.rb
201
+ - lib/search_kit/events/publish.rb
202
+ - lib/search_kit/indices.rb
203
+ - lib/search_kit/indices/cli.rb
204
+ - lib/search_kit/logger.rb
205
+ - lib/search_kit/messaging.rb
206
+ - lib/search_kit/search.rb
207
+ - lib/search_kit/search/cli.rb
208
+ - lib/search_kit/search/cli/actions.rb
209
+ - lib/search_kit/version.rb
210
+ - scripts/console
211
+ - search-kit.gemspec
212
+ homepage: https://github.com/qbox-io/search-kit
213
+ licenses:
214
+ - MIT
215
+ metadata: {}
216
+ post_install_message:
217
+ rdoc_options: []
218
+ require_paths:
219
+ - lib
220
+ required_ruby_version: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ required_rubygems_version: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ requirements: []
231
+ rubyforge_project:
232
+ rubygems_version: 2.4.5
233
+ signing_key:
234
+ specification_version: 4
235
+ summary: Client gem for search kit
236
+ test_files: []