es 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/es.gemspec CHANGED
@@ -17,5 +17,9 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.add_dependency 'oj'
21
+ gem.add_dependency 'curb'
22
+
20
23
  gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'rake'
21
25
  end
data/lib/es.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'es/version'
2
+ require 'es/connection'
3
+ require 'es/raw_client'
2
4
  require 'es/client'
3
5
 
4
6
  module ES
@@ -1,73 +1,49 @@
1
+ # TODO find a better way to wrap serialization to client
1
2
  module ES
2
3
  class Client
3
4
  def initialize(opts = {})
4
- @host = opts[:host] || 'http://localhost:9200'
5
- @backend = opts[:backend] || Curl
6
- @serializer = opts[:serializer] || Oj
7
- end
8
-
9
- def create_index(name, definition)
10
- http_put(url(name), serialize(definition))
11
- end
12
-
13
- def delete_index(name)
14
- http_delete(url(name))
5
+ @dumper = opts[:dumper] || Oj
6
+ @client = opts[:client] || begin
7
+ RawClient.new(opts)
8
+ end
15
9
  end
16
10
 
17
11
  def bulk(requests, path = nil)
18
- serialized_requests = requests.collect do |request|
19
- serialize(request) + "\n"
20
- end.join('')
12
+ serialized = requests.map do |r|
13
+ @dumper.dump(r)
14
+ end.join("\n")
21
15
 
22
- http_post(url(path, :bulk), serialized_requests)
23
- end
16
+ response = @client.bulk(serialized, path)
24
17
 
25
- def index(path, data)
26
- http_put(url(path), serialize(data))
18
+ @dumper.load(response)
27
19
  end
28
20
 
29
- def search(path, query)
30
- http_post(url(path, :search), serialize(query))
21
+ def get(path)
22
+ response = @client.get(path)
23
+ @dumper.load(response)
31
24
  end
32
25
 
33
- private
34
- def url(path, action = nil)
35
- root = path ? "#{@host}/#{path}" : @host
36
- action ? "#{root}/_#{action}" : root
37
- end
38
-
39
- def http_put(url, data)
40
- # TODO refactor
41
- response = @backend.put(url, data)
42
- if [200, 201].include?(response.response_code)
43
- @serializer.load(response.body_str)
44
- else
45
- raise StandardError.new(response.body_str)
46
- end
26
+ def index(path, data)
27
+ serialized = @dumper.dump(data)
28
+ response = @client.index(path, serialized)
29
+ @dumper.load(response)
47
30
  end
48
31
 
49
- def http_post(url, data)
50
- # TODO refactor
51
- response = @backend.post(url, data)
52
- if response.response_code == 200
53
- @serializer.load(response.body_str)
54
- else
55
- raise StandardError.new(response.body_str)
56
- end
32
+ def search(path, data)
33
+ serialized = @dumper.dump(data)
34
+ response = @client.search(path, serialized)
35
+ @dumper.load(response)
57
36
  end
58
37
 
59
- def http_delete(url)
60
- # TODO refactor
61
- response = @backend.delete(url)
62
- if response.response_code == 200
63
- @serializer.load(response.body_str)
64
- else
65
- raise StandardError.new(response.body_str)
66
- end
38
+ def create_index(path, data)
39
+ serialized = @dumper.dump(data)
40
+ response = @client.create_index(path, serialized)
41
+ @dumper.load(response)
67
42
  end
68
43
 
69
- def serialize(data)
70
- @serializer.dump(data.as_json)
44
+ def delete_index(path)
45
+ response = @client.delete_index(path)
46
+ @dumper.load(response)
71
47
  end
72
48
  end
73
- end
49
+ end
@@ -0,0 +1,36 @@
1
+ module ES
2
+ class Connection
3
+ OK_HTTP_CODES = [200, 201]
4
+
5
+ def initialize(host, driver)
6
+ @host = host
7
+ @driver = driver
8
+ end
9
+
10
+ def request(method, path, data = nil)
11
+ response = translate_request(method, "#{@host}/#{path}", data)
12
+ raise Error.new(response) unless OK_HTTP_CODES.include?(response.response_code)
13
+ response.body_str
14
+ end
15
+
16
+ class Error < StandardError
17
+ def initialize(response)
18
+ super(response)
19
+ end
20
+ end
21
+
22
+ private
23
+ def translate_request(method, url, data)
24
+ case method
25
+ when :get
26
+ @driver.http_get(url)
27
+ when :put
28
+ @driver.http_put(url, data)
29
+ when :post
30
+ @driver.http_post(url, data)
31
+ when :delete
32
+ @driver.http_post(url)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module ES
2
+ class RawClient
3
+ def initialize(opts = {})
4
+ @connection = opts[:connection] || begin
5
+ host = opts[:host] || 'http://localhost:9200'
6
+ driver = opts[:driver] || Curl
7
+ Connection.new(host, driver)
8
+ end
9
+ end
10
+
11
+ def create_index(name, definition)
12
+ @connection.request(:put, name, definition)
13
+ end
14
+
15
+ def delete_index(name)
16
+ @connection.request(:delete, name)
17
+ end
18
+
19
+ def get(path)
20
+ @connection.request(:get, path)
21
+ end
22
+
23
+ def index(path, data)
24
+ @connection.request(:put, path, data)
25
+ end
26
+
27
+ def search(path, query)
28
+ @connection.request(:post, action_path(path, :search), query)
29
+ end
30
+
31
+ def bulk(path, requests)
32
+ @connection.request(:post, action_path(path, :bulk), requests)
33
+ end
34
+
35
+ private
36
+ def action_path(path, action)
37
+ "#{path}/_#{action}"
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module ES
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,47 @@
1
+ require 'es/raw_client'
2
+ require 'es/client'
3
+ require 'oj'
4
+
5
+ describe ES::Client do
6
+ let(:client) { double(:Client) }
7
+ subject { described_class.new(client: client) }
8
+ let(:raw_response) { '{}' }
9
+ let(:response) { {} }
10
+
11
+ it 'serializes data for .create_index' do
12
+ client.should_receive(:create_index).with('index/1', '[1,2,3]').and_return(raw_response)
13
+
14
+ subject.create_index('index/1', [1,2,3]).should == response
15
+ end
16
+
17
+ it 'serializes data for .delete_index' do
18
+ client.should_receive(:delete_index).with('index/1').and_return(raw_response)
19
+
20
+ subject.delete_index('index/1').should == response
21
+ end
22
+
23
+ it 'serializes data for .bulk' do
24
+ requests = [1, 2, 3]
25
+ client.should_receive(:bulk).with("1\n2\n3", 'an_index').and_return(raw_response)
26
+
27
+ subject.bulk(requests, 'an_index').should == response
28
+ end
29
+
30
+ it 'serializes data for .index' do
31
+ client.should_receive(:index).with('index/1', '[1,2,3]').and_return(raw_response)
32
+
33
+ subject.index('index/1', [1,2,3]).should == response
34
+ end
35
+
36
+ it 'serializes data for .get' do
37
+ client.should_receive(:get).with('index/1').and_return(raw_response)
38
+
39
+ subject.get('index/1').should == response
40
+ end
41
+
42
+ it 'serializes data for .search' do
43
+ client.should_receive(:search).with('index/1', '[1,2,3]').and_return(raw_response)
44
+
45
+ subject.search('index/1', [1,2,3]).should == response
46
+ end
47
+ end
@@ -0,0 +1,41 @@
1
+ require 'es/connection'
2
+
3
+ describe ES::Connection do
4
+ let(:data) { :data }
5
+ let(:response_body) { :response }
6
+ let(:response) { stub(response_code: 200, body_str: response_body) }
7
+ let(:driver) { double(:Driver) }
8
+
9
+ subject { described_class.new('http://localhost:9200', driver) }
10
+
11
+ it 'sends get request with url to driver' do
12
+ driver.should_receive(:http_get).with('http://localhost:9200/index/1').and_return(response)
13
+
14
+ subject.request(:get, 'index/1').should == response_body
15
+ end
16
+
17
+ it 'sends post request with url to driver' do
18
+ driver.should_receive(:http_post).with('http://localhost:9200/index', data).and_return(response)
19
+
20
+ subject.request(:post, 'index', data).should == response_body
21
+ end
22
+
23
+ it 'sends put request with url to driver' do
24
+ driver.should_receive(:http_put).with('http://localhost:9200/index', data).and_return(response)
25
+
26
+ subject.request(:put, 'index', data).should == response_body
27
+ end
28
+
29
+ it 'sends delete request with url to driver' do
30
+ driver.should_receive(:http_post).with('http://localhost:9200/index').and_return(response)
31
+
32
+ subject.request(:delete, 'index').should == response_body
33
+ end
34
+
35
+ it 'throws error if response code is not 2XX' do
36
+ error_response = stub(response_code: 400)
37
+ driver.should_receive(:http_post).and_return(error_response)
38
+
39
+ expect { subject.request(:post, 'index/_search', data) }.to raise_error(ES::Connection::Error)
40
+ end
41
+ end
@@ -0,0 +1,56 @@
1
+ require 'es/raw_client'
2
+
3
+ describe ES::RawClient do
4
+ let(:data) { {} }
5
+ let(:response) { :response }
6
+ let(:connection) { double(:Connection) }
7
+ let(:subject) { described_class.new({connection: connection}) }
8
+
9
+ context '.create_index' do
10
+ it 'sends PUT to connection' do
11
+ connection.should_receive(:request).with(:put, :an_index, data).and_return(response)
12
+
13
+ subject.create_index(:an_index, data).should == response
14
+ end
15
+ end
16
+
17
+ context '.delete_index' do
18
+ it 'sends DELETE to connection' do
19
+ connection.should_receive(:request).with(:delete, :an_index).and_return(response)
20
+
21
+ subject.delete_index(:an_index).should == response
22
+ end
23
+ end
24
+
25
+ context '.get' do
26
+ it 'sends GET to connection' do
27
+ connection.should_receive(:request).with(:get, 'index/1').and_return(response)
28
+
29
+ subject.get('index/1').should == response
30
+ end
31
+ end
32
+
33
+ context '.index' do
34
+ it 'sends PUT to connection' do
35
+ connection.should_receive(:request).with(:put, 'index/1', data).and_return(response)
36
+
37
+ subject.index('index/1', data).should == response
38
+ end
39
+ end
40
+
41
+ context '.search' do
42
+ it 'sends POST to connection' do
43
+ connection.should_receive(:request).with(:post, 'index/_search', data).and_return(response)
44
+
45
+ subject.search('index', data).should == response
46
+ end
47
+ end
48
+
49
+ context '.bulk' do
50
+ it 'sends POST to connection' do
51
+ connection.should_receive(:request).with(:post, 'index/_bulk', data).and_return(response)
52
+
53
+ subject.bulk('index', data).should == response
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,18 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
4
  prerelease:
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jan Suchal
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-06 00:00:00.000000000 Z
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: oj
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ type: :runtime
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: curb
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
14
46
  - !ruby/object:Gem::Dependency
15
47
  name: rspec
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
16
54
  requirement: !ruby/object:Gem::Requirement
17
55
  none: false
18
56
  requirements:
@@ -21,12 +59,22 @@ dependencies:
21
59
  version: '0'
22
60
  type: :development
23
61
  prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
24
64
  version_requirements: !ruby/object:Gem::Requirement
25
65
  none: false
26
66
  requirements:
27
67
  - - ! '>='
28
68
  - !ruby/object:Gem::Version
29
69
  version: '0'
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
30
78
  description: Simple wrapper for elasticsearch
31
79
  email:
32
80
  - johno@jsmf.net
@@ -42,7 +90,12 @@ files:
42
90
  - es.gemspec
43
91
  - lib/es.rb
44
92
  - lib/es/client.rb
93
+ - lib/es/connection.rb
94
+ - lib/es/raw_client.rb
45
95
  - lib/es/version.rb
96
+ - spec/es/client_spec.rb
97
+ - spec/es/connection_spec.rb
98
+ - spec/es/raw_client_spec.rb
46
99
  homepage: http://github.com/minio-sk/es
47
100
  licenses: []
48
101
  post_install_message:
@@ -63,8 +116,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
116
  version: '0'
64
117
  requirements: []
65
118
  rubyforge_project:
66
- rubygems_version: 1.8.24
119
+ rubygems_version: 1.8.25
67
120
  signing_key:
68
121
  specification_version: 3
69
122
  summary: Simple wrapper for elasticsearch.
70
- test_files: []
123
+ test_files:
124
+ - spec/es/client_spec.rb
125
+ - spec/es/connection_spec.rb
126
+ - spec/es/raw_client_spec.rb