lib/s3repo/client.rb in s3repo-1.0.0 vs lib/s3repo/client.rb in s3repo-2.0.0
- old
+ new
@@ -3,37 +3,50 @@
module S3Repo
##
# AWS API client
class Client
def initialize(params = {})
- @api = Aws::S3::Client.new
- @defaults = params
+ @options = params
+ @api = Aws::S3::Client.new(region: region)
end
- def respond_to?(method, include_all = false)
+ def respond_to_missing?(method, include_all = false)
@api.respond_to?(method, include_all) || super
end
- def upload!(key, path)
+ def upload(key, body)
puts "Uploading #{key}"
- put_object key: key, body: File.open(path), &:read
+ put_object key: key, body: body
end
+ def upload_file(key, path)
+ upload(key, File.open(path).read)
+ end
+
private
+ def region
+ @options[:region] || raise('AWS region not set')
+ end
+
+ def bucket
+ @options[:bucket] || raise('Bucket not set')
+ end
+
def method_missing(method, *args, &block)
return super unless @api.respond_to?(method)
define_singleton_method(method) do |*singleton_args|
params = build_params(singleton_args)
@api.send(method, params)
end
send(method, args.first)
end
def build_params(args)
- fail 'Too many arguments given' if args.size > 1
+ raise 'Too many arguments given' if args.size > 1
params = args.first || {}
- fail 'Argument must be a hash' unless params.is_a? Hash
- @defaults.dup.merge!(params)
+ raise 'Argument must be a hash' unless params.is_a? Hash
+ params[:bucket] ||= bucket
+ params
end
end
end