Class: Bosh::Blobstore::SimpleBlobstoreClient

Inherits:
BaseClient show all
Defined in:
lib/blobstore_client/simple_blobstore_client.rb

Constant Summary

Constants inherited from Client

Client::PROVIDER_NAMES, Client::VERSION

Instance Method Summary collapse

Methods inherited from BaseClient

#create, #delete, #exists?, #get

Methods inherited from Client

create, safe_create

Constructor Details

#initialize(options) ⇒ SimpleBlobstoreClient

Returns a new instance of SimpleBlobstoreClient.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 8

def initialize(options)
  super(options)

  @client = HTTPClient.new
  @endpoint = @options[:endpoint]
  @bucket = @options[:bucket] || 'resources'
  @headers = {}

  user = @options[:user]
  password = @options[:password]

  if user && password
    @headers['Authorization'] = 'Basic ' +
      Base64.strict_encode64("#{user}:#{password}").strip
  end
end

Instance Method Details

#create_file(id, file) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 29

def create_file(id, file)
  response = @client.post(url(id), { content: file }, @headers)
  if response.status != 200
    raise BlobstoreError,
          "Could not create object, #{response.status}/#{response.content}"
  end
  response.content
end

#delete_object(id) ⇒ Object

Raises:



49
50
51
52
53
54
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 49

def delete_object(id)
  response = @client.delete(url(id), header: @headers)

  raise NotFound, "Object '#{id}' is not found, #{response.status}/#{response.content}" if response.status == 404
  raise BlobstoreError, "Could not delete object, #{response.status}/#{response.content}" if response.status != 204
end

#get_file(id, file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 38

def get_file(id, file)
  response = @client.get(url(id), header: @headers) do |block|
    file.write(block)
  end

  if response.status != 200
    raise BlobstoreError,
          "Could not fetch object, #{response.status}/#{response.content}"
  end
end

#object_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 56

def object_exists?(id)
  response = @client.head(url(id), header: @headers)
  if response.status == 200
    true
  elsif response.status == 404
    false
  else
    raise BlobstoreError, "Could not get object existence, #{response.status}/#{response.content}"
  end
end

#url(id = nil) ⇒ Object



25
26
27
# File 'lib/blobstore_client/simple_blobstore_client.rb', line 25

def url(id = nil)
  ["#{@endpoint}/#{@bucket}", id].compact.join('/')
end