Class: Blobby::HttpStore

Inherits:
Object
  • Object
show all
Defined in:
lib/blobby/http_store.rb

Overview

A BLOB store backed by HTTP.

Defined Under Namespace

Classes: StoredObject

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ HttpStore

Returns a new instance of HttpStore.



16
17
18
19
20
21
# File 'lib/blobby/http_store.rb', line 16

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

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



23
24
25
# File 'lib/blobby/http_store.rb', line 23

def base_uri
  @base_uri
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



24
25
26
# File 'lib/blobby/http_store.rb', line 24

def max_retries
  @max_retries
end

Class Method Details

.from_uri(uri) ⇒ Object



12
13
14
# File 'lib/blobby/http_store.rb', line 12

def self.from_uri(uri)
  new(uri)
end

Instance Method Details

#[](key) ⇒ Object



34
35
36
37
# File 'lib/blobby/http_store.rb', line 34

def [](key)
  KeyConstraint.must_allow!(key)
  StoredObject.new(self, key)
end

#available?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/blobby/http_store.rb', line 26

def available?
  with_http_connection do
    true
  end
rescue StandardError
  false
end

#with_http_connectionObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/blobby/http_store.rb', line 39

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
    end
  rescue *retryable_exceptions => e
    raise e if remaining_retry_intervals.empty?

    sleep(remaining_retry_intervals.shift) && retry
  end
end