Skip to content

Commit 2a7c2a2

Browse files
AaronRustadkarmi
authored andcommitted
[CLIENT] Changed, that host ports are stored as numbers
One of the reasons is to be able to do something like: URI::HTTP.build(Elasticsearch::Client.new.transport.hosts.first) Closes #175
1 parent 8340e6a commit 2a7c2a2

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,25 @@ def __extract_hosts(hosts_config, options={})
143143
end
144144

145145
result = hosts.map do |host|
146-
case host
147-
when String
148-
if host =~ /^[a-z]+\:\/\//
149-
uri = URI.parse(host)
150-
{ :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port.to_s }
146+
host_parts = case host
147+
when String
148+
if host =~ /^[a-z]+\:\/\//
149+
uri = URI.parse(host)
150+
{ :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port }
151+
else
152+
host, port = host.split(':')
153+
{ :host => host, :port => port }
154+
end
155+
when URI
156+
{ :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port }
157+
when Hash
158+
host
151159
else
152-
host, port = host.split(':')
153-
{ :host => host, :port => port }
160+
raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
154161
end
155-
when URI
156-
{ :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port.to_s }
157-
when Hash
158-
host
159-
else
160-
raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
161-
end
162+
163+
host_parts[:port] = host_parts[:port].to_i unless host_parts[:port].nil?
164+
host_parts
162165
end
163166

164167
result.shuffle! if options[:randomize_hosts]

elasticsearch-transport/test/unit/client_test.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ def initialize(*); end
127127
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
128128
assert_equal 'myhost', hosts[0][:host]
129129
assert_equal 'https', hosts[0][:scheme]
130+
assert_nil hosts[0][:port]
131+
end
132+
133+
should "extract from hash with a port passed as a string" do
134+
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
135+
assert_equal 443, hosts[0][:port]
136+
end
137+
138+
should "extract from hash with a port passed as an integer" do
139+
hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
140+
assert_equal 443, hosts[0][:port]
130141
end
131142

132143
should "extract from Hashie::Mash" do
@@ -154,10 +165,10 @@ def initialize(*); end
154165
assert_equal 2, hosts.size
155166

156167
assert_equal 'host1', hosts[0][:host]
157-
assert_equal '1000', hosts[0][:port]
168+
assert_equal 1000, hosts[0][:port]
158169

159170
assert_equal 'host2', hosts[1][:host]
160-
assert_equal '2000', hosts[1][:port]
171+
assert_equal 2000, hosts[1][:port]
161172
end
162173

163174
should "extract path" do
@@ -171,7 +182,7 @@ def initialize(*); end
171182

172183
assert_equal 'https', hosts[0][:scheme]
173184
assert_equal 'myhost', hosts[0][:host]
174-
assert_equal '8080', hosts[0][:port]
185+
assert_equal 8080, hosts[0][:port]
175186
end
176187

177188
should "extract credentials" do
@@ -181,14 +192,14 @@ def initialize(*); end
181192
assert_equal 'USERNAME', hosts[0][:user]
182193
assert_equal 'PASSWORD', hosts[0][:password]
183194
assert_equal 'myhost', hosts[0][:host]
184-
assert_equal '8080', hosts[0][:port]
195+
assert_equal 8080, hosts[0][:port]
185196
end
186197

187198
should "pass hashes over" do
188199
hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]
189200

190201
assert_equal 'myhost', hosts[0][:host]
191-
assert_equal '1000', hosts[0][:port]
202+
assert_equal 1000, hosts[0][:port]
192203
assert_equal 'bar', hosts[0][:foo]
193204
end
194205

@@ -200,7 +211,7 @@ def initialize(*); end
200211
assert_equal 'USERNAME', hosts[0][:user]
201212
assert_equal 'PASSWORD', hosts[0][:password]
202213
assert_equal 'myhost', hosts[0][:host]
203-
assert_equal '4430', hosts[0][:port]
214+
assert_equal 4430, hosts[0][:port]
204215
end
205216

206217
should "split comma-separated URLs" do

0 commit comments

Comments
 (0)