stretch 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,83 +1,21 @@
1
1
  require "stretch/connection"
2
- require "stretch/uri_builder"
2
+ require "stretch/cluster"
3
+ require "stretch/index"
3
4
 
4
5
  module Stretch
5
6
  class Client
6
- attr_reader :connection, :scope
7
-
8
7
  def initialize options = {}
9
8
  self.tap do
10
- @scope = {
11
- :cluster => false,
12
- :index => nil
13
- }
14
-
15
9
  @connection = Stretch::Connection.new options
16
10
  end
17
11
  end
18
12
 
19
- # Chainable methods
20
13
  def cluster
21
- self.tap do
22
- scope[:cluster] = true
23
- scope[:index] = nil
24
- end
14
+ Cluster.new @connection
25
15
  end
26
16
 
27
17
  def index name
28
- self.tap do
29
- scope[:cluster] = false
30
- scope[:index] = name
31
- end
32
- end
33
-
34
- # End points
35
- def health options = {}
36
- with_scopes :cluster, :index do
37
- connection.get build_path("/health"), options
38
- end
39
- end
40
-
41
- def state options = {}
42
- with_scopes :cluster do
43
- connection.get build_path("/state"), options
44
- end
45
- end
46
-
47
- def settings options = {}
48
- with_scopes :cluster, :index do
49
- if options.any?
50
- connection.put build_path("/settings"), options
51
- else
52
- connection.get build_path("/settings")
53
- end
54
- end
55
- end
56
-
57
- def open!
58
- with_scopes :index do
59
- connection.post build_path("/_open")
60
- end
61
- end
62
-
63
- def close!
64
- with_scopes :index do
65
- connection.post build_path("/_close")
66
- end
67
- end
68
-
69
- private
70
- def build_path path
71
- Stretch::URIBuilder.build_from_scope scope, path
72
- end
73
-
74
- def with_scopes *scopes, &block
75
- if scopes.any? {|s| scope.has_key?(s) && scope[s] }
76
- yield
77
- else
78
- raise InvalidScope,
79
- "Requires one of the following scopes: #{scopes.inspect}. #{scope.inspect} given."
80
- end
18
+ Index.new name, @connection
81
19
  end
82
20
  end
83
21
  end
@@ -0,0 +1,43 @@
1
+ module Stretch
2
+ class Cluster
3
+ attr_reader :connection
4
+
5
+ def initialize connection
6
+ @connection = connection
7
+ end
8
+
9
+ def health options = {}
10
+ indices = options.delete(:indices) if options.has_key? :indices
11
+
12
+ path = "/health"
13
+
14
+ if indices
15
+ path << "/#{[indices].flatten.join(',')}"
16
+ options[:level] = "indices"
17
+ end
18
+
19
+ get path, options
20
+ end
21
+
22
+ def state options = {}
23
+ get "/state", options
24
+ end
25
+
26
+ def settings options = {}
27
+ if options.any?
28
+ put "/settings", options
29
+ else
30
+ get "/settings"
31
+ end
32
+ end
33
+
34
+ private
35
+ def get path, options = {}
36
+ @connection.request :get, "/_cluster#{path}", options
37
+ end
38
+
39
+ def put path, options = {}
40
+ @connection.request :put, "/_cluster#{path}", options
41
+ end
42
+ end
43
+ end
@@ -11,23 +11,6 @@ module Stretch
11
11
  @connection = Faraday.new(options)
12
12
  end
13
13
 
14
- def get path, options = {}
15
- request :get, path, options
16
- end
17
-
18
- def post path, options = {}
19
- request :post, path, options
20
- end
21
-
22
- def put path, options = {}
23
- request :put, path, options
24
- end
25
-
26
- def delete path, options = {}
27
- request :delete, path, options
28
- end
29
-
30
- private
31
14
  def request method, path, options = {}
32
15
  validate_request_method method
33
16
 
@@ -46,6 +29,7 @@ module Stretch
46
29
  handle_response response
47
30
  end
48
31
 
32
+ private
49
33
  def validate_request_method method
50
34
  unless REQUEST_METHODS.member? method
51
35
  raise Stretch::UnsupportedRequestMethod, "#{method} is not supported!"
@@ -55,7 +39,7 @@ module Stretch
55
39
  def handle_response response
56
40
  if response.success?
57
41
  if response.body.empty?
58
- request :get, path
42
+ { "ok" => true }
59
43
  else
60
44
  MultiJson.load response.body
61
45
  end
@@ -0,0 +1,41 @@
1
+ module Stretch
2
+ class Index
3
+ attr_reader :name, :connection
4
+
5
+ def initialize name, connection
6
+ raise ArgumentError if name.nil? || name.empty?
7
+
8
+ @name = name
9
+ @connection = connection
10
+ end
11
+
12
+ def settings options = {}
13
+ if options.any?
14
+ put "/_settings", options
15
+ else
16
+ get("/_settings")[@name]["settings"]
17
+ end
18
+ end
19
+
20
+ def open
21
+ post "/_open"
22
+ end
23
+
24
+ def close
25
+ post "/_close"
26
+ end
27
+
28
+ private
29
+ def get path, options = {}
30
+ @connection.request :get, "/#{@name}#{path}", options
31
+ end
32
+
33
+ def put path, options = {}
34
+ @connection.request :put, "/#{@name}#{path}", options
35
+ end
36
+
37
+ def post path, options = {}
38
+ @connection.request :post, "/#{@name}#{path}", options
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module Stretch
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -4,150 +4,12 @@ require "stretch/client"
4
4
 
5
5
  describe Stretch::Client do
6
6
  let(:instance) { described_class.new }
7
- let(:connection) { MiniTest::Mock.new }
8
7
 
9
- describe ".new" do
10
- describe "scope defaults" do
11
- it "index to nil" do
12
- assert_nil instance.scope[:index]
13
- end
14
-
15
- it "cluster to false" do
16
- refute instance.scope[:cluster]
17
- end
18
- end
19
- end
20
-
21
- it "can chain index" do
22
- assert instance.index("foo").is_a?(Stretch::Client)
23
- assert_equal "foo", instance.scope[:index]
24
- end
25
-
26
- it "can chain cluster" do
27
- assert instance.cluster.is_a?(Stretch::Client)
28
- assert instance.scope[:cluster]
29
- end
30
-
31
- describe "#health" do
32
- it "requires either a cluster scope or an index scope" do
33
- instance.stub :scope, { :cluster => false, :index => nil } do
34
- assert_raises Stretch::InvalidScope do
35
- instance.health
36
- end
37
- end
38
- end
39
-
40
- it "requests cluster health if the scope is cluster" do
41
- instance.stub :connection, connection do
42
- connection.expect :get, { "status" => "ok" }, ["/_cluster/health", {}]
43
- instance.cluster.health
44
-
45
- connection.verify
46
- end
47
- end
48
-
49
- it "requests index health if the scope is an index" do
50
- instance.stub :connection, connection do
51
- connection.expect :get, { "status" => "ok" }, ["/foo/health", {}]
52
- instance.index("foo").health
53
-
54
- connection.verify
55
- end
56
- end
57
- end
58
-
59
- describe "#state" do
60
- it "requires a cluster scope" do
61
- instance.stub :scope, { :cluster => false } do
62
- assert_raises Stretch::InvalidScope do
63
- instance.state
64
- end
65
- end
66
- end
67
-
68
- it "can return the state of a cluster" do
69
- instance.connection.stub :get, { "status" => "ok" } do
70
- assert_equal "ok", instance.cluster.state["status"]
71
- end
72
- end
73
- end
74
-
75
- describe "#settings" do
76
- it "requires either a cluster scope or an index scope" do
77
- instance.stub :scope, { :cluster => false, :index => nil } do
78
- assert_raises Stretch::InvalidScope do
79
- instance.settings
80
- end
81
- end
82
- end
83
-
84
- describe "no options given" do
85
- it "performs a get request for the settings" do
86
- response = { "persistent" => { "foo" => 1 } }
87
-
88
- instance.stub :connection, connection do
89
- connection.expect :get, response, ["/_cluster/settings"]
90
- assert_equal 1, instance.cluster.settings["persistent"]["foo"]
91
-
92
- connection.verify
93
- end
94
- end
95
- end
96
-
97
- describe "options given" do
98
- it "performs a put request with options for the settings" do
99
- params = { :index => { :refresh_interval => -1 } }
100
- response = { "index" => {"refresh_interval" => -1} }
101
-
102
- instance.stub :connection, connection do
103
- connection.expect :put, response, ["/foo/settings", params]
104
- assert_equal -1, instance.index("foo").settings(params)["index"]["refresh_interval"]
105
-
106
- connection.verify
107
- end
108
- end
109
- end
8
+ it "#index" do
9
+ assert instance.index("foo").is_a?(Stretch::Index)
110
10
  end
111
11
 
112
- describe "#open!" do
113
- it "requires the index scope" do
114
- instance.stub :scope, { :cluster => true, :index => nil } do
115
- assert_raises Stretch::InvalidScope do
116
- instance.open!
117
- end
118
- end
119
- end
120
-
121
- it "performs a post request to open the index" do
122
- response = { "ok" => true, "acknowledged" => true }
123
-
124
- instance.stub :connection, connection do
125
- connection.expect :post, response, ["/foo/_open"]
126
- assert_equal true, instance.index("foo").open!["ok"]
127
-
128
- connection.verify
129
- end
130
- end
131
- end
132
-
133
- describe "#close!" do
134
- it "requires the index scope" do
135
- instance.stub :scope, { :cluster => true, :index => nil } do
136
- assert_raises Stretch::InvalidScope do
137
- instance.close!
138
- end
139
- end
140
- end
141
-
142
- it "performs a post request to close the index" do
143
- response = { "ok" => true, "acknowledged" => true }
144
-
145
- instance.stub :connection, connection do
146
- connection.expect :post, response, ["/foo/_close"]
147
- assert_equal true, instance.index("foo").close!["ok"]
148
-
149
- connection.verify
150
- end
151
- end
12
+ it "#cluster" do
13
+ assert instance.cluster.is_a?(Stretch::Cluster)
152
14
  end
153
15
  end
@@ -0,0 +1,74 @@
1
+ require "test_helper"
2
+
3
+ require "stretch/cluster"
4
+
5
+ describe Stretch::Cluster do
6
+ let(:connection) { MiniTest::Mock.new }
7
+ let(:instance) { described_class.new connection }
8
+
9
+ describe "#initialize" do
10
+ it "assigns connection" do
11
+ connection.expect :==, true, [connection]
12
+
13
+ assert_equal connection, described_class.new(connection).connection
14
+ end
15
+ end
16
+
17
+ describe "#health" do
18
+ it "sends a get request for health with options" do
19
+ connection.expect :request,
20
+ { "status" => "ok" },
21
+ [:get, "/_cluster/health", { :timeout => "2s" }]
22
+
23
+ instance.health :timeout => "2s"
24
+
25
+ connection.verify
26
+ end
27
+
28
+ describe "with indices option" do
29
+ it "sends a get request for indices health with correct level" do
30
+ connection.expect :request,
31
+ { "indices" => { "foo" => {}, "bar" => {} } },
32
+ [:get, "/_cluster/health/foo,bar", { :level => "indices", :timeout => "2s" }]
33
+
34
+ instance.health :indices => ["foo", "bar"], :timeout => "2s"
35
+
36
+ connection.verify
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "#state" do
42
+ it "sends a get request for the state of the cluster" do
43
+ connection.expect :request,
44
+ { "nodes" => [1,2,3], "metadata" => { "foo" => "bar" }},
45
+ [:get, "/_cluster/state", { :filter_routing_table => true }]
46
+
47
+ instance.state :filter_routing_table => true
48
+
49
+ connection.verify
50
+ end
51
+ end
52
+
53
+ describe "#settings" do
54
+ it "sends a put request if there are any options" do
55
+ connection.expect :request,
56
+ { "ok" => true },
57
+ [:put, "/_cluster/settings", { :foo => "bar" }]
58
+
59
+ instance.settings :foo => "bar"
60
+
61
+ connection.verify
62
+ end
63
+
64
+ it "sends a get request if there are no options" do
65
+ connection.expect :request,
66
+ { "foo" => "bar" },
67
+ [:get, "/_cluster/settings", {}]
68
+
69
+ instance.settings
70
+
71
+ connection.verify
72
+ end
73
+ end
74
+ end
@@ -4,39 +4,4 @@ require "stretch/connection"
4
4
 
5
5
  describe Stretch::Connection do
6
6
  let(:instance) { described_class.new }
7
- let(:response) do
8
- Struct.new :response do
9
- def body
10
- MultiJson.dump({ "status" => "ok" })
11
- end
12
-
13
- def success?
14
- true
15
- end
16
- end.new
17
- end
18
-
19
- it "accepts get requests" do
20
- instance.connection.stub :get, response do
21
- instance.get "/foo"
22
- end
23
- end
24
-
25
- it "accepts post requests" do
26
- instance.connection.stub :post, response do
27
- instance.post "/foo"
28
- end
29
- end
30
-
31
- it "accepts put requests" do
32
- instance.connection.stub :put, response do
33
- instance.put "/foo"
34
- end
35
- end
36
-
37
- it "accepts delete requests" do
38
- instance.connection.stub :delete, response do
39
- instance.delete "/foo"
40
- end
41
- end
42
7
  end
@@ -0,0 +1,77 @@
1
+ require "test_helper"
2
+
3
+ require "stretch/index"
4
+
5
+ describe Stretch::Index do
6
+ let(:connection) { MiniTest::Mock.new }
7
+ let(:instance) { described_class.new "foo", connection }
8
+
9
+ describe "#initialize" do
10
+ it "raises an ArgumentError if the index name is nil" do
11
+ assert_raises ArgumentError do
12
+ described_class.new nil, connection
13
+ end
14
+ end
15
+
16
+ it "raises an ArgumentError if the index name is empty" do
17
+ assert_raises ArgumentError do
18
+ described_class.new "", connection
19
+ end
20
+ end
21
+
22
+ it "sets the name and the connection" do
23
+ connection.expect :==, true, [connection]
24
+
25
+ assert_equal "foo", instance.name
26
+ assert_equal connection, instance.connection
27
+
28
+ connection.verify
29
+ end
30
+ end
31
+
32
+ describe "#settings" do
33
+ it "sends a put request if there are any options" do
34
+ connection.expect :request,
35
+ { "ok" => true },
36
+ [:put, "/foo/_settings", { :foo => "bar" }]
37
+
38
+ instance.settings :foo => "bar"
39
+
40
+ connection.verify
41
+ end
42
+
43
+ it "sends a get request if there are no options" do
44
+ connection.expect :request,
45
+ { "foo" => "bar" },
46
+ [:get, "/foo/_settings", {}]
47
+
48
+ instance.settings
49
+
50
+ connection.verify
51
+ end
52
+ end
53
+
54
+ describe "#open" do
55
+ it "sends a post request to open the index" do
56
+ connection.expect :request,
57
+ { "ok" => true },
58
+ [:post, "/foo/_close", {}]
59
+
60
+ instance.close
61
+
62
+ connection.verify
63
+ end
64
+ end
65
+
66
+ describe "#close" do
67
+ it "sends a post request to close the index" do
68
+ connection.expect :request,
69
+ { "ok" => true },
70
+ [:post, "/foo/_open", {}]
71
+
72
+ instance.open
73
+
74
+ connection.verify
75
+ end
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-04-14 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -123,14 +123,18 @@ files:
123
123
  - Rakefile
124
124
  - lib/stretch.rb
125
125
  - lib/stretch/client.rb
126
+ - lib/stretch/cluster.rb
126
127
  - lib/stretch/connection.rb
128
+ - lib/stretch/index.rb
127
129
  - lib/stretch/uri_builder.rb
128
130
  - lib/stretch/version.rb
129
131
  - script/bootstrap
130
132
  - script/test
131
133
  - stretch.gemspec
132
134
  - test/stretch/client_test.rb
135
+ - test/stretch/cluster_test.rb
133
136
  - test/stretch/connection_test.rb
137
+ - test/stretch/index_test.rb
134
138
  - test/stretch_test.rb
135
139
  - test/test_helper.rb
136
140
  homepage: https://github.com/wfarr/stretch
@@ -146,18 +150,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
150
  - - ! '>='
147
151
  - !ruby/object:Gem::Version
148
152
  version: '0'
149
- segments:
150
- - 0
151
- hash: 3712832259908765
152
153
  required_rubygems_version: !ruby/object:Gem::Requirement
153
154
  none: false
154
155
  requirements:
155
156
  - - ! '>='
156
157
  - !ruby/object:Gem::Version
157
158
  version: '0'
158
- segments:
159
- - 0
160
- hash: 3712832259908765
161
159
  requirements: []
162
160
  rubyforge_project:
163
161
  rubygems_version: 1.8.23
@@ -167,6 +165,8 @@ summary: It's not anywhere near complete at this time, but the code is pretty al
167
165
  right allegedly.
168
166
  test_files:
169
167
  - test/stretch/client_test.rb
168
+ - test/stretch/cluster_test.rb
170
169
  - test/stretch/connection_test.rb
170
+ - test/stretch/index_test.rb
171
171
  - test/stretch_test.rb
172
172
  - test/test_helper.rb