Class: S3MPI::Interface

Inherits:
Object
  • Object
show all
Includes:
Converters, S3
Defined in:
lib/s3mpi/interface.rb

Constant Summary collapse

UUID =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Converters

#converter

Methods included from S3

#s3_object

Constructor Details

#initialize(bucket, path = '', default_converter = :json) ⇒ S3MPI::Interface

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



31
32
33
34
35
36
# 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

Instance Attribute Details

#bucketAWS::S3::Bucket (readonly)

Return S3 bucket under use.

Returns:

  • (AWS::S3::Bucket)


14
15
16
# File 'lib/s3mpi/interface.rb', line 14

def bucket
  @bucket
end

#default_converterString (readonly)

Return the default converter for store & read.

Returns:

  • (String)


24
25
26
# File 'lib/s3mpi/interface.rb', line 24

def default_converter
  @default_converter
end

#pathString (readonly)

Return S3 path under use.

Returns:

  • (String)


19
20
21
# File 'lib/s3mpi/interface.rb', line 19

def path
  @path
end

Instance Method Details

#exists?(key) ⇒ TrueClass, FalseClass

Check whether a key exists for this MPI interface.

Parameters:

  • key (String)

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

Returns:

  • (TrueClass, FalseClass)


81
82
83
# File 'lib/s3mpi/interface.rb', line 81

def exists?(key)
  s3_object(key).exists?
end

#read(key = nil, as: default_converter) ⇒ Object

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

Parameters:

  • key (String) (defaults to: nil)

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

  • :as (Symbol)

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



65
66
67
68
69
# 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) ⇒ Object



71
# File 'lib/s3mpi/interface.rb', line 71

def read_csv(key = nil); read(key, as: :csv); end

#read_json(key = nil) ⇒ Object



72
# File 'lib/s3mpi/interface.rb', line 72

def read_json(key = nil); read(key, as: :json); end

#read_string(key = nil) ⇒ Object



73
# 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) ⇒ Object

Store a Ruby object in an S3 bucket.

Parameters:

  • obj (Object)

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

  • key (String) (defaults to: UUID)

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

  • :as (Symbol)

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

  • tries (Integer) (defaults to: 1)

    The number of times to attempt to store the object.



48
49
50
51
52
53
# 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) ⇒ Object



55
# File 'lib/s3mpi/interface.rb', line 55

def store_csv(obj, key = UUID); store(obj, key, as: :csv); end

#store_json(obj, key = UUID) ⇒ Object



56
# File 'lib/s3mpi/interface.rb', line 56

def store_json(obj, key = UUID); store(obj, key, as: :json); end

#store_string(obj, key = UUID) ⇒ Object



57
# File 'lib/s3mpi/interface.rb', line 57

def store_string(obj, key = UUID); store(obj, key, as: :string); end