class S3MPI::Interface

Constants

UUID

Attributes

bucket[R]

Return S3 bucket under use.

@return [AWS::S3::Bucket]

default_converter[R]

Return the default converter for store & read.

@return [String]

path[R]

Return S3 path under use.

@return [String]

Public Class Methods

new(bucket, path = '', default_converter = :json) click to toggle source

Create a new S3MPI object that responds to read and store.

@return [S3MPI::Interface]

@api public

# File lib/s3mpi/interface.rb, line 31
def initialize bucket, path = '', default_converter = :json
  @bucket = parse_bucket(bucket)
  @path   = path.freeze
  converter(default_converter) # verify it is valid
  @default_converter = default_converter
end

Public Instance Methods

exists?(key) click to toggle source

Check whether a key exists for this MPI interface.

@param [String] key

The key under which to save the object in the S3 bucket.

@return [TrueClass,FalseClass]

# File lib/s3mpi/interface.rb, line 81
def exists?(key)
  s3_object(key).exists?
end
read(key = nil, as: default_converter) click to toggle source

Read and de-serialize an object from an S3 bucket.

@param [String] key

The key under which to save the object in the S3 bucket.

@param [Symbol] :as

Which converter to use e.g. :json, :csv, :string
# File lib/s3mpi/interface.rb, line 65
def read(key = nil, as: default_converter)
  converter(as).parse(s3_object(key).read)
rescue AWS::S3::Errors::NoSuchKey
  nil
end
read_csv(key = nil) click to toggle source
# File lib/s3mpi/interface.rb, line 71
def read_csv(key = nil); read(key, as: :csv); end
read_json(key = nil) click to toggle source
# File lib/s3mpi/interface.rb, line 72
def read_json(key = nil); read(key, as: :json); end
read_string(key = nil) click to toggle source
# File lib/s3mpi/interface.rb, line 73
def read_string(key = nil); read(key, as: :string); end
store(obj, key = UUID, as: default_converter, tries: 1) click to toggle source

Store a Ruby object in an S3 bucket.

@param [Object] obj

Any convertable Ruby object (usually a hash or array).

@param [String] key

The key under which to save the object in the S3 bucket.

@param [Symbol] :as

Which converter to use e.g. :json, :csv, :string

@param [Integer] tries

The number of times to attempt to store the object.
# File lib/s3mpi/interface.rb, line 48
def store(obj, key = UUID, as: default_converter, tries: 1)
  key = SecureRandom.uuid if key.equal?(UUID)
  s3_object(key).write(converter(as).generate(obj))
rescue AWS::Errors::Base
  (tries -= 1) > 0 ? retry : raise
end
store_csv(obj, key = UUID) click to toggle source
# File lib/s3mpi/interface.rb, line 55
def store_csv(obj, key = UUID); store(obj, key, as: :csv); end
store_json(obj, key = UUID) click to toggle source
# File lib/s3mpi/interface.rb, line 56
def store_json(obj, key = UUID); store(obj, key, as: :json); end
store_string(obj, key = UUID) click to toggle source
# File lib/s3mpi/interface.rb, line 57
def store_string(obj, key = UUID); store(obj, key, as: :string); end