Sha256: 00de56ffc098621fd894fa911c5ca2991b6b7a45dde5bfc852661bb160b3424c

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require 'aws-sdk'

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

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

    def upload(key, body)
      puts "Uploading #{key}"
      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)
      raise 'Too many arguments given' if args.size > 1
      params = args.first || {}
      raise 'Argument must be a hash' unless params.is_a? Hash
      params[:bucket] ||= bucket
      params
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
s3repo-2.0.3 lib/s3repo/client.rb
s3repo-2.0.2 lib/s3repo/client.rb
s3repo-2.0.1 lib/s3repo/client.rb
s3repo-2.0.0 lib/s3repo/client.rb