elastic_record 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'elastic_record'
5
- s.version = '0.6.3'
5
+ s.version = '0.6.4'
6
6
  s.summary = 'Use Elastic Search with your objects'
7
7
  s.description = 'Find your records with elastic search'
8
8
 
@@ -1,3 +1,5 @@
1
+ require 'net/http'
2
+
1
3
  module ElasticRecord
2
4
  class Connection
3
5
  # :timeout: 10
@@ -16,40 +18,52 @@ module ElasticRecord
16
18
  end
17
19
 
18
20
  def head(path)
19
- http_request(Net::HTTP::Head, path).code
21
+ http_request(:head, path).code
20
22
  end
21
23
 
22
24
  def json_get(path, json = nil)
23
- json_request Net::HTTP::Get, path, json
25
+ json_request :get, path, json
24
26
  end
25
27
 
26
28
  def json_post(path, json = nil)
27
- json_request Net::HTTP::Post, path, json
29
+ json_request :post, path, json
28
30
  end
29
31
 
30
32
  def json_put(path, json = nil)
31
- json_request Net::HTTP::Put, path, json
33
+ json_request :put, path, json
32
34
  end
33
35
 
34
36
  def json_delete(path, json = nil)
35
- json_request Net::HTTP::Delete, path, json
37
+ json_request :delete, path, json
36
38
  end
37
39
 
38
- def json_request(request_klass, path, json)
40
+ def json_request(method, path, json)
39
41
  body = json.is_a?(Hash) ? ActiveSupport::JSON.encode(json) : json
40
- response = http_request(request_klass, path, body)
41
- json = ActiveSupport::JSON.decode response.body
42
+ response = http_request(method, path, body)
42
43
 
44
+ json = ActiveSupport::JSON.decode response.body
43
45
  raise json['error'] if json['error']
44
46
 
45
47
  json
46
48
  end
47
49
 
48
- def http_request(request_klass, path, body = nil)
49
- request = request_klass.new(path)
50
+ METHODS = {
51
+ head: Net::HTTP::Head,
52
+ get: Net::HTTP::Get,
53
+ post: Net::HTTP::Post,
54
+ put: Net::HTTP::Put,
55
+ delete: Net::HTTP::Delete
56
+ }
57
+
58
+ def http_request(method, path, body = nil)
59
+ request = METHODS[method].new(path)
50
60
  request.body = body
51
61
 
52
- http.request(request)
62
+ ActiveSupport::Notifications.instrument("request.elastic_record") do |payload|
63
+ payload[:method] = method
64
+ payload[:request_uri] = path
65
+ payload[:response] = http.request(request)
66
+ end
53
67
  end
54
68
 
55
69
  private
@@ -0,0 +1,15 @@
1
+ module ElasticRecord
2
+ class LogSubscriber < ActiveSupport::LogSubscriber
3
+ def request(event)
4
+ response = event.payload[:response]
5
+ info "#{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]
7
+ end
8
+
9
+ # def logger
10
+ # Rails.logger
11
+ # end
12
+ end
13
+ end
14
+
15
+ ElasticRecord::LogSubscriber.attach_to :elastic_record
@@ -1,4 +1,7 @@
1
1
  module ElasticRecord
2
2
  module ActiveRecord
3
+ def initialize_from_elastic_search(results)
4
+
5
+ end
3
6
  end
4
7
  end
@@ -1,5 +1,11 @@
1
1
  module ElasticRecord
2
2
  class Railtie < Rails::Railtie
3
- rake_tasks { load "elastic_record/tasks/index.rake" }
3
+ initializer 'elastic_record.require_log_subscriber' do
4
+ require 'elastic_record/log_subscriber'
5
+ end
6
+
7
+ rake_tasks do
8
+ load "elastic_record/tasks/index.rake"
9
+ end
4
10
  end
5
11
  end
@@ -0,0 +1,27 @@
1
+ require "helper"
2
+ require "active_support/log_subscriber/test_helper"
3
+ require "elastic_record/log_subscriber"
4
+
5
+ class ElasticRecord::LogSubscriberTest < ActiveSupport::TestCase
6
+ include ActiveSupport::LogSubscriber::TestHelper
7
+
8
+ def setup
9
+ super
10
+
11
+ ElasticRecord::LogSubscriber.attach_to :elastic_record
12
+ end
13
+
14
+ # def set_logger(logger)
15
+ # ElasticRecord::Model.logger = logger
16
+ # end
17
+
18
+ def test_request_notification
19
+ Widget.elastic_connection.head "/widgets"
20
+
21
+ wait
22
+
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])
26
+ end
27
+ 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.3
4
+ version: 0.6.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -66,6 +66,7 @@ files:
66
66
  - lib/elastic_record/index/manage.rb
67
67
  - lib/elastic_record/index/mapping.rb
68
68
  - lib/elastic_record/index/percolator.rb
69
+ - lib/elastic_record/log_subscriber.rb
69
70
  - lib/elastic_record/model.rb
70
71
  - lib/elastic_record/orm/active_record.rb
71
72
  - lib/elastic_record/railtie.rb
@@ -86,6 +87,7 @@ files:
86
87
  - test/elastic_record/index/mapping_test.rb
87
88
  - test/elastic_record/index/percolator_test.rb
88
89
  - test/elastic_record/index_test.rb
90
+ - test/elastic_record/log_subscriber_test.rb
89
91
  - test/elastic_record/model_test.rb
90
92
  - test/elastic_record/relation/batches_test.rb
91
93
  - test/elastic_record/relation/delegation_test.rb