search-kit 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/config/locales/en.yml +112 -3
  4. data/lib/search_kit.rb +5 -6
  5. data/lib/search_kit/cli.rb +45 -10
  6. data/lib/search_kit/cli/documents.rb +75 -0
  7. data/lib/search_kit/cli/events.rb +88 -0
  8. data/lib/search_kit/cli/indices.rb +96 -0
  9. data/lib/search_kit/cli/search.rb +60 -0
  10. data/lib/search_kit/cli/subscribers.rb +45 -0
  11. data/lib/search_kit/clients.rb +12 -0
  12. data/lib/search_kit/clients/documents.rb +71 -0
  13. data/lib/search_kit/clients/events.rb +77 -0
  14. data/lib/search_kit/clients/indices.rb +70 -0
  15. data/lib/search_kit/clients/keys.rb +78 -0
  16. data/lib/search_kit/clients/populate.rb +72 -0
  17. data/lib/search_kit/clients/scaffold.rb +36 -0
  18. data/lib/search_kit/clients/search.rb +33 -0
  19. data/lib/search_kit/clients/subscribers.rb +54 -0
  20. data/lib/search_kit/configuration.rb +0 -1
  21. data/lib/search_kit/errors.rb +4 -1
  22. data/lib/search_kit/messages.rb +41 -0
  23. data/lib/search_kit/messages/messaging.rb +64 -0
  24. data/lib/search_kit/models.rb +14 -0
  25. data/lib/search_kit/models/document.rb +30 -0
  26. data/lib/search_kit/models/documents.rb +31 -0
  27. data/lib/search_kit/models/event.rb +18 -0
  28. data/lib/search_kit/models/events.rb +31 -0
  29. data/lib/search_kit/models/key.rb +26 -0
  30. data/lib/search_kit/models/keys.rb +38 -0
  31. data/lib/search_kit/models/search.rb +17 -0
  32. data/lib/search_kit/models/subscriber.rb +26 -0
  33. data/lib/search_kit/polling.rb +30 -0
  34. data/lib/search_kit/polling/process.rb +40 -0
  35. data/lib/search_kit/thor.rb +12 -0
  36. data/lib/search_kit/version.rb +1 -1
  37. data/search-kit.gemspec +2 -0
  38. metadata +58 -20
  39. data/lib/search_kit/documents.rb +0 -59
  40. data/lib/search_kit/documents/cli.rb +0 -70
  41. data/lib/search_kit/events.rb +0 -59
  42. data/lib/search_kit/events/cli.rb +0 -52
  43. data/lib/search_kit/events/cli/complete.rb +0 -34
  44. data/lib/search_kit/events/cli/list.rb +0 -48
  45. data/lib/search_kit/events/cli/pending.rb +0 -48
  46. data/lib/search_kit/events/cli/publish.rb +0 -42
  47. data/lib/search_kit/events/cli/status.rb +0 -43
  48. data/lib/search_kit/events/poll.rb +0 -32
  49. data/lib/search_kit/events/poll/process.rb +0 -42
  50. data/lib/search_kit/events/publish.rb +0 -48
  51. data/lib/search_kit/indices.rb +0 -58
  52. data/lib/search_kit/indices/cli.rb +0 -65
  53. data/lib/search_kit/messaging.rb +0 -44
  54. data/lib/search_kit/search.rb +0 -30
  55. data/lib/search_kit/search/cli.rb +0 -49
  56. data/lib/search_kit/search/cli/actions.rb +0 -104
@@ -0,0 +1,60 @@
1
+ require 'thor'
2
+ require 'search_kit/thor'
3
+
4
+ module SearchKit
5
+ class CLI < Thor
6
+ class Search < Thor
7
+ namespace :search
8
+
9
+ no_commands do
10
+ def client
11
+ @client ||= Search.new
12
+ end
13
+
14
+ def messages
15
+ @messages ||= Messages.new
16
+ end
17
+ end
18
+
19
+ document :create
20
+ option :display, aliases: ['-d'], type: :array, required: false
21
+ def create(slug, phrase)
22
+ search = client.search(slug, phrase: phrase)
23
+ head_path = 'cli.search.create.success.headline'
24
+ info_path = 'cli.search.create.success.info'
25
+ headline = I18n.t(head_path, slug: slug, phrase: phrase)
26
+ info = I18n.t(info_path, count: search.results, time: search.time)
27
+ lines = [ headline, info ]
28
+ display = options.fetch('display', [])
29
+
30
+ lines += search.documents.map do |document|
31
+ if display.any?
32
+ fields = display.map { |field| document.get(field) }
33
+ " -- #{fields.join(' | ')} | score: #{document.score}"
34
+ else
35
+ " -- #{document.id} | score: #{document.score}"
36
+ end
37
+ end
38
+
39
+ lines.each(&messages.method(:info))
40
+ rescue Errors::Unauthorized
41
+ messages.unauthorized
42
+ rescue Errors::IndexNotFound
43
+ messages.not_found
44
+ rescue Errors::BadRequest
45
+ messages.bad_request
46
+ rescue Errors::Unprocessable
47
+ messages.unprocessable
48
+ rescue Faraday::ConnectionFailed
49
+ messages.no_service
50
+ end
51
+
52
+ no_commands do
53
+ alias_method :search, :create
54
+ alias_method :c, :create
55
+ alias_method :s, :search
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,45 @@
1
+ require 'thor'
2
+ require 'search_kit/thor'
3
+
4
+ module SearchKit
5
+ class CLI < Thor
6
+ class Subscribers < Thor
7
+ namespace :subscribers
8
+
9
+ no_commands do
10
+ def client
11
+ @client ||= SearchKit::Clients::Subscribers.new
12
+ end
13
+
14
+ def messages
15
+ @messages ||= Messages.new
16
+ end
17
+ end
18
+
19
+ document :create
20
+ def create(email, password)
21
+ subscriber = client.create(email: email, password: password)
22
+ messages.info(subscriber.to_json)
23
+ rescue Errors::BadRequest
24
+ messages.bad_request
25
+ rescue Errors::Unprocessable
26
+ messages.unprocessable
27
+ rescue Faraday::ConnectionFailed
28
+ messages.no_service
29
+ end
30
+
31
+ document :info
32
+ def info
33
+ subscriber = client.info
34
+ messages.info(subscriber.to_json)
35
+ rescue Errors::SubscriberNotFound
36
+ messages.not_found
37
+ rescue Errors::Unauthorized
38
+ messages.unauthorized
39
+ rescue Faraday::ConnectionFailed
40
+ messages.no_service
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ module SearchKit
2
+ module Clients
3
+ autoload :Documents, 'search_kit/clients/documents'
4
+ autoload :Events, 'search_kit/clients/events'
5
+ autoload :Indices, 'search_kit/clients/indices'
6
+ autoload :Keys, 'search_kit/clients/keys'
7
+ autoload :Populate, 'search_kit/clients/populate'
8
+ autoload :Scaffold, 'search_kit/clients/scaffold'
9
+ autoload :Search, 'search_kit/clients/search'
10
+ autoload :Subscribers, 'search_kit/clients/subscribers'
11
+ end
12
+ end
@@ -0,0 +1,71 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SearchKit
5
+ module Clients
6
+ class Documents
7
+ attr_reader :connection, :token
8
+
9
+ def initialize
10
+ uri = [SearchKit.config.app_uri, "documents"].join("/")
11
+ @connection = Faraday.new(uri)
12
+ @token = SearchKit.config.app_token
13
+ end
14
+
15
+ def create(slug, options)
16
+ document = {
17
+ token: token,
18
+ data: { type: "documents", attributes: options }
19
+ }
20
+
21
+ response = connection.post(slug, document)
22
+ body = JSON.parse(response.body, symbolize_names: true)
23
+
24
+ fail Errors::BadRequest if response.status == 400
25
+ fail Errors::Unauthorized if response.status == 401
26
+ fail Errors::IndexNotFound if response.status == 404
27
+ fail Errors::Unprocessable if response.status == 422
28
+
29
+ body
30
+ end
31
+
32
+ def delete(slug, id)
33
+ response = connection.delete("#{slug}/#{id}", token: token)
34
+ body = JSON.parse(response.body, symbolize_names: true)
35
+
36
+ fail Errors::Unauthorized if response.status == 401
37
+ fail Errors::IndexNotFound if response.status == 404
38
+
39
+ body
40
+ end
41
+
42
+ def show(slug, id)
43
+ response = connection.get("#{slug}/#{id}", token: token)
44
+ body = JSON.parse(response.body, symbolize_names: true)
45
+
46
+ fail Errors::Unauthorized if response.status == 401
47
+ fail Errors::IndexNotFound if response.status == 404
48
+
49
+ body
50
+ end
51
+
52
+ def update(slug, id, options)
53
+ document = {
54
+ token: token,
55
+ data: { type: "documents", id: id, attributes: options }
56
+ }
57
+
58
+ response = connection.patch("#{slug}/#{id}", document)
59
+ body = JSON.parse(response.body, symbolize_names: true)
60
+
61
+ fail Errors::BadRequest if response.status == 400
62
+ fail Errors::Unauthorized if response.status == 401
63
+ fail Errors::IndexNotFound if response.status == 404
64
+ fail Errors::Unprocessable if response.status == 422
65
+
66
+ body
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,77 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SearchKit
5
+ module Clients
6
+ class Events
7
+ attr_reader :connection, :token
8
+
9
+ def initialize
10
+ uri = [ SearchKit.config.app_uri, "events" ].join("/")
11
+ @connection = Faraday.new(uri)
12
+ @token = SearchKit.config.app_token
13
+ end
14
+
15
+ def complete(id)
16
+ response = connection.delete(id, token: token)
17
+ body = JSON.parse(response.body, symbolize_names: true)
18
+
19
+ fail Errors::Unauthorized if response.status == 401
20
+ fail Errors::EventNotFound if response.status == 404
21
+
22
+ SearchKit::Models::Event.new body.fetch(:data, {})
23
+ end
24
+
25
+ def index
26
+ response = connection.get(token: token)
27
+ body = JSON.parse(response.body, symbolize_names: true)
28
+
29
+ fail Errors::Unauthorized if response.status == 401
30
+
31
+ SearchKit::Models::Events.new body.fetch(:data, [])
32
+ end
33
+
34
+ def pending(channel)
35
+ params = { "filter[channel]" => channel, token: token }
36
+ response = connection.get('', params)
37
+ body = JSON.parse(response.body, symbolize_names: true)
38
+
39
+ fail Errors::BadRequest if response.status == 400
40
+ fail Errors::Unauthorized if response.status == 401
41
+ fail Errors::Unprocessable if response.status == 422
42
+
43
+ SearchKit::Models::Events.new body.fetch(:data, [])
44
+ end
45
+
46
+ def publish(channel, payload)
47
+ params = {
48
+ token: token,
49
+ data: {
50
+ type: 'events',
51
+ attributes: { channel: channel, payload: payload }
52
+ }
53
+ }
54
+
55
+ response = connection.post("", params)
56
+ body = JSON.parse(response.body, symbolize_names: true)
57
+
58
+ fail Errors::BadRequest if response.status == 400
59
+ fail Errors::Unauthorized if response.status == 401
60
+ fail Errors::Unprocessable if response.status == 422
61
+
62
+ SearchKit::Models::Event.new body.fetch(:data, {})
63
+ end
64
+
65
+ def show(id)
66
+ response = connection.get(id, token: token)
67
+ body = JSON.parse(response.body, symbolize_names: true)
68
+
69
+ fail Errors::Unauthorized if response.status == 401
70
+ fail Errors::EventNotFound if response.status == 404
71
+
72
+ SearchKit::Models::Event.new body.fetch(:data, {})
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,70 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SearchKit
5
+ module Clients
6
+ class Indices
7
+ attr_reader :connection, :token
8
+
9
+ def initialize
10
+ uri = [SearchKit.config.app_uri, "indices"].join("/")
11
+ @connection = Faraday.new(uri)
12
+ @token = SearchKit.config.app_token
13
+ end
14
+
15
+ def archive(slug)
16
+ response = connection.delete(slug, token: token)
17
+ body = JSON.parse(response.body, symbolize_names: true)
18
+
19
+ fail Errors::Unauthorized if response.status == 401
20
+ fail Errors::IndexNotFound if response.status == 404
21
+
22
+ body
23
+ end
24
+
25
+ def create(name)
26
+ options = {
27
+ token: token,
28
+ data: { type: 'indices', attributes: { name: name } }
29
+ }
30
+
31
+ response = connection.post('', options)
32
+ body = JSON.parse(response.body, symbolize_names: true)
33
+
34
+ fail Errors::Unauthorized if response.status == 401
35
+ fail Errors::BadRequest if response.status == 400
36
+ fail Errors::Unprocessable if response.status == 422
37
+
38
+ body
39
+ end
40
+
41
+ def show(slug)
42
+ response = connection.get(slug, token: token)
43
+ body = JSON.parse(response.body, symbolize_names: true)
44
+
45
+ fail Errors::Unauthorized if response.status == 401
46
+ fail Errors::IndexNotFound if response.status == 404
47
+
48
+ body
49
+ end
50
+
51
+ def update(slug, options)
52
+ options = {
53
+ token: token,
54
+ data: { type: 'indices', attributes: options }
55
+ }
56
+
57
+ response = connection.patch(slug, options)
58
+ body = JSON.parse(response.body, symbolize_names: true)
59
+
60
+ fail Errors::BadRequest if response.status == 400
61
+ fail Errors::Unauthorized if response.status == 401
62
+ fail Errors::IndexNotFound if response.status == 404
63
+ fail Errors::Unprocessable if response.status == 422
64
+
65
+ body
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,78 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SearchKit
5
+ module Clients
6
+ class Keys
7
+ attr_reader :connection, :token
8
+
9
+ def initialize
10
+ uri = [SearchKit.config.app_uri, "keys"].join("/")
11
+ @connection = Faraday.new(uri)
12
+ @token = SearchKit.config.app_token
13
+ end
14
+
15
+ def create(name, options = {})
16
+ options = {
17
+ token: token,
18
+ data: { type: 'keys', attributes: { name: name }.merge(options) }
19
+ }
20
+
21
+ response = connection.post('', options)
22
+ body = JSON.parse(response.body, symbolize_names: true)
23
+
24
+ fail Errors::BadRequest if response.status == 400
25
+ fail Errors::Unauthorized if response.status == 401
26
+ fail Errors::Unprocessable if response.status == 422
27
+
28
+ body
29
+ end
30
+
31
+ def expire(id)
32
+ response = connection.delete(id, token: token)
33
+ body = JSON.parse(response.body, symbolize_names: true)
34
+
35
+ fail Errors::Unauthorized if response.status == 401
36
+ fail Errors::KeyNotFound if response.status == 404
37
+
38
+ body
39
+ end
40
+
41
+ def index
42
+ response = connection.get("", token: token)
43
+ body = JSON.parse(response.body, symbolize_names: true)
44
+
45
+ fail Errors::Unauthorized if response.status == 401
46
+
47
+ body
48
+ end
49
+
50
+ def show(id)
51
+ response = connection.get(id, token: token)
52
+ body = JSON.parse(response.body, symbolize_names: true)
53
+
54
+ fail Errors::Unauthorized if response.status == 401
55
+ fail Errors::KeyNotFound if response.status == 404
56
+
57
+ body
58
+ end
59
+
60
+ def update(id, options)
61
+ options = {
62
+ token: token, data: { type: 'keys', attributes: options }
63
+ }
64
+
65
+ response = connection.patch(id, options)
66
+ body = JSON.parse(response.body, symbolize_names: true)
67
+
68
+ fail Errors::BadRequest if response.status == 400
69
+ fail Errors::Unauthorized if response.status == 401
70
+ fail Errors::KeyNotFound if response.status == 404
71
+ fail Errors::Unprocessable if response.status == 422
72
+
73
+ body
74
+ end
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,72 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module SearchKit
5
+ module Clients
6
+ class Populate
7
+ attr_reader :connection, :token
8
+
9
+ def initialize
10
+ uri = [SearchKit.config.app_uri, "populate"].join("/")
11
+ @connection = Faraday.new(uri)
12
+ @token = SearchKit.config.app_token
13
+ end
14
+
15
+ def create(slug, documents)
16
+ documents = documents.map do |document|
17
+ { type: 'documents', attributes: document }
18
+ end
19
+
20
+ params = { token: token, data: documents }
21
+ response = connection.post(slug, params)
22
+ body = JSON.parse(response.body, symbolize_names: true)
23
+
24
+ fail Errors::BadRequest if response.status == 400
25
+ fail Errors::Unauthorized if response.status == 401
26
+ fail Errors::IndexNotFound if response.status == 404
27
+ fail Errors::Unprocessable if response.status == 422
28
+
29
+ body
30
+ end
31
+
32
+ def update(slug, documents)
33
+ documents = documents.map do |document|
34
+ {
35
+ type: 'documents',
36
+ id: document.fetch(:id, nil),
37
+ attributes: document
38
+ }
39
+ end
40
+
41
+ params = { token: token, data: documents }
42
+ response = connection.patch(slug, params)
43
+ body = JSON.parse(response.body, symbolize_names: true)
44
+
45
+ fail Errors::BadRequest if response.status == 400
46
+ fail Errors::Unauthorized if response.status == 401
47
+ fail Errors::IndexNotFound if response.status == 404
48
+ fail Errors::Unprocessable if response.status == 422
49
+
50
+ body
51
+ end
52
+
53
+ def delete(slug, ids)
54
+ documents = ids.map do |id|
55
+ { type: 'documents', id: id, attributes: { id: id } }
56
+ end
57
+
58
+ params = { token: token, data: documents }
59
+ response = connection.delete(slug, params)
60
+ body = JSON.parse(response.body, symbolize_names: true)
61
+
62
+ fail Errors::BadRequest if response.status == 400
63
+ fail Errors::Unauthorized if response.status == 401
64
+ fail Errors::IndexNotFound if response.status == 404
65
+ fail Errors::Unprocessable if response.status == 422
66
+
67
+ body
68
+ end
69
+
70
+ end
71
+ end
72
+ end