Class: Happening::S3::Item

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/happening/s3/item.rb

Constant Summary collapse

REQUIRED_FIELDS =
[:server]
VALID_HEADERS =
['Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-Length', 'Content-MD5', 'Content-Type', 'Expect', 'Expires']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, aws_id, options = {}) ⇒ Item

Returns a new instance of Item.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/happening/s3/item.rb', line 14

def initialize(bucket, aws_id, options = {})
  @options = {
    :timeout => 10,
    :server => 's3.amazonaws.com',
    :protocol => 'https',
    :aws_access_key_id => nil,
    :aws_secret_access_key => nil,
    :retry_count => 4,
    :permissions => 'private',
    :ssl => Happening::S3.ssl_options
  }.update(symbolize_keys(options))
  assert_valid_keys(options, :timeout, :server, :protocol, :aws_access_key_id, :aws_secret_access_key, :retry_count, :permissions, :ssl)
  @aws_id = aws_id.to_s
  @bucket = bucket.to_s

  validate
end

Instance Attribute Details

#aws_idObject

Returns the value of attribute aws_id.



12
13
14
# File 'lib/happening/s3/item.rb', line 12

def aws_id
  @aws_id
end

#bucketObject

Returns the value of attribute bucket.



12
13
14
# File 'lib/happening/s3/item.rb', line 12

def bucket
  @bucket
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/happening/s3/item.rb', line 12

def options
  @options
end

Instance Method Details

#delete(request_options = {}, &blk) ⇒ Object



53
54
55
56
57
58
# File 'lib/happening/s3/item.rb', line 53

def delete(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("DELETE", path, {'url' => path}) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  Happening::S3::Request.new(:delete, url, {:ssl => options[:ssl]}.update(request_options)).execute
end

#get(request_options = {}, &blk) ⇒ Object



39
40
41
42
43
44
# File 'lib/happening/s3/item.rb', line 39

def get(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("GET", path) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  Happening::S3::Request.new(:get, url, {:ssl => options[:ssl]}.update(request_options)).execute
end

#head(request_options = {}, &blk) ⇒ Object



32
33
34
35
36
37
# File 'lib/happening/s3/item.rb', line 32

def head(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("HEAD", path) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  Happening::S3::Request.new(:head, url, {:ssl => options[:ssl]}.update(request_options)).execute
end

#path(with_bucket = true) ⇒ Object



68
69
70
# File 'lib/happening/s3/item.rb', line 68

def path(with_bucket=true)
  with_bucket ? "/#{bucket}/#{CGI::escape(aws_id)}" : "/#{CGI::escape(aws_id)}"
end

#put(data, request_options = {}, &blk) ⇒ Object



46
47
48
49
50
51
# File 'lib/happening/s3/item.rb', line 46

def put(data, request_options = {}, &blk)
  headers = construct_aws_headers('PUT', request_options.delete(:headers) || {})
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers, :data => data)
  Happening::S3::Request.new(:put, url, {:ssl => options[:ssl]}.update(request_options)).execute
end

#serverObject



64
65
66
# File 'lib/happening/s3/item.rb', line 64

def server
  dns_bucket? ? "#{bucket}.#{options[:server]}" : options[:server]
end

#urlObject



60
61
62
# File 'lib/happening/s3/item.rb', line 60

def url
  URI::Generic.new(options[:protocol], nil, server, port, nil, path(!dns_bucket?), nil, nil, nil).to_s
end