lib/blobby/http_store.rb in blobby-1.0.1 vs lib/blobby/http_store.rb in blobby-1.1.0

- old
+ new

@@ -5,17 +5,22 @@ # A BLOB store backed by HTTP. # class HttpStore - def initialize(base_url, options = {}) - @base_url = base_url - @base_url += "/" unless @base_url.end_with?("/") + def self.from_uri(uri) + new(uri) + end + + def initialize(uri, options = {}) + uri = URI(uri) + uri = URI("#{uri}/") unless uri.to_s.end_with?("/") + @base_uri = uri @max_retries = options.fetch(:max_retries, 2) end - attr_reader :base_url + attr_reader :base_uri attr_reader :max_retries def available? with_http_connection do true @@ -27,14 +32,10 @@ def [](key) KeyConstraint.must_allow!(key) StoredObject.new(self, key) end - def base_uri - URI(base_url) - end - def with_http_connection remaining_retry_intervals = retry_intervals(max_retries) begin Net::HTTP.start(base_uri.host, base_uri.port) do |http| yield http, base_uri.path @@ -91,10 +92,14 @@ end end end def write(content) - content = content.read if content.respond_to?(:read) + if content.respond_to?(:read) + content = content.read + else + content = content.dup + end with_http_connection do |http, path| put = Net::HTTP::Put.new(path) put.body = content put["Content-Type"] = "application/octet-stream" response = http.request(put)