elastic_record 0.6.6 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source :rubygems
2
2
  gemspec
3
+
3
4
  gem 'rake'
5
+ gem 'fakeweb'
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '0.6.6'
5
+ s.version = '0.6.8'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -20,4 +20,4 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency 'arelastic'
22
22
  s.add_dependency 'activemodel'
23
- end
23
+ end
@@ -1,13 +1,13 @@
1
1
  require 'net/http'
2
2
 
3
3
  module ElasticRecord
4
- class Connection
5
- # :timeout: 10
6
- # :retries: 2
7
- # :auto_discovery: false
4
+ class ConnectionError < StandardError
5
+ end
8
6
 
7
+ class Connection
9
8
  attr_accessor :servers, :options
10
9
  attr_accessor :request_count, :current_server
10
+ attr_accessor :max_request_count
11
11
  def initialize(servers, options = {})
12
12
  if servers.is_a?(Array)
13
13
  self.servers = servers
@@ -17,6 +17,7 @@ module ElasticRecord
17
17
 
18
18
  self.current_server = choose_server
19
19
  self.request_count = 0
20
+ self.max_request_count = 100
20
21
  self.options = options
21
22
  end
22
23
 
@@ -45,7 +46,7 @@ module ElasticRecord
45
46
  response = http_request(method, path, body)
46
47
 
47
48
  json = ActiveSupport::JSON.decode response.body
48
- raise json['error'] if json['error']
49
+ raise ConnectionError.new(json['error']) if json['error']
49
50
 
50
51
  json
51
52
  end
@@ -61,11 +62,12 @@ module ElasticRecord
61
62
  def http_request(method, path, body = nil)
62
63
  request = METHODS[method].new(path)
63
64
  request.body = body
65
+ http = new_http
64
66
 
65
67
  ActiveSupport::Notifications.instrument("request.elastic_record") do |payload|
66
- payload[:method] = method
67
- payload[:request_uri] = path
68
- payload[:response] = http.request(request)
68
+ payload[:http] = http
69
+ payload[:request] = request
70
+ payload[:response] = http.request(request)
69
71
  end
70
72
  end
71
73
 
@@ -74,10 +76,10 @@ module ElasticRecord
74
76
  servers.sample
75
77
  end
76
78
 
77
- def http
79
+ def new_http
78
80
  self.request_count += 1
79
81
 
80
- if request_count > 100
82
+ if request_count > max_request_count{}
81
83
  self.current_server = choose_server
82
84
  self.request_count = 0
83
85
  end
@@ -1,9 +1,14 @@
1
1
  module ElasticRecord
2
2
  class LogSubscriber < ActiveSupport::LogSubscriber
3
3
  def request(event)
4
- response = event.payload[:response]
5
- debug "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]} (%.1fms)" % [event.duration]
6
- # info "--> %d %s %d (%.1fms)" % [response.code, response.message, response.body.to_s.length, event.duration]
4
+ payload = event.payload
5
+ request_log = "#{payload[:request].method} #{payload[:http].address}:#{payload[:http].port}#{payload[:request].path}"
6
+
7
+ if payload[:request].body
8
+ request_log << " '#{payload[:request].body}'"
9
+ end
10
+
11
+ debug "(%.1fms) #{request_log}" % [event.duration]
7
12
  end
8
13
 
9
14
  # def logger
@@ -59,7 +59,7 @@ namespace :index do
59
59
  end
60
60
 
61
61
  logger.info " Reindexing into #{index_name}"
62
- model.find_in_batches do |records|
62
+ model.find_in_batches(batch_size: 100) do |records|
63
63
  model.elastic_index.bulk_add(records, index_name)
64
64
  end
65
65
 
@@ -5,4 +5,38 @@ class ElasticRecord::ConnectionTest < MiniTest::Spec
5
5
  assert_equal ['foo', 'bar'], ElasticRecord::Connection.new('foo,bar').servers
6
6
  assert_equal ['foo', 'bar'], ElasticRecord::Connection.new(['foo', 'bar']).servers
7
7
  end
8
+
9
+ def test_head
10
+ FakeWeb.register_uri(:head, %r[/success], status: ["200", "OK"])
11
+ assert_equal "200", connection.head("/success")
12
+
13
+ FakeWeb.register_uri(:head, %r[/failure], status: ["404", "Not Found"])
14
+ assert_equal "404", connection.head("/failure")
15
+ end
16
+
17
+ def test_json_requests
18
+ expected = {'foo' => 'bar'}
19
+ FakeWeb.register_uri(:any, %r[/test], status: ["200", "OK"], body: ActiveSupport::JSON.encode(expected))
20
+
21
+ assert_equal expected, connection.json_delete("/test")
22
+ assert_equal expected, connection.json_get("/test")
23
+ assert_equal expected, connection.json_post("/test")
24
+ assert_equal expected, connection.json_put("/test")
25
+ end
26
+
27
+ def test_json_request_with_error
28
+ response_json = {'error' => 'Doing it wrong'}
29
+ FakeWeb.register_uri(:get, %r[/error], status: ["404", "Not Found"], body: ActiveSupport::JSON.encode(response_json))
30
+
31
+ error = assert_raises ElasticRecord::ConnectionError do
32
+ connection.json_get("/error")
33
+ end
34
+
35
+ assert_equal 'Doing it wrong', error.message
36
+ end
37
+
38
+ private
39
+ def connection
40
+ ElasticRecord::Connection.new(ElasticRecord::Config.servers)
41
+ end
8
42
  end
@@ -16,12 +16,13 @@ class ElasticRecord::LogSubscriberTest < ActiveSupport::TestCase
16
16
  # end
17
17
 
18
18
  def test_request_notification
19
- Widget.elastic_connection.head "/widgets"
19
+ FakeWeb.register_uri(:any, %r[/test], status: ["200", "OK"], body: ActiveSupport::JSON.encode('the' => 'response'))
20
+ Widget.elastic_connection.json_get "/test", {'foo' => 'bar'}
20
21
 
21
22
  wait
22
23
 
23
- assert_equal 1, @logger.logged(:info).size
24
- assert_match "HEAD /widgets", @logger.logged(:info)[0]
25
- # assert_match(/\-\-\> 200 200 33/, @logger.logged(:info)[1])
24
+ assert_equal 1, @logger.logged(:debug).size
25
+ assert_match /GET (.*)test/, @logger.logged(:debug)[0]
26
+ assert_match %r['#{ActiveSupport::JSON.encode('foo' => 'bar')}'], @logger.logged(:debug)[0]
26
27
  end
27
28
  end
data/test/helper.rb CHANGED
@@ -6,7 +6,13 @@ require 'minitest/autorun'
6
6
  require 'support/widget'
7
7
  require 'support/connect'
8
8
 
9
+ FakeWeb.allow_net_connect = %r[^https?://127.0.0.1]
10
+
9
11
  module MiniTest
10
12
  class Spec
13
+ def setup
14
+ super
15
+ FakeWeb.clean_registry
16
+ end
11
17
  end
12
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastic_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.6
4
+ version: 0.6.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arelastic