stretcher 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -43,10 +43,27 @@ res.results # => [#<Hashie::Mash _id="123" text="Hello">]
43
43
  res.raw # => #<Hashie::Mash ...> Raw JSON from the search
44
44
  ```
45
45
 
46
+ ### Block Syntax
47
+
48
+ ```ruby
49
+ # A nested block syntax is also supported.
50
+ # with_server takes the same args as #new, but is amenable to blocks
51
+ Stretcher::Server.with_server('http://localhost:9200') {|srv|
52
+ srv.index(:foo) {|idx|
53
+ idx.type(:bar) {|t| [t.exists?, t.get_mapping] }
54
+ }
55
+ }
56
+ # => [true, #<Hashie::Mash bar=...>]
57
+ ```
58
+
46
59
  ### Multi Search
47
60
 
48
61
  ```ruby
49
- server.index('foo').msearch([{query: {match_all: {}}}])
62
+ # Within a single index
63
+ server.index(:foo).msearch([{query: {match_all: {}}}])
64
+ # => Returns an array of Stretcher::SearchResults
65
+ # Across multiple indexes
66
+ server.msearch([{index: 'foo'}, {query: {match_all: {}}}])
50
67
  # => Returns an array of Stretcher::SearchResults
51
68
  ```
52
69
 
@@ -11,8 +11,18 @@ module Stretcher
11
11
  @logger = options[:logger] || server.logger
12
12
  end
13
13
 
14
- def type(name)
15
- IndexType.new(self, name)
14
+ # Returns a Stretcher::IndexType object for the type +name+.
15
+ # Optionally takes a block, which will be passed a single arg with the Index obj
16
+ # The block syntax returns the evaluated value of the block
17
+ #
18
+ # Examples:
19
+ #
20
+ # my_index.index(:foo) # => #<Stretcher::Index ...>
21
+ #
22
+ # my_index.index(:foo) {|idx| 1+1} # => 2
23
+ def type(name, &block)
24
+ t = IndexType.new(self, name)
25
+ block ? block.call(t) : t
16
26
  end
17
27
 
18
28
  # Given a hash of documents, will bulk index
@@ -1,12 +1,21 @@
1
1
  module Stretcher
2
2
  class Server
3
3
  attr_reader :uri, :http, :logger
4
+
5
+ # Instantiate a new instance in a manner convenient for using the block syntax.
6
+ # Can be used interchangably with +Stretcher::Server.new+ but will return the value
7
+ # of the block if present. See the regular constructor for full options.
8
+ def self.with_server(*args)
9
+ s = self.new(*args)
10
+ yield s
11
+ end
4
12
 
5
13
  # Represents a Server context in elastic search.
14
+ # Use +connect+ when you want to use the block syntax.
6
15
  # The options hash takes an optional instance of Logger under :logger.
7
16
  #
8
17
  # Ex: server = Stretcher::Server.new('http://localhost:9200').
9
- def initialize(uri, options={})
18
+ def initialize(uri='http://localhost:9200', options={})
10
19
  @uri = uri
11
20
 
12
21
  @http = Faraday.new(:url => @uri) do |builder|
@@ -33,8 +42,18 @@ module Stretcher
33
42
  end
34
43
  end
35
44
 
36
- def index(name)
37
- Index.new(self, name, logger: logger)
45
+ # Returns a Stretcher::Index object for the index +name+.
46
+ # Optionally takes a block, which will be passed a single arg with the Index obj
47
+ # The block syntax returns the evaluated value of the block
48
+ #
49
+ # Examples:
50
+ #
51
+ # my_server.index(:foo) # => #<Stretcher::Index ...>
52
+ #
53
+ # my_server.index(:foo) {|idx| 1+1} # => 2
54
+ def index(name, &block)
55
+ idx = Index.new(self, name, logger: logger)
56
+ block ? block.call(idx) : idx
38
57
  end
39
58
 
40
59
  def bulk(data)
@@ -1,3 +1,3 @@
1
1
  module Stretcher
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -28,6 +28,16 @@ describe Stretcher::Index do
28
28
  index.exists?.should be_true
29
29
  end
30
30
 
31
+ it "should support block syntax for types" do
32
+ exposed = nil
33
+ res = index.type(:foo) {|t|
34
+ exposed = t
35
+ :retval
36
+ }
37
+ res.should == :retval
38
+ exposed.class.should == Stretcher::IndexType
39
+ end
40
+
31
41
  it "should return stats without error" do
32
42
  index.stats['_all'].should_not be_nil
33
43
  end
@@ -7,6 +7,16 @@ describe Stretcher::Server do
7
7
  server.class.should == Stretcher::Server
8
8
  end
9
9
 
10
+ it "should support the block friendly 'with_server'" do
11
+ exposed = nil
12
+ res = Stretcher::Server.with_server() {|s|
13
+ exposed = s
14
+ :retval
15
+ }
16
+ res.should == :retval
17
+ exposed.class.should == Stretcher::Server
18
+ end
19
+
10
20
  it "should properly return that our server is up" do
11
21
  server.up?.should be_true
12
22
  end
@@ -14,4 +24,14 @@ describe Stretcher::Server do
14
24
  it "should beget an index object cleanly" do
15
25
  server.index('foo').class.should == Stretcher::Index
16
26
  end
27
+
28
+ it "should support block syntax for indexes" do
29
+ exposed = nil
30
+ res = server.index(:foo) {|i|
31
+ exposed = i
32
+ :retval
33
+ }
34
+ res.should == :retval
35
+ exposed.class.should == Stretcher::Index
36
+ end
17
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
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: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday