Class: S3MPI::Interface
- Inherits:
-
Object
- Object
- S3MPI::Interface
- Includes:
- Converters, S3
- Defined in:
- lib/s3mpi/interface.rb
Constant Summary collapse
- UUID =
Object.new.freeze
Instance Attribute Summary collapse
-
#bucket ⇒ AWS::S3::Bucket
readonly
Return S3 bucket under use.
-
#default_converter ⇒ String
readonly
Return the default converter for store & read.
-
#path ⇒ String
readonly
Return S3 path under use.
Instance Method Summary collapse
-
#exists?(key) ⇒ TrueClass, FalseClass
Check whether a key exists for this MPI interface.
-
#initialize(bucket, path = '', default_converter = :json) ⇒ S3MPI::Interface
constructor
Create a new S3MPI object that responds to #read and #store.
-
#read(key = nil, as: default_converter) ⇒ Object
Read and de-serialize an object from an S3 bucket.
- #read_csv(key = nil) ⇒ Object
- #read_json(key = nil) ⇒ Object
- #read_string(key = nil) ⇒ Object
-
#store(obj, key = UUID, as: default_converter, tries: 1) ⇒ Object
Store a Ruby object in an S3 bucket.
- #store_csv(obj, key = UUID) ⇒ Object
- #store_json(obj, key = UUID) ⇒ Object
- #store_string(obj, key = UUID) ⇒ Object
Methods included from Converters
Methods included from S3
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
#bucket ⇒ AWS::S3::Bucket (readonly)
Return S3 bucket under use.
14 15 16 |
# File 'lib/s3mpi/interface.rb', line 14 def bucket @bucket end |
#default_converter ⇒ String (readonly)
Return the default converter for store & read.
24 25 26 |
# File 'lib/s3mpi/interface.rb', line 24 def default_converter @default_converter end |
#path ⇒ String (readonly)
Return S3 path under use.
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.
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.
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.
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 |