elastomer-client 0.7.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.overcommit.yml +5 -0
  3. data/.rubocop.yml +83 -0
  4. data/CHANGELOG.md +8 -0
  5. data/Gemfile +2 -2
  6. data/README.md +1 -1
  7. data/Rakefile +2 -2
  8. data/elastomer-client.gemspec +4 -2
  9. data/lib/elastomer/client.rb +42 -37
  10. data/lib/elastomer/client/bulk.rb +2 -2
  11. data/lib/elastomer/client/cluster.rb +19 -19
  12. data/lib/elastomer/client/delete_by_query.rb +7 -7
  13. data/lib/elastomer/client/docs.rb +81 -24
  14. data/lib/elastomer/client/errors.rb +2 -2
  15. data/lib/elastomer/client/index.rb +65 -29
  16. data/lib/elastomer/client/multi_percolate.rb +127 -0
  17. data/lib/elastomer/client/multi_search.rb +2 -2
  18. data/lib/elastomer/client/nodes.rb +4 -4
  19. data/lib/elastomer/client/percolator.rb +77 -0
  20. data/lib/elastomer/client/repository.rb +7 -7
  21. data/lib/elastomer/client/scroller.rb +14 -14
  22. data/lib/elastomer/client/snapshot.rb +9 -9
  23. data/lib/elastomer/client/template.rb +3 -3
  24. data/lib/elastomer/client/warmer.rb +5 -16
  25. data/lib/elastomer/core_ext/time.rb +1 -1
  26. data/lib/elastomer/middleware/encode_json.rb +5 -5
  27. data/lib/elastomer/middleware/opaque_id.rb +3 -3
  28. data/lib/elastomer/middleware/parse_json.rb +5 -5
  29. data/lib/elastomer/notifications.rb +4 -4
  30. data/lib/elastomer/version.rb +1 -1
  31. data/script/bootstrap +2 -0
  32. data/script/console +5 -5
  33. data/test/assertions.rb +26 -24
  34. data/test/client/bulk_test.rb +111 -111
  35. data/test/client/cluster_test.rb +58 -58
  36. data/test/client/delete_by_query_test.rb +53 -53
  37. data/test/client/docs_test.rb +279 -203
  38. data/test/client/errors_test.rb +1 -1
  39. data/test/client/index_test.rb +143 -109
  40. data/test/client/multi_percolate_test.rb +130 -0
  41. data/test/client/multi_search_test.rb +30 -28
  42. data/test/client/nodes_test.rb +30 -29
  43. data/test/client/percolator_test.rb +52 -0
  44. data/test/client/repository_test.rb +23 -23
  45. data/test/client/scroller_test.rb +40 -40
  46. data/test/client/snapshot_test.rb +15 -15
  47. data/test/client/stubbed_client_test.rb +15 -15
  48. data/test/client/template_test.rb +10 -10
  49. data/test/client/warmer_test.rb +18 -18
  50. data/test/client_test.rb +53 -53
  51. data/test/core_ext/time_test.rb +12 -12
  52. data/test/middleware/encode_json_test.rb +20 -20
  53. data/test/middleware/opaque_id_test.rb +10 -10
  54. data/test/middleware/parse_json_test.rb +14 -14
  55. data/test/notifications_test.rb +32 -32
  56. data/test/test_helper.rb +24 -24
  57. metadata +38 -2
@@ -1,20 +1,20 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
- require 'elastomer/core_ext/time'
1
+ require File.expand_path("../../test_helper", __FILE__)
2
+ require "elastomer/core_ext/time"
3
3
 
4
- describe 'JSON conversions for Time' do
4
+ describe "JSON conversions for Time" do
5
5
  before do
6
- @name = 'elastomer-time-test'
6
+ @name = "elastomer-time-test"
7
7
  @index = $client.index(@name)
8
8
 
9
9
  unless @index.exists?
10
10
  @index.create \
11
- :settings => { 'index.number_of_shards' => 1, 'index.number_of_replicas' => 0 },
11
+ :settings => { "index.number_of_shards" => 1, "index.number_of_replicas" => 0 },
12
12
  :mappings => {
13
13
  :doc1 => {
14
14
  :_source => { :enabled => true }, :_all => { :enabled => false },
15
15
  :properties => {
16
- :title => { :type => 'string', :index => 'not_analyzed' },
17
- :created_at => { :type => 'date' }
16
+ :title => { :type => "string", :index => "not_analyzed" },
17
+ :created_at => { :type => "date" }
18
18
  }
19
19
  }
20
20
  }
@@ -29,18 +29,18 @@ describe 'JSON conversions for Time' do
29
29
  @index.delete if @index.exists?
30
30
  end
31
31
 
32
- it 'generates ISO8601 formatted time strings' do
32
+ it "generates ISO8601 formatted time strings" do
33
33
  time = Time.utc(2013, 5, 3, 10, 1, 31)
34
34
  assert_equal '"2013-05-03T10:01:31Z"', MultiJson.encode(time)
35
35
  end
36
36
 
37
- it 'indexes time fields' do
37
+ it "indexes time fields" do
38
38
  time = Time.utc(2013, 5, 3, 10, 1, 31)
39
- h = @docs.index({:title => 'test document', :created_at => time}, :type => 'doc1')
39
+ h = @docs.index({:title => "test document", :created_at => time}, :type => "doc1")
40
40
 
41
41
  assert_created(h)
42
42
 
43
- doc = @docs.get(:type => 'doc1', :id => h['_id'])
44
- assert_equal '2013-05-03T10:01:31Z', doc['_source']['created_at']
43
+ doc = @docs.get(:type => "doc1", :id => h["_id"])
44
+ assert_equal "2013-05-03T10:01:31Z", doc["_source"]["created_at"]
45
45
  end
46
46
  end
@@ -1,53 +1,53 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  describe Elastomer::Middleware::EncodeJson do
4
4
  let(:middleware) { Elastomer::Middleware::EncodeJson.new(lambda {|env| env})}
5
5
 
6
6
  def process(body, content_type = nil)
7
7
  env = { :body => body, :request_headers => Faraday::Utils::Headers.new }
8
- env[:request_headers]['content-type'] = content_type if content_type
8
+ env[:request_headers]["content-type"] = content_type if content_type
9
9
  middleware.call(env)
10
10
  end
11
11
 
12
- it 'handles no body' do
12
+ it "handles no body" do
13
13
  result = process(nil)
14
14
  assert_nil result[:body]
15
- assert_nil result[:request_headers]['content-type']
15
+ assert_nil result[:request_headers]["content-type"]
16
16
  end
17
17
 
18
- it 'handles empty body' do
19
- result = process('')
18
+ it "handles empty body" do
19
+ result = process("")
20
20
  assert_empty result[:body]
21
- assert_nil result[:request_headers]['content-type']
21
+ assert_nil result[:request_headers]["content-type"]
22
22
  end
23
23
 
24
- it 'handles string body' do
24
+ it "handles string body" do
25
25
  result = process('{"a":1}')
26
26
  assert_equal '{"a":1}', result[:body]
27
- assert_equal 'application/json', result[:request_headers]['content-type']
27
+ assert_equal "application/json", result[:request_headers]["content-type"]
28
28
  end
29
29
 
30
- it 'handles object body' do
30
+ it "handles object body" do
31
31
  result = process({:a => 1})
32
32
  assert_equal '{"a":1}', result[:body]
33
- assert_equal 'application/json', result[:request_headers]['content-type']
33
+ assert_equal "application/json", result[:request_headers]["content-type"]
34
34
  end
35
35
 
36
- it 'handles empty object body' do
36
+ it "handles empty object body" do
37
37
  result = process({})
38
- assert_equal '{}', result[:body]
39
- assert_equal 'application/json', result[:request_headers]['content-type']
38
+ assert_equal "{}", result[:body]
39
+ assert_equal "application/json", result[:request_headers]["content-type"]
40
40
  end
41
41
 
42
- it 'handles object body with json type' do
43
- result = process({:a => 1}, 'application/json; charset=utf-8')
42
+ it "handles object body with json type" do
43
+ result = process({:a => 1}, "application/json; charset=utf-8")
44
44
  assert_equal '{"a":1}', result[:body]
45
- assert_equal 'application/json; charset=utf-8', result[:request_headers]['content-type']
45
+ assert_equal "application/json; charset=utf-8", result[:request_headers]["content-type"]
46
46
  end
47
47
 
48
- it 'handles object body with incompatible type' do
49
- result = process({:a => 1}, 'application/xml; charset=utf-8')
48
+ it "handles object body with incompatible type" do
49
+ result = process({:a => 1}, "application/xml; charset=utf-8")
50
50
  assert_equal({:a => 1}, result[:body])
51
- assert_equal 'application/xml; charset=utf-8', result[:request_headers]['content-type']
51
+ assert_equal "application/xml; charset=utf-8", result[:request_headers]["content-type"]
52
52
  end
53
53
  end
@@ -1,25 +1,25 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  describe Elastomer::Middleware::OpaqueId do
4
4
 
5
5
  before do
6
6
  stubs = Faraday::Adapter::Test::Stubs.new do |stub|
7
- stub.get('/_cluster/health') { |env|
7
+ stub.get("/_cluster/health") { |env|
8
8
  [ 200,
9
9
 
10
- { 'X-Opaque-Id' => env[:request_headers]['X-Opaque-Id'],
11
- 'Content-Type' => 'application/json; charset=UTF-8',
12
- 'Content-Length' => '49' },
10
+ { "X-Opaque-Id" => env[:request_headers]["X-Opaque-Id"],
11
+ "Content-Type" => "application/json; charset=UTF-8",
12
+ "Content-Length" => "49" },
13
13
 
14
14
  %q[{"cluster_name":"elasticsearch","status":"green"}]
15
15
  ]
16
16
  }
17
17
 
18
- stub.get('/') { |env|
19
- [ 200, {}, {'version' => {'number' => '1.0.0'}} ]
18
+ stub.get("/") { |env|
19
+ [ 200, {}, {"version" => {"number" => "1.0.0"}} ]
20
20
  }
21
- stub.get('/_cluster/state') { |env|
22
- [ 200, {'X-Opaque-Id' => "00000000-0000-0000-0000-000000000000"}, %q[{"foo":"bar"}] ]
21
+ stub.get("/_cluster/state") { |env|
22
+ [ 200, {"X-Opaque-Id" => "00000000-0000-0000-0000-000000000000"}, %q[{"foo":"bar"}] ]
23
23
  }
24
24
  end
25
25
 
@@ -35,7 +35,7 @@ describe Elastomer::Middleware::OpaqueId do
35
35
  assert_equal({"cluster_name" => "elasticsearch", "status" => "green"}, health)
36
36
  end
37
37
 
38
- it 'raises an exception on conflicting headers' do
38
+ it "raises an exception on conflicting headers" do
39
39
  assert_raises(Elastomer::Client::OpaqueIdError) { @client.cluster.state }
40
40
  end
41
41
 
@@ -1,4 +1,4 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path("../../test_helper", __FILE__)
2
2
 
3
3
  describe Elastomer::Middleware::ParseJson do
4
4
  let(:middleware) { Elastomer::Middleware::ParseJson.new(lambda {|env| Faraday::Response.new(env)})}
@@ -6,7 +6,7 @@ describe Elastomer::Middleware::ParseJson do
6
6
 
7
7
  def process(body, content_type = nil)
8
8
  env = { :body => body, :response_headers => Faraday::Utils::Headers.new(headers) }
9
- env[:response_headers]['content-type'] = content_type if content_type
9
+ env[:response_headers]["content-type"] = content_type if content_type
10
10
  middleware.call(env)
11
11
  end
12
12
 
@@ -15,34 +15,34 @@ describe Elastomer::Middleware::ParseJson do
15
15
  assert_nil response.body
16
16
  end
17
17
 
18
- it 'nullifies empty body' do
18
+ it "nullifies empty body" do
19
19
  response = process("")
20
20
  assert_nil response.body
21
21
  end
22
22
 
23
- it 'nullifies blank body' do
23
+ it "nullifies blank body" do
24
24
  response = process(" ")
25
25
  assert_nil response.body
26
26
  end
27
27
 
28
- it 'parses json body with empty type' do
28
+ it "parses json body with empty type" do
29
29
  response = process('{"a":1}')
30
- assert_equal({'a' => 1}, response.body)
30
+ assert_equal({"a" => 1}, response.body)
31
31
  end
32
32
 
33
- it 'parses json body of correct type' do
34
- response = process('{"a":1}', 'application/json; charset=utf-8')
35
- assert_equal({'a' => 1}, response.body)
33
+ it "parses json body of correct type" do
34
+ response = process('{"a":1}', "application/json; charset=utf-8")
35
+ assert_equal({"a" => 1}, response.body)
36
36
  end
37
37
 
38
- it 'ignores json body if incorrect type' do
39
- response = process('{"a":1}', 'application/xml; charset=utf-8')
38
+ it "ignores json body if incorrect type" do
39
+ response = process('{"a":1}', "application/xml; charset=utf-8")
40
40
  assert_equal('{"a":1}', response.body)
41
41
  end
42
42
 
43
- it 'chokes on invalid json' do
44
- assert_raises(Faraday::Error::ParsingError) { process '{!' }
45
- assert_raises(Faraday::Error::ParsingError) { process 'invalid' }
43
+ it "chokes on invalid json" do
44
+ assert_raises(Faraday::Error::ParsingError) { process "{!" }
45
+ assert_raises(Faraday::Error::ParsingError) { process "invalid" }
46
46
 
47
47
  # surprisingly these are all valid according to MultiJson
48
48
  #
@@ -1,9 +1,9 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
- require 'elastomer/notifications'
1
+ require File.expand_path("../test_helper", __FILE__)
2
+ require "elastomer/notifications"
3
3
 
4
4
  describe Elastomer::Notifications do
5
5
  before do
6
- @name = 'elastomer-notifications-test'
6
+ @name = "elastomer-notifications-test"
7
7
  @index = $client.index @name
8
8
  @index.delete if @index.exists?
9
9
  @events = []
@@ -20,51 +20,51 @@ describe Elastomer::Notifications do
20
20
  it "instruments timeouts" do
21
21
  $client.stub :connection, lambda { raise Faraday::Error::TimeoutError } do
22
22
  assert_raises(Elastomer::Client::TimeoutError) { $client.info }
23
- event = @events.detect { |e| e.payload[:action] == 'cluster.info' }
23
+ event = @events.detect { |e| e.payload[:action] == "cluster.info" }
24
24
  exception = event.payload[:exception]
25
- assert_equal 'Elastomer::Client::TimeoutError', exception[0]
26
- assert_match 'timeout', exception[1]
25
+ assert_equal "Elastomer::Client::TimeoutError", exception[0]
26
+ assert_match "timeout", exception[1]
27
27
  end
28
28
  end
29
29
 
30
- it 'instruments cluster actions' do
31
- $client.ping; assert_action_event('cluster.ping')
32
- $client.info; assert_action_event('cluster.info')
30
+ it "instruments cluster actions" do
31
+ $client.ping; assert_action_event("cluster.ping")
32
+ $client.info; assert_action_event("cluster.info")
33
33
  end
34
34
 
35
- it 'instruments node actions' do
35
+ it "instruments node actions" do
36
36
  nodes = $client.nodes
37
- nodes.info; assert_action_event('nodes.info')
38
- nodes.stats; assert_action_event('nodes.stats')
39
- nodes.hot_threads; assert_action_event('nodes.hot_threads')
37
+ nodes.info; assert_action_event("nodes.info")
38
+ nodes.stats; assert_action_event("nodes.stats")
39
+ nodes.hot_threads; assert_action_event("nodes.hot_threads")
40
40
  end
41
41
 
42
- it 'instruments node shutdown' do
43
- client = stub_client(:post, '/_cluster/nodes/_shutdown')
44
- client.nodes.shutdown; assert_action_event('nodes.shutdown')
42
+ it "instruments node shutdown" do
43
+ client = stub_client(:post, "/_cluster/nodes/_shutdown")
44
+ client.nodes.shutdown; assert_action_event("nodes.shutdown")
45
45
  end
46
46
 
47
- it 'instruments index actions' do
48
- @index.exists?; assert_action_event('index.exists')
49
- @index.create('number_of_replicas' => 0)
50
- assert_action_event('index.create')
51
- @index.get_settings; assert_action_event('index.get_settings')
52
- @index.update_settings('number_of_replicas' => 0)
53
- assert_action_event('index.get_settings')
54
- @index.close; assert_action_event('index.close')
55
- @index.open; assert_action_event('index.open')
56
- @index.delete; assert_action_event('index.delete')
47
+ it "instruments index actions" do
48
+ @index.exists?; assert_action_event("index.exists")
49
+ @index.create("number_of_replicas" => 0)
50
+ assert_action_event("index.create")
51
+ @index.get_settings; assert_action_event("index.get_settings")
52
+ @index.update_settings("number_of_replicas" => 0)
53
+ assert_action_event("index.get_settings")
54
+ @index.close; assert_action_event("index.close")
55
+ @index.open; assert_action_event("index.open")
56
+ @index.delete; assert_action_event("index.delete")
57
57
  end
58
58
 
59
- it 'includes the response body in the payload' do
60
- @index.create('number_of_replicas' => 0)
61
- event = @events.detect { |e| e.payload[:action] == 'index.create' }
59
+ it "includes the response body in the payload" do
60
+ @index.create("number_of_replicas" => 0)
61
+ event = @events.detect { |e| e.payload[:action] == "index.create" }
62
62
  assert event.payload[:response_body]
63
63
  end
64
64
 
65
- it 'includes the request body in the payload' do
66
- @index.create('number_of_replicas' => 0)
67
- event = @events.detect { |e| e.payload[:action] == 'index.create' }
65
+ it "includes the request body in the payload" do
66
+ @index.create("number_of_replicas" => 0)
67
+ event = @events.detect { |e| e.payload[:action] == "index.create" }
68
68
 
69
69
  payload = event.payload
70
70
  assert payload[:response_body]
data/test/test_helper.rb CHANGED
@@ -1,31 +1,31 @@
1
- require 'webmock/minitest'
1
+ require "webmock/minitest"
2
2
  WebMock.allow_net_connect!
3
3
 
4
- require 'securerandom'
5
- require 'rubygems' unless defined? Gem
6
- require 'bundler'
4
+ require "securerandom"
5
+ require "rubygems" unless defined? Gem
6
+ require "bundler"
7
7
  Bundler.require(:default, :development)
8
8
 
9
- if ENV['COVERAGE'] == 'true'
10
- require 'simplecov'
9
+ if ENV["COVERAGE"] == "true"
10
+ require "simplecov"
11
11
  SimpleCov.start do
12
12
  add_filter "/test/"
13
13
  add_filter "/vendor/"
14
14
  end
15
15
  end
16
16
 
17
- require 'minitest/spec'
18
- require 'minitest/autorun'
17
+ require "minitest/spec"
18
+ require "minitest/autorun"
19
19
 
20
20
  # push the lib folder onto the load path
21
- $LOAD_PATH.unshift 'lib'
22
- require 'elastomer/client'
21
+ $LOAD_PATH.unshift "lib"
22
+ require "elastomer/client"
23
23
 
24
24
  # we are going to use the same client instance everywhere!
25
25
  # the client should always be stateless
26
26
  $client_params = {
27
- :port => ENV['BOXEN_ELASTICSEARCH_PORT'] || 9200,
28
- :read_timeout => 2,
27
+ :port => ENV["BOXEN_ELASTICSEARCH_PORT"] || 9200,
28
+ :read_timeout => 10,
29
29
  :open_timeout => 1,
30
30
  :opaque_id => false
31
31
  }
@@ -50,7 +50,7 @@ MiniTest::Unit.after_tests do
50
50
  end
51
51
 
52
52
  # add custom assertions
53
- require File.expand_path('../assertions', __FILE__)
53
+ require File.expand_path("../assertions", __FILE__)
54
54
 
55
55
  # require 'elastomer/notifications'
56
56
  # require 'pp'
@@ -75,11 +75,11 @@ require File.expand_path('../assertions', __FILE__)
75
75
  # Returns the cluster health response.
76
76
  # Raises Elastomer::Client::TimeoutError if requested status is not achieved
77
77
  # within 5 seconds.
78
- def wait_for_index(name, status='yellow')
78
+ def wait_for_index(name, status="yellow")
79
79
  $client.cluster.health(
80
80
  :index => name,
81
81
  :wait_for_status => status,
82
- :timeout => '5s'
82
+ :timeout => "5s"
83
83
  )
84
84
  end
85
85
 
@@ -89,7 +89,7 @@ end
89
89
  #
90
90
  # Returns true if Elasticsearch version is 1.x.
91
91
  def es_version_1_x?
92
- $client.semantic_version >= '1.0.0'
92
+ $client.semantic_version >= "1.0.0"
93
93
  end
94
94
 
95
95
  # Elasticsearch 1.4 changed the response body for interacting with index
@@ -98,20 +98,20 @@ end
98
98
  #
99
99
  # Returns `true` if the response contains an "aliases" key.
100
100
  def es_version_always_returns_aliases?
101
- $client.semantic_version < '1.4.0' ||
102
- $client.semantic_version >= '1.4.3'
101
+ $client.semantic_version < "1.4.0" ||
102
+ $client.semantic_version >= "1.4.3"
103
103
  end
104
104
 
105
105
  # ElasticSearch 1.3 added the `search_shards` API endpoint.
106
106
  def es_version_supports_search_shards?
107
- $client.semantic_version >= '1.3.0'
107
+ $client.semantic_version >= "1.3.0"
108
108
  end
109
109
 
110
110
  # Elasticsearch 1.2 removed support for gateway snapshots.
111
111
  #
112
112
  # Returns true if Elasticsearch version supports gateway snapshots.
113
113
  def es_version_supports_gateway_snapshots?
114
- $client.semantic_version <= '1.2.0'
114
+ $client.semantic_version <= "1.2.0"
115
115
  end
116
116
 
117
117
  # Elasticsearch 1.4.0 had a bug in its handling of the Mapping API where it
@@ -120,13 +120,13 @@ end
120
120
  #
121
121
  # See: https://github.com/elastic/elasticsearch/pull/8426
122
122
  def es_version_supports_update_mapping_with__all_disabled?
123
- $client.semantic_version != '1.4.0'
123
+ $client.semantic_version != "1.4.0"
124
124
  end
125
125
 
126
126
  # Elasticsearch 1.6 requires the repo.path setting when creating
127
127
  # FS repositories.
128
128
  def es_version_requires_repo_path?
129
- $client.semantic_version >= '1.6.0'
129
+ $client.semantic_version >= "1.6.0"
130
130
  end
131
131
 
132
132
  def run_snapshot_tests?
@@ -146,8 +146,8 @@ def run_snapshot_tests?
146
146
  end
147
147
 
148
148
  def create_repo(name, settings = {})
149
- location = File.join(*[ENV['SNAPSHOT_DIR'], name].compact)
150
- default_settings = {:type => 'fs', :settings => {:location => location}}
149
+ location = File.join(*[ENV["SNAPSHOT_DIR"], name].compact)
150
+ default_settings = {:type => "fs", :settings => {:location => location}}
151
151
  $client.repository(name).create(default_settings.merge(settings))
152
152
  end
153
153