Class: Rack::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/s3.rb,
lib/rack/s3/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3

Returns a new instance of S3.



8
9
10
11
12
13
# File 'lib/rack/s3.rb', line 8

def initialize(options={})
  @bucket = options[:bucket]

  establish_aws_connection(options[:access_key_id],
                           options[:secret_access_key])
end

Instance Method Details

#_call(env) ⇒ Object



27
28
29
30
31
32
# File 'lib/rack/s3.rb', line 27

def _call(env)
  @env = env
  [ 200, headers, object.value ]
rescue AWS::S3::NoSuchKey
  not_found
end

#call(env) ⇒ Object



23
24
25
# File 'lib/rack/s3.rb', line 23

def call(env)
  dup._call env
end

#establish_aws_connection(access_key_id, secret_access_key) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rack/s3.rb', line 15

def establish_aws_connection(access_key_id, secret_access_key)
  return unless access_key_id && secret_access_key

  AWS::S3::Base.establish_connection!(
    :access_key_id     => access_key_id,
    :secret_access_key => secret_access_key)
end

#headersObject



34
35
36
37
38
39
40
41
42
# File 'lib/rack/s3.rb', line 34

def headers
  about = object.about

  { 'Content-Type'   => about['content-type'],
    'Content-Length' => about['content-length'],
    'Etag'           => about['etag'],
    'Last-Modified'  => about['last-modified']
  }
end

#not_foundObject



56
57
58
59
60
61
62
63
# File 'lib/rack/s3.rb', line 56

def not_found
  body = "File not found: #{ path_info }\n"

  [ 404,
    { 'Content-Type'   => "text/plain",
      'Content-Length' => body.size.to_s },
    [ body ]]
end

#objectObject



52
53
54
# File 'lib/rack/s3.rb', line 52

def object
  @object ||= AWS::S3::S3Object.find(path, @bucket)
end

#pathObject



48
49
50
# File 'lib/rack/s3.rb', line 48

def path
  path_info[0...1] == '/' ? path_info[1..-1] : path_info
end

#path_infoObject



44
45
46
# File 'lib/rack/s3.rb', line 44

def path_info
  CGI.unescape @env['PATH_INFO']
end