client: add each_file_info iterator
[ruby-mogilefs-client.git] / lib / mogilefs / paths_size.rb
blobba61f9ee8f19410414a09ff5dc08b42569324865
1 # -*- encoding: binary -*-
2 # This is only a hack for old MogileFS installs that didn't have file_info
3 require "net/http"
4 require "uri"
5 module MogileFS::PathsSize
6   def self.call(paths)
7     errors = {}
8     paths.each do |path|
9       uri = URI.parse(path)
10       begin
11         case r = Net::HTTP.start(uri.host, uri.port) { |x| x.head(uri.path) }
12         when Net::HTTPOK
13           return r["Content-Length"].to_i
14         else
15           errors[path] = r
16         end
17       rescue => err
18         errors[path] = err
19       end
20     end
21     errors = errors.map { |path,err| "#{path} - #{err.message} (#{err.class})" }
22     raise MogileFS::Error, "all paths failed with HEAD: #{errors.join(', ')}"
23   end
24 end