elastic_search_framework 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3462f6d260c17cc73a60848b61901bd26672a8b7
4
- data.tar.gz: 9b3ec91367cc9e54d4dea7d6cb2a226c6d3a4486
3
+ metadata.gz: ad27b2e732484f3dd87dab43f5970556944130d3
4
+ data.tar.gz: e544ff195387046123ace421f109e26afac7276b
5
5
  SHA512:
6
- metadata.gz: a367754c71d545eaf6fb439ce9c4c11c5f0dd49dc4c935316c2fa708c3c8464af7125c1f10b6abcf7c5d25babf84a785f54ae142ea3ad907da28453361b79cbf
7
- data.tar.gz: 845e279c30d93a5542bd23552993bec60ea81912e8ee22ed79f8848a4b5e61a1507f6fed8bcf29982f3232f4e793ac4140b92e1cc3d393b195ca413e8ba2461b
6
+ metadata.gz: bed96a3e35f0b61bfbabfae074fbb1aed3dd2ce0cc236230b97af0f89f26e521f84e349ad2622ac17bcb9ceb8b29567576c5ae1f8bf085938a2cd63cb98dc28c
7
+ data.tar.gz: 19c48dd859d007e6170602df0525389bfbe698fdd8a0b089a039cdd24e1483b721f92658e1348622228383a3ac2b6fb08f7e99dbefbff7eaf2900778ceec5e13
@@ -1,6 +1,6 @@
1
1
  require 'hash_kit'
2
2
  require 'json'
3
- require 'curb'
3
+ require 'connection_pool'
4
4
  require_relative 'elastic_search_framework/version'
5
5
  require_relative 'elastic_search_framework/logger'
6
6
  require_relative 'elastic_search_framework/exceptions'
@@ -54,23 +54,33 @@ module ElasticSearchFramework
54
54
  end
55
55
 
56
56
  def put(payload:)
57
- client = curl
58
- client.url = "#{host}/#{full_name}"
59
- json = JSON.dump(payload)
60
- client.http_put(json)
61
- unless is_valid_response?(client)
62
- raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self}] - Failed to put index. Payload: #{payload} | Response: #{client.body_str}")
57
+ uri = URI("#{host}/#{full_name}")
58
+
59
+ request = Net::HTTP::Put.new(uri.request_uri)
60
+ request.body = JSON.dump(payload)
61
+
62
+ response = repository.with_client do |client|
63
+ client.request(request)
64
+ end
65
+
66
+ unless is_valid_response?(response.code)
67
+ raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self}] - Failed to put index. Payload: #{payload} | Response: #{response.body}")
63
68
  end
64
69
  true
65
70
  end
66
71
 
67
72
  def get
68
- client = curl
69
- client.url = "#{host}/#{full_name}"
70
- client.http_get
73
+ uri = URI("#{host}/#{full_name}")
74
+
75
+ request = Net::HTTP::Get.new(uri.request_uri)
76
+
77
+ response = repository.with_client do |client|
78
+ client.request(request)
79
+ end
80
+
71
81
  result = nil
72
- if is_valid_response?(client)
73
- result = JSON.parse(client.body_str)
82
+ if is_valid_response?(response.code)
83
+ result = JSON.parse(response.body)
74
84
  end
75
85
  result
76
86
  end
@@ -80,10 +90,15 @@ module ElasticSearchFramework
80
90
  end
81
91
 
82
92
  def delete
83
- client = curl
84
- client.url = "#{host}/#{full_name}"
85
- client.http_delete
86
- is_valid_response?(client) || client.response_code == 404
93
+ uri = URI("#{host}/#{full_name}")
94
+
95
+ request = Net::HTTP::Delete.new(uri.request_uri)
96
+
97
+ response = repository.with_client do |client|
98
+ client.request(request)
99
+ end
100
+
101
+ is_valid_response?(response.code) || Integer(response.code) == 404
87
102
  end
88
103
 
89
104
  def create_payload(description:, mappings:)
@@ -134,12 +149,8 @@ module ElasticSearchFramework
134
149
  self.instance_variable_defined?(:@elastic_search_index_mappings) ? self.instance_variable_get(:@elastic_search_index_mappings) : {}
135
150
  end
136
151
 
137
- def curl
138
- Curl::Easy.new
139
- end
140
-
141
- def is_valid_response?(client)
142
- [200,201,202].include?(client.response_code)
152
+ def is_valid_response?(code)
153
+ [200,201,202].include?(Integer(code))
143
154
  end
144
155
 
145
156
  def full_name
@@ -174,4 +185,4 @@ module ElasticSearchFramework
174
185
  ElasticSearchFramework::Query.new(index: self)
175
186
  end
176
187
  end
177
- end
188
+ end
@@ -2,48 +2,75 @@ module ElasticSearchFramework
2
2
  class Repository
3
3
 
4
4
  def set(index:, entity:, type: 'default')
5
- client = curl
6
- client.url = "#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}"
5
+ uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{get_id_value(index: index, entity: entity)}")
7
6
  hash = hash_helper.to_hash(entity)
8
- client.http_put(JSON.dump(hash))
9
- unless is_valid_response?(client)
10
- raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred setting an index document. Response: #{client.body_str}")
7
+
8
+ request = Net::HTTP::Put.new(uri.request_uri)
9
+ request.body = JSON.dump(hash)
10
+
11
+ response = with_client do |client|
12
+ client.request(request)
13
+ end
14
+
15
+ unless is_valid_response?(response.code)
16
+ raise ElasticSearchFramework::Exceptions::IndexError.new(
17
+ "An error occurred setting an index document. Response: #{response.body}"
18
+ )
11
19
  end
12
20
  return true
13
21
  end
14
22
 
15
23
  def get(index:, id:, type: 'default')
16
- client = curl
17
- client.url = "#{host}/#{index.full_name}/#{type.downcase}/#{id}/_source"
18
- client.get
19
- if is_valid_response?(client)
20
- result = JSON.load(client.body_str)
24
+ uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{id}/_source")
25
+
26
+ request = Net::HTTP::Get.new(uri.request_uri)
27
+
28
+ response = with_client do |client|
29
+ client.request(request)
30
+ end
31
+
32
+ if is_valid_response?(response.code)
33
+ result = JSON.load(response.body)
21
34
  hash_helper.indifferent!(result)
22
35
  return result
23
- elsif client.response_code == 404
36
+ elsif Integer(response.code) == 404
24
37
  return nil
25
38
  else
26
- raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred getting an index document. Response: #{client.body_str}")
39
+ raise ElasticSearchFramework::Exceptions::IndexError.new(
40
+ "An error occurred getting an index document. Response: #{response.body}"
41
+ )
27
42
  end
28
43
  end
29
44
 
30
45
  def drop(index:, id:, type: 'default')
31
- client = curl
32
- client.url = "#{host}/#{index.full_name}/#{type.downcase}/#{id}"
33
- client.delete
34
- if is_valid_response?(client) || client.response_code == 404
46
+ uri = URI("#{host}/#{index.full_name}/#{type.downcase}/#{id}")
47
+
48
+ request = Net::HTTP::Delete.new(uri.request_uri)
49
+
50
+ response = with_client do |client|
51
+ client.request(request)
52
+ end
53
+
54
+ if is_valid_response?(response.code) || Integer(response.code) == 404
35
55
  return true
36
56
  else
37
- raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred dropping an index document. Response: #{client.body_str}")
57
+ raise ElasticSearchFramework::Exceptions::IndexError.new(
58
+ "An error occurred dropping an index document. Response: #{response.body}"
59
+ )
38
60
  end
39
61
  end
40
62
 
41
63
  def query(index:, expression:, type: 'default', limit: 10, count: false)
42
- client = curl
43
- client.url = "#{host}/#{index.full_name}/#{type}/_search?q=#{URI.encode(expression)}&size=#{limit}"
44
- client.get
45
- if is_valid_response?(client)
46
- result = JSON.parse(client.body_str)
64
+ uri = URI("#{host}/#{index.full_name}/#{type}/_search?q=#{URI.encode(expression)}&size=#{limit}")
65
+
66
+ request = Net::HTTP::Get.new(uri.request_uri)
67
+
68
+ response = with_client do |client|
69
+ client.request(request)
70
+ end
71
+
72
+ if is_valid_response?(response.code)
73
+ result = JSON.parse(response.body)
47
74
  hash_helper.indifferent!(result)
48
75
  if count
49
76
  return result[:hits][:total]
@@ -51,22 +78,42 @@ module ElasticSearchFramework
51
78
  return result[:hits][:total] > 0 ? result[:hits][:hits] : []
52
79
  end
53
80
  else
54
- raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred executing an index query. Response: #{client.body_str}")
81
+ raise ElasticSearchFramework::Exceptions::IndexError.new("An error occurred executing an index query. Response: #{response.body}")
82
+ end
83
+ end
84
+
85
+ def client
86
+ @client ||= Net::HTTP.new(host_uri.host, host_uri.port).tap do |c|
87
+ c.use_ssl = host_uri.scheme == 'https'
88
+ c.open_timeout = open_timeout
89
+ c.read_timeout = read_timeout
55
90
  end
56
91
  end
57
92
 
58
- def curl
59
- Curl::Easy.new
93
+ def idle_timeout
94
+ @idle_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5)
60
95
  end
61
96
 
62
- def is_valid_response?(client)
63
- [200,201,202].include?(client.response_code)
97
+ def read_timeout
98
+ @read_timeout ||= Integer(ENV['CONNECTION_READ_TIMEOUT'] || 5)
99
+ end
100
+
101
+ def open_timeout
102
+ @read_timeout ||= Integer(ENV['CONNECTION_OPEN_TIMEOUT'] || 1)
103
+ end
104
+
105
+ def is_valid_response?(status)
106
+ [200,201,202].include?(Integer(status))
64
107
  end
65
108
 
66
109
  def host
67
110
  "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}"
68
111
  end
69
112
 
113
+ def host_uri
114
+ URI("#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}")
115
+ end
116
+
70
117
  def hash_helper
71
118
  @hash_helper ||= HashKit::Helper.new
72
119
  end
@@ -75,5 +122,32 @@ module ElasticSearchFramework
75
122
  entity.instance_variable_get("@#{index.description[:id]}")
76
123
  end
77
124
 
125
+ def with_client
126
+ response = nil
127
+
128
+ self.class.pool.with do |base|
129
+ base.client.read_timeout = read_timeout
130
+ begin
131
+ response = yield base.client
132
+ ensure
133
+ base.client.read_timeout = idle_timeout
134
+ end
135
+ end
136
+
137
+ response
138
+ end
139
+
140
+ def self.pool
141
+ @pool ||= ConnectionPool.new(size: pool_size, timeout: pool_timeout) { ElasticSearchFramework::Repository.new }
142
+ end
143
+
144
+ def self.pool_size
145
+ @pool_size ||= Integer(ENV['CONNECTION_POOL_SIZE'] || 25)
146
+ end
147
+
148
+ def self.pool_timeout
149
+ @pool_timeout ||= Integer(ENV['CONNECTION_IDLE_TIMEOUT'] || 5)
150
+ end
151
+
78
152
  end
79
- end
153
+ end
@@ -1,3 +1,3 @@
1
1
  module ElasticSearchFramework
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -92,63 +92,41 @@ RSpec.describe ElasticSearchFramework::Index do
92
92
  end
93
93
  end
94
94
 
95
- describe '#curl' do
96
- it 'should return a Curl::Easy instance' do
97
- expect(ExampleIndex.curl).to be_a(Curl::Easy)
98
- end
99
- it 'should return a unique Curl::Easy instance' do
100
- expect(ExampleIndex.curl).not_to eq ExampleIndex.curl
101
- end
102
- end
103
-
104
95
  describe '#is_valid_response?' do
105
- let(:client) { double }
96
+ let(:code) { 200 }
106
97
  context 'when a 200 response code is returned' do
107
- before do
108
- allow(client).to receive(:response_code).and_return(200)
109
- end
110
98
  it 'should return true' do
111
- expect(ExampleIndex.is_valid_response?(client)).to be true
99
+ expect(ExampleIndex.is_valid_response?(code)).to be true
112
100
  end
113
101
  end
114
102
  context 'when a 201 response code is returned' do
115
- before do
116
- allow(client).to receive(:response_code).and_return(201)
117
- end
103
+ let(:code) { 201 }
118
104
  it 'should return true' do
119
- expect(ExampleIndex.is_valid_response?(client)).to be true
105
+ expect(ExampleIndex.is_valid_response?(code)).to be true
120
106
  end
121
107
  end
122
108
  context 'when a 202 response code is returned' do
123
- before do
124
- allow(client).to receive(:response_code).and_return(202)
125
- end
109
+ let(:code) { 202 }
126
110
  it 'should return true' do
127
- expect(ExampleIndex.is_valid_response?(client)).to be true
111
+ expect(ExampleIndex.is_valid_response?(code)).to be true
128
112
  end
129
113
  end
130
114
  context 'when a 400 response code is returned' do
131
- before do
132
- allow(client).to receive(:response_code).and_return(400)
133
- end
115
+ let(:code) { 400 }
134
116
  it 'should return false' do
135
- expect(ExampleIndex.is_valid_response?(client)).to be false
117
+ expect(ExampleIndex.is_valid_response?(code)).to be false
136
118
  end
137
119
  end
138
120
  context 'when a 401 response code is returned' do
139
- before do
140
- allow(client).to receive(:response_code).and_return(401)
141
- end
121
+ let(:code) { 401 }
142
122
  it 'should return false' do
143
- expect(ExampleIndex.is_valid_response?(client)).to be false
123
+ expect(ExampleIndex.is_valid_response?(code)).to be false
144
124
  end
145
125
  end
146
126
  context 'when a 500 response code is returned' do
147
- before do
148
- allow(client).to receive(:response_code).and_return(500)
149
- end
127
+ let(:code) { 500 }
150
128
  it 'should return false' do
151
- expect(ExampleIndex.is_valid_response?(client)).to be false
129
+ expect(ExampleIndex.is_valid_response?(code)).to be false
152
130
  end
153
131
  end
154
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_search_framework
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-11 00:00:00.000000000 Z
11
+ date: 2017-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: curb
98
+ name: connection_pool
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="