Sha256: cef7f3eeb7d4b9c2e2e9e62e9caf74a08bbd9102168a481ec9c40084eea4417a

Contents?: true

Size: 933 Bytes

Versions: 1

Compression:

Stored size: 933 Bytes

Contents

require 'aws-sdk'

module S3Repo
  ##
  # AWS API client
  class Client
    def initialize(params = {})
      @api = Aws::S3::Client.new
      @defaults = params
    end

    def respond_to?(method, include_all = false)
      @api.respond_to?(method, include_all) || super
    end

    def upload!(key, path)
      puts "Uploading #{key}"
      put_object key: key, body: File.open(path), &:read
    end

    private

    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
      params = args.first || {}
      fail 'Argument must be a hash' unless params.is_a? Hash
      @defaults.dup.merge!(params)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
s3repo-1.0.0 lib/s3repo/client.rb