lib/crate_ruby/client.rb in crate_ruby-0.0.1 vs lib/crate_ruby/client.rb in crate_ruby-0.0.2

- old
+ new

@@ -5,17 +5,17 @@ DEFAULT_HOST = "127.0.0.1" DEFAULT_PORT = "4200" attr_accessor :logger + # Currently only a single server is supported. Fail over will be implemented in upcoming versions + # @param [Array] servers An Array of servers including ports [127.0.0.1:4200, 10.0.0.1:4201] # @param [opts] Optional paramters - # * host: ip or host name, defaults to 127.0.0.1 - # * port: defaults to 4200 - def initialize(opts = {}) - @host = opts[:host] || DEFAULT_HOST - @port = opts[:port] || DEFAULT_PORT - @uri = "http://#{DEFAULT_HOST}:#{DEFAULT_PORT}" + # * logger: Custom Logger + def initialize(servers = [], opts = {}) + @servers = servers + @servers << "#{DEFAULT_HOST}:#{DEFAULT_PORT}" if servers.empty? @logger = opts[:logger] || CrateRuby.logger end def inspect %Q{#<CrateRuby::Client:#{object_id}, @uri="#{@uri}">} @@ -66,11 +66,11 @@ # @param [String] sql statement to execute # @return [ResultSet, false] def execute(sql) req = Net::HTTP::Post.new("/_sql", initheader = {'Content-Type' => 'application/json'}) req.body = {"stmt" => sql}.to_json - response = Net::HTTP.new(@host, @port).start { |http| http.request(req) } + response = request(req) success = case response.code when "200" ResultSet.new response.body when "400" @logger.info(response.body) @@ -89,20 +89,19 @@ def blob_put(table, digest, data) uri = blob_path(table, digest) @logger.debug("BLOB PUT #{uri}") req = Net::HTTP::Put.new(blob_path(table, digest)) req.body = data - #req["Content-Type"] = "multipart/form-data, boundary=#{boundary}" - response = Net::HTTP.new(@host, @port).start { |http| http.request(req) } + response = request(req) success = case response.code when "201" true else @logger.info("Response #{response.code}: " + response.body) false end - success + success end # Download blob # @param [String] table # @param [String] digest SHA1 hexdigest @@ -110,13 +109,11 @@ # @return [Blob] File data to write to file or use directly def blob_get(table, digest) uri = blob_path(table, digest) @logger.debug("BLOB GET #{uri}") req = Net::HTTP::Get.new(uri) - response = Net::HTTP.new(@host, @port).start do |http| - http.request(req) - end + response = request(req) case response.code when "200" response.body else @logger.info("Response #{response.code}: #{response.body}") @@ -131,11 +128,11 @@ # @return [Boolean] def blob_delete(table, digest) uri = blob_path(table, digest) @logger.debug("BLOB DELETE #{uri}") req = Net::HTTP::Delete.new(uri) - response = Net::HTTP.new(@host, @port).start { |http| http.request(req) } + response = request(req) success = case response.code when "200" true else @logger.info("Response #{response.code}: #{response.body}") @@ -148,8 +145,16 @@ def blob_path(table, digest) "/_blobs/#{table}/#{digest}" end + def connection + host, port = @servers.first.split(':'); + Net::HTTP.new(host, port) + end + + def request(req) + connection.start { |http| http.request(req) } + end end end \ No newline at end of file