elasticshell 0.0.5 → 0.0.6

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.
data/README.rdoc CHANGED
@@ -74,7 +74,7 @@ you can connect to servers by issuing the +connect+ command:
74
74
  === Understanding Scope
75
75
 
76
76
  Scopes are defined by the Elasticsearch REST API. Some scopes like
77
- <tt>/_cluster</tt> or <tt>/nodes</tt> are static and present for all
77
+ <tt>/_cluster</tt> or <tt>/_nodes</tt> are static and present for all
78
78
  Elasticsearch clusters. Other scopes like <tt>/my_index/my_type</tt>
79
79
  depend upon the particular cluster.
80
80
 
@@ -239,10 +239,10 @@ Or you can try sending it to Ruby itself:
239
239
 
240
240
  GET /my_index$ _search /tmp/query_that_needs_filtering.json | puts response["hits"]["hits"].first["body"]
241
241
 
242
- Everything to the right of the +|+ is executing within a Ruby process
243
- that has the +response+ and +request+ variables in scope. You can be
244
- even more free by just piping without any Ruby code, which will leave
245
- you in a Ruby (Pry) shell with the same binding.
242
+ Everything to the right of the <tt>|</tt> is executing within a Ruby
243
+ process that has the +response+ and +request+ variables in scope. You
244
+ can be even more free by just piping without any Ruby code, which will
245
+ leave you in a Ruby (Pry) shell with the same binding.
246
246
 
247
247
  GET /my_index$ _search /tmp/query_that_needs_interaction.json |
248
248
  >> response
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -9,29 +9,20 @@ module Elasticshell
9
9
  @options = options
10
10
  end
11
11
 
12
- def connect options={}
13
- servers = (@options.merge(options)[:servers] || Settings[:servers].to_s.split(","))
12
+ def safely_connect options={}
13
+ servers = @options.merge(options)[:servers]
14
14
  raise ClientError.new("Must provide at least one server to connect to.") if servers.empty?
15
- begin
16
- Elasticshell.debug("Connecting to Elasticsearch servers: #{servers.join(', ')}")
17
- begin
18
- timeout(5) do
19
- @client = ElasticSearch::Client.new(servers)
20
- end
21
- rescue Timeout::Error => e
22
- Elasticshell.error("Could not connect to Elasticsearch servers: #{servers.join(', ')}")
23
- return
24
- end
25
- Elasticshell.info("Connected to Elasticsearch servers: #{servers.join(', ')}")
26
- rescue ElasticSearch::ConnectionFailed => e
27
- raise ClientError.new("Could not connect to Elasticsearch server: #{servers.join(', ')}")
28
- end
15
+ connect(servers.dup, first_attempt=true)
29
16
  end
30
17
 
31
18
  def connected?
32
19
  @client
33
20
  end
34
21
 
22
+ def safely verb, params={}, options={}, body=''
23
+ request(verb, params, options.merge(:safely => true))
24
+ end
25
+
35
26
  def request verb, params={}, options={}, body=''
36
27
  raise ClientError.new("Not connected to any Elasticsearch servers.") unless connected?
37
28
  safe = options.delete(:safely)
@@ -50,8 +41,33 @@ module Elasticshell
50
41
  end
51
42
  end
52
43
  end
44
+
45
+ private
46
+
47
+ def connect servers, first_attempt=false
48
+ string = servers.join(' ')
49
+ Elasticshell.debug("Attempting to connect to #{string} ...") if first_attempt
50
+ raise ClientError.new("Timed out or failed to connect to #{string}") if servers.empty?
51
+ begin
52
+ server = servers.shift
53
+ timeout(5) do
54
+ @client = ElasticSearch::Client.new(server)
55
+ Elasticshell.info("Connected to #{server}")
56
+ end
57
+ rescue Timeout::Error => e
58
+ Elasticshell.debug("Timed out connecting to #{server}")
59
+ connect(servers)
60
+ rescue ElasticSearch::ConnectionFailed => e
61
+ Elasticshell.debug("Failure connecting to #{server}")
62
+ connect(servers)
63
+ rescue => e
64
+ Elasticshell.error("#{e.class} -- #{e.message}")
65
+ connect(servers)
66
+ end
67
+ end
53
68
 
54
69
  def perform_request verb, params, options, body
70
+ # p [verb, params, options, body]
55
71
  @client.execute(:standard_request, verb.downcase.to_sym, params, options, body)
56
72
  end
57
73
 
@@ -68,10 +84,6 @@ module Elasticshell
68
84
  # end
69
85
  end
70
86
 
71
- def safely verb, params={}, options={}, body=''
72
- request(verb, params, options.merge(:safely => true))
73
- end
74
-
75
87
  end
76
88
 
77
89
  end
@@ -11,7 +11,7 @@ module Elasticshell
11
11
  def evaluate!
12
12
  servers = (input.split(/\s+/, 2)[1] || '').split(/\s+/)
13
13
  if servers.empty?
14
- shell.client.connect()
14
+ shell.client.safely_connect()
15
15
  else
16
16
  uris = servers.map do |raw|
17
17
  has_port = raw =~ /:\d+/
@@ -31,7 +31,7 @@ module Elasticshell
31
31
  nil
32
32
  end
33
33
  end.compact
34
- shell.client.connect(:servers => uris)
34
+ shell.client.safely_connect(:servers => uris)
35
35
  end
36
36
  end
37
37
 
@@ -15,7 +15,7 @@ module Elasticshell
15
15
  end
16
16
 
17
17
  def perform_request!
18
- self.response = shell.client.request(request[:verb], {:op => request[:path] }, request[:query_options].merge(:log => shell.log_requests?), request[:body])
18
+ self.response = shell.client.safely(request[:verb], {:op => request[:path].gsub(/^\//,'') }, request[:query_options].merge(:log => shell.log_requests?), request[:body])
19
19
  end
20
20
 
21
21
  def pipe?
@@ -108,15 +108,11 @@ module Elasticshell
108
108
  end
109
109
 
110
110
  def prompt
111
- verb_string = self.class.formatted_verb(verb)
111
+ verb_string = Elasticshell.format((verb.to_s =~ /^(?:G|H)/i ? :passive_http_verb_format : :active_http_verb_format), "%v", verb.to_s.upcase)
112
112
  scope_string = Elasticshell.format((scope.exists? ? :existing_scope_format : :missing_scope_format), "%s", scope.path)
113
113
  Elasticshell.format((pretty? ? :pretty_prompt_format : :prompt_format), ["%s", "%v"], [scope_string, verb_string])
114
114
  end
115
115
 
116
- def self.formatted_verb verb
117
- Elasticshell.format((verb.to_s =~ /^(?:G|H)/i ? :passive_http_verb_format : :active_http_verb_format), "%v", verb.to_s.upcase)
118
- end
119
-
120
116
  def pretty?
121
117
  @pretty
122
118
  end
@@ -61,7 +61,7 @@ module Elasticshell
61
61
  end
62
62
 
63
63
  def self.request verb, host, path
64
- log.info(format(:http_request_format, ["%v", "%h", "%p"], [Shell.formatted_verb(verb), host, path]))
64
+ log.info(format(:http_request_format, ["%v", "%h", "%p"], [verb.to_s.upcase, host, path]))
65
65
  end
66
66
 
67
67
  def self.debug msg
@@ -24,7 +24,7 @@ describe Client do
24
24
  @client.stub!(:log_request)
25
25
  @client.stub!(:perform_request)
26
26
  @verb = "GET"
27
- @params = {:op => "/foobar"}
27
+ @params = {:op => "foobar"}
28
28
  end
29
29
 
30
30
  it "should log all requests by default" do
@@ -8,12 +8,12 @@ describe Commands::Connect do
8
8
  end
9
9
 
10
10
  it "should start a connection to the default servers" do
11
- @shell.client.should_receive(:connect).with()
11
+ @shell.client.should_receive(:safely_connect).with()
12
12
  expect(@shell.eval_line("connect"))
13
13
  end
14
14
 
15
15
  it "should start a connection to the given comma-separated servers" do
16
- @shell.client.should_receive(:connect).with(:servers => %w[http://123.123.123.123:9200/ http://321.321.321.321:9200/])
16
+ @shell.client.should_receive(:safely_connect).with(:servers => %w[http://123.123.123.123:9200/ http://321.321.321.321:9200/])
17
17
  expect(@shell.eval_line("connect http://123.123.123.123:9200 http://321.321.321.321:9200"))
18
18
  end
19
19
 
@@ -8,50 +8,50 @@ describe Commands::Request do
8
8
  end
9
9
 
10
10
  it "should recognize a simple request" do
11
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, kind_of(Hash), "").and_return('{}')
11
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, kind_of(Hash), "").and_return('{}')
12
12
  @shell.eval_line("_simple")
13
13
  end
14
14
 
15
15
  it "should recognize a simple request with a different verb" do
16
- @shell.client.should_receive(:request).with("POST", { :op => "/_simple" }, kind_of(Hash), "").and_return('{}')
16
+ @shell.client.should_receive(:safely).with("POST", { :op => "_simple" }, kind_of(Hash), "").and_return('{}')
17
17
  @shell.eval_line("POST _simple")
18
18
  end
19
19
 
20
20
  it "should recognize a request with an absolute path" do
21
- @shell.client.should_receive(:request).with("GET", { :op => "/some/thing" }, kind_of(Hash), "").and_return('{}')
21
+ @shell.client.should_receive(:safely).with("GET", { :op => "some/thing" }, kind_of(Hash), "").and_return('{}')
22
22
  @shell.eval_line("/some/thing")
23
23
  end
24
24
 
25
25
  it "should recognize a request with a query string" do
26
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, {"foo" => "bar", "baz" => "booz what", :log => true}, "").and_return('{}')
26
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, {"foo" => "bar", "baz" => "booz what", :log => true}, "").and_return('{}')
27
27
  @shell.eval_line("_simple?foo=bar&baz=booz+what")
28
28
  end
29
29
 
30
30
  it "should recognize a request with an inline body" do
31
- @shell.client.should_receive(:request).with("POST", { :op => "/_simple" }, kind_of(Hash), "{}").and_return('{}')
31
+ @shell.client.should_receive(:safely).with("POST", { :op => "_simple" }, kind_of(Hash), "{}").and_return('{}')
32
32
  @shell.eval_line("POST _simple {}")
33
33
  end
34
34
 
35
35
  it "should recognize a request with a body read from a local file" do
36
36
  File.should_receive(:exist?).with("/some/file.json").and_return(true)
37
37
  File.should_receive(:read).with("/some/file.json").and_return("{}")
38
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, kind_of(Hash), "{}").and_return('{}')
38
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, kind_of(Hash), "{}").and_return('{}')
39
39
  @shell.eval_line("_simple /some/file.json")
40
40
  end
41
41
 
42
42
  it "should recognize a request with a body read from STDIN" do
43
43
  @shell.input_stream.stub!(:gets).and_return("{}")
44
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, kind_of(Hash), "{}").and_return('{}')
44
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, kind_of(Hash), "{}").and_return('{}')
45
45
  @shell.eval_line("_simple -")
46
46
  end
47
47
 
48
48
  it "should be able to pipe the output of a request to Ruby inline" do
49
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, kind_of(Hash), "").and_return('{}')
49
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, kind_of(Hash), "").and_return('{}')
50
50
  @shell.eval_line("_simple | response")
51
51
  end
52
52
 
53
53
  it "should be able to pipe the output of a request to a RIPL session" do
54
- @shell.client.should_receive(:request).with("GET", { :op => "/_simple" }, kind_of(Hash), "").and_return('{}')
54
+ @shell.client.should_receive(:safely).with("GET", { :op => "_simple" }, kind_of(Hash), "").and_return('{}')
55
55
  require 'ripl'
56
56
  Ripl.should_receive(:start)
57
57
  @shell.eval_line("_simple |")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticshell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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-10-27 00:00:00.000000000 Z
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -187,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
187
  version: '0'
188
188
  segments:
189
189
  - 0
190
- hash: 4595416963629376856
190
+ hash: 3786860425467706238
191
191
  required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  none: false
193
193
  requirements: