elasticord 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5e4119d7285ad68f49090e7b9344e88c24561c8
4
- data.tar.gz: 9cbf209b6d0dc56674975cbe9672dcfbba414444
3
+ metadata.gz: fbe255262b51c074ad05a210ae821894cae53710
4
+ data.tar.gz: f5fa702ceffbb7051937446166e919fcf9f6204f
5
5
  SHA512:
6
- metadata.gz: 1f6a9926758e9ffdafc6439208ddd82fb96480e7102f1f91914407f7c30be698572703b95d6939c49ae9625a2407403dca8f9510e4a8170ee55982d1703ea018
7
- data.tar.gz: 845a169cd4b027b00d26cc7aa76acf5c634fb45d675e55ce6d2c08179003b5f6d1b5df798987975b480f61a6a70db3e2d838f1a246abfbf7fd0f9f40d0e7b840
6
+ metadata.gz: c350937a3105ac455fd96fa0c4bc8238551ea759732c58a7ebea5672f89efe1e56a0abb68b357603751ee9f07aa9e21b0b819da64369c03f1db8a65d79b0dc9d
7
+ data.tar.gz: eb4694da749d2a315a3d5458ccac26afa5aff1346224ab2d65079e3e5aeecdea2c41e5731d02d3b56b8eff219f26a5dca941994310f68f1b6c021e75ada66c85
@@ -12,7 +12,7 @@ PATH
12
12
  object_attorney (>= 3.0.7)
13
13
  pg (>= 0.17)
14
14
  ransack (>= 1.7.0)
15
- elasticord (1.0.1)
15
+ elasticord (1.0.3)
16
16
  elasticsearch (~> 5.0)
17
17
 
18
18
  GEM
@@ -14,6 +14,8 @@ module DashOverlord
14
14
 
15
15
  context.search_builder = resources.ransack(filter_query.presence)
16
16
 
17
+ return unless context.search_builder.respond_to?(:result)
18
+
17
19
  context.resources = context.search_builder.result(distinct: true)
18
20
  end
19
21
  end
@@ -1,3 +1,4 @@
1
+ require 'forwardable'
1
2
  require 'elasticsearch'
2
3
 
3
4
  require_relative 'elasticord/base'
@@ -5,12 +6,12 @@ require_relative 'elasticord/config'
5
6
  require_relative 'elasticord/version'
6
7
 
7
8
  module Elasticord
8
- def self.client
9
- @client ||= reset_client
9
+ def self.elastic_search_client
10
+ @elastic_search_client ||= reset_elastic_search_client
10
11
  end
11
12
 
12
- def self.reset_client
13
- @client = Elasticsearch::Client.new \
13
+ def self.reset_elastic_search_client
14
+ @elastic_search_client = Elasticsearch::Client.new \
14
15
  log: Elasticord::Config.get(:log)
15
16
  end
16
17
 
@@ -1,51 +1,27 @@
1
1
  require 'ostruct'
2
2
 
3
- require 'elasticord/orm/get'
4
- require 'elasticord/orm/index'
5
- require 'elasticord/orm/create'
6
- require 'elasticord/orm/delete'
7
- require 'elasticord/orm/search_builder'
3
+ require 'elasticord/client/base'
8
4
 
9
5
  module Elasticord
10
6
  class Base < OpenStruct
11
7
  module ClassMethods
12
- def create(attributes = {})
13
- result = ORM::Create.one(index, type, attributes)
8
+ extend Forwardable
14
9
 
15
- return false unless result
10
+ def client(sub_type = nil)
11
+ full_type = [type, sub_type].compact.join('_')
16
12
 
17
- attributes[:id] ||= result['_id']
18
-
19
- self.new(attributes)
20
- end
21
-
22
- def get(id)
23
- result = ORM::Get.one(index, type, id)
24
-
25
- return false unless result
26
-
27
- self.new(result.merge({ 'id' => id }))
28
- end
29
-
30
- def search(body = {})
31
- none.load(body)
13
+ Client::Base.new \
14
+ Elasticord.elastic_search_client, self, index, full_type
32
15
  end
33
16
 
34
- def none
35
- ORM::SearchBuilder.new(self, index, type)
36
- end
37
-
38
- def delete_by_query(attributes = {})
39
- ORM::Delete.by_query(index, type, attributes)
40
- end
41
-
42
- def delete_all
43
- ORM::Delete.all(index)
44
- end
45
-
46
- def refresh_index
47
- ORM::Index.refresh(index)
48
- end
17
+ def_delegators :client,
18
+ :get,
19
+ :none,
20
+ :create,
21
+ :search,
22
+ :delete_all,
23
+ :refresh_index,
24
+ :delete_by_query
49
25
 
50
26
  def index
51
27
  return @index if defined?(@index)
@@ -1,5 +1,5 @@
1
1
  module Elasticord
2
- module Entities
2
+ module Client
3
3
  class ArrayWithMetaData < SimpleDelegator
4
4
  attr_accessor :per_page, :current_page, :total_results
5
5
 
@@ -26,12 +26,6 @@ module Elasticord
26
26
  def from
27
27
  (current_page - 1) * size
28
28
  end
29
-
30
- protected
31
-
32
- def parse_string_number(number)
33
- number.class == String ? number.presence : number
34
- end
35
29
  end
36
30
  end
37
31
  end
@@ -0,0 +1,57 @@
1
+ require 'ostruct'
2
+
3
+ require 'elasticord/client/get'
4
+ require 'elasticord/client/index'
5
+ require 'elasticord/client/create'
6
+ require 'elasticord/client/delete'
7
+ require 'elasticord/client/search_builder'
8
+
9
+ module Elasticord
10
+ module Client
11
+ class Base
12
+ def initialize(elastic_search_client, entity, index, type)
13
+ @elastic_search_client, @entity, @index, @type =
14
+ elastic_search_client, entity, index, type
15
+ end
16
+
17
+ def create(attributes = {})
18
+ result = Client::Create.one \
19
+ elastic_search_client, index, type, attributes
20
+
21
+ return false unless result
22
+
23
+ attributes[:id] ||= result['_id']
24
+
25
+ entity.new(attributes)
26
+ end
27
+
28
+ def get(id)
29
+ result = Client::Get.one(elastic_search_client, index, type, id)
30
+
31
+ return false unless result
32
+
33
+ entity.new(result.merge({ 'id' => id }))
34
+ end
35
+
36
+ def search(body_params = {})
37
+ none.load(body_params)
38
+ end
39
+
40
+ def none
41
+ Client::SearchBuilder.new(elastic_search_client, entity, index, type)
42
+ end
43
+
44
+ def delete_all
45
+ Client::Delete.all(elastic_search_client, index)
46
+ end
47
+
48
+ def refresh_index
49
+ Client::Index.refresh(elastic_search_client, index)
50
+ end
51
+
52
+ protected
53
+
54
+ attr_reader :elastic_search_client, :entity, :index, :type
55
+ end
56
+ end
57
+ end
@@ -1,15 +1,15 @@
1
1
  module Elasticord
2
- module ORM
2
+ module Client
3
3
  module Create
4
4
  module_function
5
5
 
6
6
  REFRESH_INDEX_AFTER_CREATE = true
7
7
 
8
- def one(index, type, attributes = {})
8
+ def one(elastic_search_client, index, type, attributes = {})
9
9
  params = build_params(index, type, attributes)
10
10
 
11
11
  begin
12
- Elasticord.client.index params
12
+ elastic_search_client.index params
13
13
  rescue Elasticsearch::Transport::Transport::Errors::Conflict
14
14
  return false
15
15
  end
@@ -0,0 +1,23 @@
1
+ module Elasticord
2
+ module Client
3
+ module Delete
4
+ module_function
5
+
6
+ def all(elastic_search_client, index)
7
+ begin
8
+ elastic_search_client.indices.delete index: index
9
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
10
+ return true
11
+ end
12
+ end
13
+
14
+ # def by_query(elastic_search_client, index, type, attributes = {})
15
+ # params = { index: index, body: attributes }
16
+ #
17
+ # params[:type] = type unless type == '_all'
18
+ #
19
+ # elastic_search_client.delete_by_query params
20
+ # end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ module Elasticord
2
+ module Client
3
+ module Get
4
+ module_function
5
+
6
+ def one(elastic_search_client, index, type, id)
7
+ result = elastic_search_client.get \
8
+ index: index, type: type, ignore: 404, id: id
9
+
10
+ (result && result['found']) ? result['_source'] : false
11
+ end
12
+
13
+ # def by_query(elastic_search_client, index, type, body = {})
14
+ # data = { total_results: 0, data: [] }
15
+ # params = { index: index, type: type, body: body }
16
+ #
17
+ # begin
18
+ # results = elastic_search_client.search(params)
19
+ #
20
+ # hits = results['hits']
21
+ #
22
+ # return data if !hits || hits['total'].to_i < 1
23
+ #
24
+ # data[:data] = hits['hits'].map { |result| result['_source'] }
25
+ # data[:total_results] = hits['total']
26
+ # rescue Elasticsearch::Transport::Transport::Errors::NotFound
27
+ # nil
28
+ # end
29
+ #
30
+ # data
31
+ # end
32
+ end
33
+ end
34
+ end
@@ -1,11 +1,11 @@
1
1
  module Elasticord
2
- module ORM
2
+ module Client
3
3
  module Index
4
4
  module_function
5
5
 
6
- def refresh(index)
6
+ def refresh(elastic_search_client, index)
7
7
  begin
8
- Elasticord.client.indices.refresh index: index
8
+ elastic_search_client.indices.refresh index: index
9
9
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
10
10
  return true
11
11
  end
@@ -1,16 +1,16 @@
1
- require 'forwardable'
2
- require 'elasticord/entities/array_with_meta_data'
1
+ require 'elasticord/client/array_with_meta_data'
3
2
 
4
3
  module Elasticord
5
- module ORM
4
+ module Client
6
5
  class SearchBuilder
7
6
  extend Forwardable
8
7
 
9
8
  PAGE_DEFAULT = 1
10
9
  PER_PAGE_DEFAULT = 10
11
10
 
12
- def initialize(entity, index, type)
13
- @entity, @index, @type = entity, index, type
11
+ def initialize(elastic_search_client, entity, index, type)
12
+ @elastic_search_client, @entity, @index, @type =
13
+ elastic_search_client, entity, index, type
14
14
  end
15
15
 
16
16
  def_delegators :results, :current_page, :per_page, :size, :from
@@ -71,25 +71,21 @@ module Elasticord
71
71
  self
72
72
  end
73
73
 
74
- def result(*_args)
75
- self
76
- end
77
-
78
74
  def body_params
79
75
  @body_params ||= { size: size, from: from, query: { bool: {} } }
80
76
  end
81
77
 
82
78
  protected
83
79
 
84
- attr_reader :type, :index, :entity
80
+ attr_reader :elastic_search_client, :entity, :index, :type
85
81
 
86
82
  def results
87
- @results ||= Entities::ArrayWithMetaData.new \
83
+ @results ||= ArrayWithMetaData.new \
88
84
  PAGE_DEFAULT, PER_PAGE_DEFAULT
89
85
  end
90
86
 
91
87
  def execute_search(body)
92
- elastic_results = Elasticord.client.search \
88
+ elastic_results = elastic_search_client.search \
93
89
  index: index, type: type, body: body_params.merge(body)
94
90
 
95
91
  hits = elastic_results['hits']
@@ -1,3 +1,3 @@
1
1
  module Elasticord
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.3'.freeze
3
3
  end
@@ -1,129 +1,167 @@
1
1
  require 'spec_helper'
2
- require 'elasticsearch'
3
2
 
4
3
  describe Elasticord::Base, elasticsearch: true do
5
- ElasticordChildClass1 = Class.new(described_class)
6
-
7
- def elastic_search_client
8
- @elastic_search_client ||= Elasticsearch::Client.new
9
- end
10
-
11
- def get_one_elastic_search_record(id)
12
- result = elastic_search_client.get \
13
- id: id,
14
- type: ElasticordChildClass1.type,
15
- index: ElasticordChildClass1.index,
16
- ignore: 404
17
-
18
- result
19
- end
20
-
21
4
  describe '.index' do
22
5
  it 'described_class should return the current env' do
23
6
  expect(described_class.index).to eq ENV['ENV']
24
7
  end
25
8
 
26
9
  context 'given a child class' do
27
- it 'ElasticordChildClass1 should return the same current env' do
28
- expect(ElasticordChildClass1.index).to eq ENV['ENV']
10
+ let(:child_class) { Class.new(described_class) }
11
+
12
+ it 'child_class should return the same current env' do
13
+ expect(child_class.index).to eq ENV['ENV']
29
14
  end
30
15
 
31
16
  context 'given that the child class sets a new index' do
32
- before { ElasticordChildClass1.index = 'child_class_new_index' }
17
+ before do
18
+ child_class.index = 'child_class_new_index'
19
+ end
33
20
 
34
21
  it 'described_class should return the current env' do
35
22
  expect(described_class.index).to eq ENV['ENV']
36
23
  end
37
24
 
38
- it 'ElasticordChildClass1 should return "child_class_new_index"' do
39
- expect(ElasticordChildClass1.index).to eq 'child_class_new_index'
25
+ it 'child_class should return "child_class_new_index"' do
26
+ expect(child_class.index).to eq 'child_class_new_index'
40
27
  end
41
28
  end
42
29
  end
43
30
  end
44
31
 
45
32
  describe '.type' do
46
- it 'described_class should return "_all"' do
33
+ it 'should return "_all"' do
47
34
  expect(described_class.type).to eq '_all'
48
35
  end
49
36
 
50
- context 'given a child class' do
51
- it 'ElasticordChildClass1 should return "elasticord_child_class1"' do
52
- expect(ElasticordChildClass1.type).to eq 'elasticord_child_class1'
37
+ context 'given a child class with a name' do
38
+ let(:child_class) do
39
+ Class.new(described_class) do
40
+ def self.name
41
+ 'ChildClass'
42
+ end
43
+ end
44
+ end
45
+
46
+ it 'should return that child class name' do
47
+ expect(child_class.type).to eq 'child_class'
53
48
  end
54
49
  end
55
50
  end
56
51
 
57
- describe '.create' do
58
- context 'given an ID' do
59
- let(:id) { '123' }
60
-
52
+ describe '.client' do
53
+ context 'without params' do
61
54
  before do
62
- @child_class = ElasticordChildClass1.create id: id, title: 'child class'
55
+ @client = described_class.client
63
56
  end
64
57
 
65
- it 'ElasticSearch should contain a record with that specific id' do
66
- result = get_one_elastic_search_record id
58
+ it 'should reaturn an instance of Client::Base' do
59
+ expect(@client).to be_an_instance_of Elasticord::Client::Base
60
+ end
67
61
 
68
- expect(result['found']).to be true
62
+ it 'reaturned client should have a protected #entity set up' do
63
+ expect(@client.send(:entity)).to eq described_class
69
64
  end
70
65
 
71
- it '@child_class.id should match' do
72
- expect(@child_class.id).to eq id
66
+ it 'reaturned client should have a protected #index set up' do
67
+ expect(@client.send(:index)).to eq ENV['ENV']
73
68
  end
74
69
 
75
- it '@child_class should be an instance of ElasticordChildClass1' do
76
- expect(@child_class).to be_an_instance_of ElasticordChildClass1
70
+ it 'reaturned client should have a protected #type set up' do
71
+ expect(@client.send(:type)).to eq '_all'
77
72
  end
78
73
  end
79
74
 
80
- context 'given no ID' do
75
+ context 'with "project_1"' do
81
76
  before do
82
- @child_class = ElasticordChildClass1.create title: 'child class'
77
+ @client = described_class.client 'project_1'
83
78
  end
84
79
 
85
- it 'ElasticSearch DB should contain a record with that specific id' do
86
- result = get_one_elastic_search_record @child_class.id
80
+ it 'should reaturn an instance of Client::Base' do
81
+ expect(@client).to be_an_instance_of Elasticord::Client::Base
82
+ end
87
83
 
88
- expect(result['found']).to be true
84
+ it 'reaturned client should have a protected #entity set up' do
85
+ expect(@client.send(:entity)).to eq described_class
86
+ end
87
+
88
+ it 'reaturned client should have a protected #index set up' do
89
+ expect(@client.send(:index)).to eq ENV['ENV']
90
+ end
91
+
92
+ it 'reaturned client should have a protected #type set up' do
93
+ expect(@client.send(:type)).to eq '_all_project_1'
89
94
  end
90
95
  end
91
96
  end
92
97
 
93
98
  describe '.search' do
94
- it 'should pass all params to a SearchBuilder instance' do
95
- attributes = { query: { match: { brand: 'oficial' } } }
99
+ it 'should pass all params to a Client::Base instance' do
100
+ attributes = [{ name: 'test' }]
96
101
 
97
- expect_any_instance_of(Elasticord::ORM::SearchBuilder).to \
98
- receive(:load).with(attributes)
102
+ expect_any_instance_of(Elasticord::Client::Base).to \
103
+ receive(:search).with(*attributes)
99
104
 
100
- ElasticordChildClass1.search(attributes)
105
+ described_class.search(*attributes)
101
106
  end
102
107
  end
103
108
 
104
109
  describe '.get' do
105
- context 'when the record does not exist' do
106
- it 'should return false' do
107
- result = ElasticordChildClass1.get '-1'
110
+ it 'should pass all params to a Client::Base instance' do
111
+ attributes = [1]
108
112
 
109
- expect(result).to be false
110
- end
113
+ expect_any_instance_of(Elasticord::Client::Base).to \
114
+ receive(:get).with(*attributes)
115
+
116
+ described_class.get(*attributes)
111
117
  end
118
+ end
112
119
 
113
- context 'when the record exists' do
114
- before do
115
- @child_class = ElasticordChildClass1.create title: 'child class'
120
+ describe '.none' do
121
+ it 'should pass all params to a Client::Base instance' do
122
+ expect_any_instance_of(Elasticord::Client::Base).to receive(:none)
116
123
 
117
- @result = ElasticordChildClass1.get @child_class.id
118
- end
124
+ described_class.none
125
+ end
126
+ end
119
127
 
120
- it 'should return an instance of ElasticordChildClass1' do
121
- expect(@result).to be_an_instance_of ElasticordChildClass1
122
- end
128
+ describe '.create' do
129
+ it 'should pass all params to a Client::Base instance' do
130
+ attributes = [{ name: 'test' }]
123
131
 
124
- it 'should return the same ID it requested' do
125
- expect(@result.id).to be @child_class.id
126
- end
132
+ expect_any_instance_of(Elasticord::Client::Base).to \
133
+ receive(:create).with(*attributes)
134
+
135
+ described_class.create(*attributes)
136
+ end
137
+ end
138
+
139
+ describe '.delete_all' do
140
+ it 'should pass all params to a Client::Base instance' do
141
+ expect_any_instance_of(Elasticord::Client::Base).to \
142
+ receive(:delete_all)
143
+
144
+ described_class.delete_all
145
+ end
146
+ end
147
+
148
+ describe '.refresh_index' do
149
+ it 'should pass all params to a Client::Base instance' do
150
+ expect_any_instance_of(Elasticord::Client::Base).to \
151
+ receive(:refresh_index)
152
+
153
+ described_class.refresh_index
154
+ end
155
+ end
156
+
157
+ describe '.delete_by_query' do
158
+ it 'should pass all params to a Client::Base instance' do
159
+ attributes = [{ name: 'test' }]
160
+
161
+ expect_any_instance_of(Elasticord::Client::Base).to \
162
+ receive(:delete_by_query).with(*attributes)
163
+
164
+ described_class.delete_by_query(*attributes)
127
165
  end
128
166
  end
129
167
  end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe Elasticord::Client::Base, elasticsearch: true do
4
+ def get_one_elastic_search_record(id)
5
+ Elasticord.elastic_search_client.get \
6
+ id: id, type: 'child_class', index: ENV['ENV'], ignore: 404
7
+ end
8
+
9
+ let(:described_instance) do
10
+ described_class.new \
11
+ Elasticord.elastic_search_client,
12
+ OpenStruct,
13
+ ENV['ENV'],
14
+ 'child_class'
15
+ end
16
+
17
+ describe '#create' do
18
+ context 'given an ID' do
19
+ let(:id) { '123' }
20
+
21
+ before do
22
+ @child_class = described_instance.create id: id, title: 'child class'
23
+ end
24
+
25
+ it 'ElasticSearch should contain a record with that specific id' do
26
+ result = get_one_elastic_search_record id
27
+
28
+ expect(result['found']).to be true
29
+ end
30
+
31
+ it '@child_class.id should match' do
32
+ expect(@child_class.id).to eq id
33
+ end
34
+
35
+ it '@child_class should be an instance of OpenStruct' do
36
+ expect(@child_class).to be_an_instance_of OpenStruct
37
+ end
38
+ end
39
+
40
+ context 'given no ID' do
41
+ before do
42
+ @child_class = described_instance.create title: 'child class'
43
+ end
44
+
45
+ it 'ElasticSearch DB should contain a record with that specific id' do
46
+ result = get_one_elastic_search_record @child_class.id
47
+
48
+ expect(result['found']).to be true
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#search' do
54
+ it 'should pass all params to a SearchBuilder instance' do
55
+ attributes = { query: { match: { brand: 'oficial' } } }
56
+
57
+ expect_any_instance_of(Elasticord::Client::SearchBuilder).to \
58
+ receive(:load).with(attributes)
59
+
60
+ described_instance.search(attributes)
61
+ end
62
+ end
63
+
64
+ describe '#get' do
65
+ context 'when the record does not exist' do
66
+ it 'should return false' do
67
+ result = described_instance.get '-1'
68
+
69
+ expect(result).to be false
70
+ end
71
+ end
72
+
73
+ context 'when the record exists' do
74
+ before do
75
+ @child_class = described_instance.create title: 'child class'
76
+
77
+ @result = described_instance.get @child_class.id
78
+ end
79
+
80
+ it 'should return an instance of OpenStruct' do
81
+ expect(@result).to be_an_instance_of OpenStruct
82
+ end
83
+
84
+ it 'should return the same ID it requested' do
85
+ expect(@result.id).to be @child_class.id
86
+ end
87
+ end
88
+ end
89
+
90
+ describe '#none' do
91
+ before do
92
+ @search_builder = described_instance.none
93
+ end
94
+
95
+ it 'should reaturn an instance of Client::SearchBuilder' do
96
+ expect(@search_builder).to \
97
+ be_an_instance_of Elasticord::Client::SearchBuilder
98
+ end
99
+
100
+ it 'reaturned search_builder should have a protected #entity set up' do
101
+ expect(@search_builder.send(:entity)).to eq OpenStruct
102
+ end
103
+
104
+ it 'reaturned search_builder should have a protected #index set up' do
105
+ expect(@search_builder.send(:index)).to eq ENV['ENV']
106
+ end
107
+
108
+ it 'reaturned search_builder should have a protected #type set up' do
109
+ expect(@search_builder.send(:type)).to eq 'child_class'
110
+ end
111
+ end
112
+ end
@@ -1,33 +1,33 @@
1
1
  require 'spec_helper'
2
- require 'elasticsearch'
3
2
 
4
- describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
5
- ElasticordChildClass2 = Class.new(Elasticord::Base)
6
-
7
- def new_search_builder
8
- described_class.new \
9
- ElasticordChildClass2,
10
- ElasticordChildClass2.index,
11
- ElasticordChildClass2.type
3
+ describe Elasticord::Client::SearchBuilder, elasticsearch: true do
4
+ let(:client_base) do
5
+ Elasticord::Client::Base.new \
6
+ Elasticord.elastic_search_client,
7
+ OpenStruct,
8
+ ENV['ENV'],
9
+ 'child_class'
12
10
  end
13
11
 
12
+ let(:described_instance) { client_base.none }
13
+
14
14
  describe '#current_page' do
15
15
  it 'should have a default value set' do
16
- expect(new_search_builder.current_page).to \
16
+ expect(described_instance.current_page).to \
17
17
  be described_class::PAGE_DEFAULT
18
18
  end
19
19
  end
20
20
 
21
21
  describe '#per_page' do
22
22
  it 'should have a default value set' do
23
- expect(new_search_builder.per_page).to \
23
+ expect(described_instance.per_page).to \
24
24
  be described_class::PER_PAGE_DEFAULT
25
25
  end
26
26
  end
27
27
 
28
28
  describe '#body_params' do
29
29
  it 'should have pagination params set' do
30
- expect(new_search_builder.body_params).to match a_hash_including({
30
+ expect(described_instance.body_params).to match a_hash_including({
31
31
  size: described_class::PER_PAGE_DEFAULT,
32
32
  from: 0
33
33
  })
@@ -35,7 +35,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
35
35
 
36
36
  context 'when setting #page with 2' do
37
37
  before do
38
- @search_builder = new_search_builder
38
+ @search_builder = described_instance
39
39
  @search_builder.page(2)
40
40
  end
41
41
 
@@ -54,16 +54,9 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
54
54
  end
55
55
  end
56
56
 
57
- describe '#result' do
58
- it 'should return a SearchBuilder instance' do
59
- expect(new_search_builder.result(distinct: true)).to \
60
- be_an_instance_of described_class
61
- end
62
- end
63
-
64
57
  describe '#page' do
65
58
  def search_builder
66
- @search_builder ||= new_search_builder
59
+ @search_builder ||= described_instance
67
60
  end
68
61
 
69
62
  context 'sending 3 as an argument' do
@@ -87,7 +80,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
87
80
 
88
81
  describe '#per' do
89
82
  def search_builder
90
- @search_builder ||= new_search_builder
83
+ @search_builder ||= described_instance
91
84
  end
92
85
 
93
86
  context 'sending 15 as an argument' do
@@ -111,13 +104,13 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
111
104
 
112
105
  context 'given that no record has been created' do
113
106
  before do
114
- ElasticordChildClass2.delete_all
115
- ElasticordChildClass2.refresh_index
107
+ client_base.delete_all
108
+ client_base.refresh_index
116
109
  end
117
110
 
118
111
  describe '#load' do
119
112
  before do
120
- @search_result = new_search_builder.load
113
+ @search_result = described_instance.load
121
114
  end
122
115
 
123
116
  it 'should return a pagination object with empty resources' do
@@ -129,14 +122,14 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
129
122
 
130
123
  context 'given that 5 records have been created' do
131
124
  before do
132
- ElasticordChildClass2.delete_all
133
- 5.times { ElasticordChildClass2.create(title: 'child class') }
134
- ElasticordChildClass2.refresh_index
125
+ client_base.delete_all
126
+ 5.times { client_base.create(title: 'child class') }
127
+ client_base.refresh_index
135
128
  end
136
129
 
137
130
  describe '#load' do
138
131
  before do
139
- @search_result = new_search_builder.load
132
+ @search_result = described_instance.load
140
133
  end
141
134
 
142
135
  it 'should find 5 total_results' do
@@ -147,7 +140,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
147
140
  expect(@search_result.length).to be 5
148
141
 
149
142
  @search_result.each do |child_class|
150
- expect(child_class).to be_an_instance_of ElasticordChildClass2
143
+ expect(child_class).to be_an_instance_of OpenStruct
151
144
  end
152
145
  end
153
146
  end
@@ -155,26 +148,26 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
155
148
 
156
149
  context 'given that 30 records have been created' do
157
150
  before do
158
- ElasticordChildClass2.delete_all
151
+ client_base.delete_all
159
152
 
160
153
  24.times do
161
- ElasticordChildClass2.create \
154
+ client_base.create \
162
155
  title: 'child class', brand: 'oficial', locked: false
163
156
  end
164
157
 
165
- ElasticordChildClass2.create(title: 'class', brand: 'oficial', locked: true)
158
+ client_base.create(title: 'class', brand: 'oficial', locked: true)
166
159
 
167
160
  5.times do
168
- ElasticordChildClass2.create(title: 'child', brand: 'unoficial', locked: false)
161
+ client_base.create(title: 'child', brand: 'unoficial', locked: false)
169
162
  end
170
163
 
171
- ElasticordChildClass2.refresh_index
164
+ client_base.refresh_index
172
165
  end
173
166
 
174
167
  describe '#load' do
175
168
  context 'when no pagination params are passed' do
176
169
  before do
177
- @search_result = new_search_builder.load({})
170
+ @search_result = described_instance.load({})
178
171
  end
179
172
 
180
173
  it 'should find 30 total_results' do
@@ -185,14 +178,14 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
185
178
  expect(@search_result.length).to be 10
186
179
 
187
180
  @search_result.each do |child_class|
188
- expect(child_class).to be_an_instance_of ElasticordChildClass2
181
+ expect(child_class).to be_an_instance_of OpenStruct
189
182
  end
190
183
  end
191
184
  end
192
185
 
193
186
  context 'when #page(2) was called' do
194
187
  before do
195
- @search_builder = new_search_builder.page(2)
188
+ @search_builder = described_instance.page(2)
196
189
  end
197
190
 
198
191
  context 'when #per(15) was called' do
@@ -207,8 +200,6 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
207
200
  locked_eq: false
208
201
  })
209
202
 
210
- @search_builder = @search_builder.result(distinct: true)
211
-
212
203
  @search_result = @search_builder.load
213
204
  end
214
205
 
@@ -225,7 +216,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
225
216
 
226
217
  context 'when single query params are passed' do
227
218
  before do
228
- @search_result = new_search_builder.load \
219
+ @search_result = described_instance.load \
229
220
  query: {
230
221
  match: { brand: 'oficial' }
231
222
  }
@@ -242,7 +233,7 @@ describe Elasticord::ORM::SearchBuilder, elasticsearch: true do
242
233
 
243
234
  context 'when multi query params are passed' do
244
235
  before do
245
- @search_result = new_search_builder.load \
236
+ @search_result = described_instance.load \
246
237
  query: {
247
238
  bool: {
248
239
  must: [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - StreetBees Dev Team
@@ -231,13 +231,14 @@ files:
231
231
  - lib/dash_overlord/version.rb
232
232
  - lib/elasticord.rb
233
233
  - lib/elasticord/base.rb
234
+ - lib/elasticord/client/array_with_meta_data.rb
235
+ - lib/elasticord/client/base.rb
236
+ - lib/elasticord/client/create.rb
237
+ - lib/elasticord/client/delete.rb
238
+ - lib/elasticord/client/get.rb
239
+ - lib/elasticord/client/index.rb
240
+ - lib/elasticord/client/search_builder.rb
234
241
  - lib/elasticord/config.rb
235
- - lib/elasticord/entities/array_with_meta_data.rb
236
- - lib/elasticord/orm/create.rb
237
- - lib/elasticord/orm/delete.rb
238
- - lib/elasticord/orm/get.rb
239
- - lib/elasticord/orm/index.rb
240
- - lib/elasticord/orm/search_builder.rb
241
242
  - lib/elasticord/version.rb
242
243
  - lib/tasks/create_dummy_data.rake
243
244
  - spec/dash_overlord/use_cases/v1/admins/log_in_spec.rb
@@ -264,7 +265,8 @@ files:
264
265
  - spec/dash_overlord/use_cases/v1/videos/index/elastic_search_spec.rb
265
266
  - spec/dash_overlord/use_cases/v1/videos/index/postgres_spec.rb
266
267
  - spec/elasticord/base_spec.rb
267
- - spec/elasticord/orm/search_builder_spec.rb
268
+ - spec/elasticord/client/base_spec.rb
269
+ - spec/elasticord/client/search_builder_spec.rb
268
270
  - spec/factories/v1/admins.rb
269
271
  - spec/factories/v1/bee_videos.rb
270
272
  - spec/factories/v1/chart_configs.rb
@@ -324,7 +326,8 @@ test_files:
324
326
  - spec/dash_overlord/use_cases/v1/videos/index/elastic_search_spec.rb
325
327
  - spec/dash_overlord/use_cases/v1/videos/index/postgres_spec.rb
326
328
  - spec/elasticord/base_spec.rb
327
- - spec/elasticord/orm/search_builder_spec.rb
329
+ - spec/elasticord/client/base_spec.rb
330
+ - spec/elasticord/client/search_builder_spec.rb
328
331
  - spec/factories/v1/admins.rb
329
332
  - spec/factories/v1/bee_videos.rb
330
333
  - spec/factories/v1/chart_configs.rb
@@ -1,23 +0,0 @@
1
- module Elasticord
2
- module ORM
3
- module Delete
4
- module_function
5
-
6
- def by_query(index, type, attributes = {})
7
- params = { index: index, body: attributes }
8
-
9
- params[:type] = type unless type == '_all'
10
-
11
- Elasticord.client.delete_by_query params
12
- end
13
-
14
- def all(index)
15
- begin
16
- Elasticord.client.indices.delete index: index
17
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
18
- return true
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,34 +0,0 @@
1
- module Elasticord
2
- module ORM
3
- module Get
4
- module_function
5
-
6
- def one(index, type, id)
7
- result = Elasticord.client.get \
8
- index: index, type: type, ignore: 404, id: id
9
-
10
- (!result || !result['found']) ? false : result['_source']
11
- end
12
-
13
- def by_query(index, type, body = {})
14
- data = { total_results: 0, data: [] }
15
- params = { index: index, type: type, body: body }
16
-
17
- begin
18
- results = Elasticord.client.search(params)
19
-
20
- hits = results['hits']
21
-
22
- return data if !hits || hits['total'].to_i < 1
23
-
24
- data[:data] = hits['hits'].map { |result| result['_source'] }
25
- data[:total_results] = hits['total']
26
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
27
- nil
28
- end
29
-
30
- data
31
- end
32
- end
33
- end
34
- end