rcarvalho-happening 0.2.5.1 → 0.2.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/happening.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rcarvalho-happening}
8
- s.version = "0.2.5.1"
8
+ s.version = "0.2.5.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Jonathan Weiss}]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "lib/happening/log.rb",
30
30
  "lib/happening/s3.rb",
31
31
  "lib/happening/s3/item.rb",
32
+ "lib/happening/s3/bucket.rb",
32
33
  "lib/happening/s3/request.rb",
33
34
  "lib/happening/utils.rb",
34
35
  "test/aws_test.rb",
@@ -0,0 +1,85 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+
4
+ module Happening
5
+ module S3
6
+ class Bucket
7
+ include Utils
8
+
9
+ REQUIRED_FIELDS = [:server]
10
+ VALID_HEADERS = ['Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-Length', 'Content-MD5', 'Content-Type', 'Expect', 'Expires']
11
+
12
+ attr_accessor :bucket, :options
13
+
14
+ def initialize(bucket, options = {})
15
+ @options = {
16
+ :timeout => 10,
17
+ :server => 's3.amazonaws.com',
18
+ :protocol => 'https',
19
+ :aws_access_key_id => nil,
20
+ :aws_secret_access_key => nil,
21
+ :retry_count => 4,
22
+ :permissions => 'private',
23
+ :ssl => Happening::S3.ssl_options
24
+ }.update(symbolize_keys(options))
25
+ assert_valid_keys(options, :timeout, :server, :protocol, :aws_access_key_id, :aws_secret_access_key, :retry_count, :permissions, :ssl)
26
+ @bucket = bucket.to_s
27
+
28
+ validate
29
+ end
30
+
31
+ def get(request_options = {}, &blk)
32
+ headers = needs_to_sign? ? aws.sign("GET", path) : {}
33
+ request_options[:on_success] = blk if blk
34
+ request_options.update(:headers => headers)
35
+ Happening::S3::Request.new(:get, url, {:ssl => options[:ssl]}.update(request_options)).execute
36
+ end
37
+
38
+ def url
39
+ URI::Generic.new(options[:protocol], nil, server, port, nil, path(!dns_bucket?), nil, nil, nil).to_s
40
+ end
41
+
42
+ def server
43
+ dns_bucket? ? "#{bucket}.#{options[:server]}" : options[:server]
44
+ end
45
+
46
+ def path(with_bucket=true)
47
+ with_bucket ? "/#{bucket}/" : "/"
48
+ end
49
+
50
+ protected
51
+
52
+ def needs_to_sign?
53
+ present?(options[:aws_access_key_id])
54
+ end
55
+
56
+ def dns_bucket?
57
+ # http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?BucketRestrictions.html
58
+ return false unless (3..63) === bucket.size
59
+ bucket.split('.').each do |component|
60
+ return false unless component[/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/]
61
+ end
62
+ true
63
+ end
64
+
65
+ def port
66
+ (options[:protocol].to_s == 'https') ? 443 : 80
67
+ end
68
+
69
+ def validate
70
+ raise ArgumentError, "need a bucket name" unless present?(bucket)
71
+
72
+ REQUIRED_FIELDS.each do |field|
73
+ raise ArgumentError, "need field #{field}" unless present?(options[field])
74
+ end
75
+
76
+ raise ArgumentError, "unknown protocoll #{options[:protocol]}" unless ['http', 'https'].include?(options[:protocol])
77
+ end
78
+
79
+ def aws
80
+ @aws ||= Happening::AWS.new(options[:aws_access_key_id], options[:aws_secret_access_key])
81
+ end
82
+
83
+ end
84
+ end
85
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcarvalho-happening
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5.1
4
+ version: 0.2.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-07-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: em-http-request
16
- requirement: &2153470320 !ruby/object:Gem::Requirement
16
+ requirement: &2157476640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153470320
24
+ version_requirements: *2157476640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda
27
- requirement: &2153469840 !ruby/object:Gem::Requirement
27
+ requirement: &2157476160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153469840
35
+ version_requirements: *2157476160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &2153469360 !ruby/object:Gem::Requirement
38
+ requirement: &2157475680 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153469360
46
+ version_requirements: *2157475680
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &2153468880 !ruby/object:Gem::Requirement
49
+ requirement: &2157475200 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.6.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2153468880
57
+ version_requirements: *2157475200
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &2153484760 !ruby/object:Gem::Requirement
60
+ requirement: &2157491080 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153484760
68
+ version_requirements: *2157491080
69
69
  description: ! 'An EventMachine based S3 client '
70
70
  email: jw@innerewut.de
71
71
  executables: []
@@ -86,6 +86,7 @@ files:
86
86
  - lib/happening/log.rb
87
87
  - lib/happening/s3.rb
88
88
  - lib/happening/s3/item.rb
89
+ - lib/happening/s3/bucket.rb
89
90
  - lib/happening/s3/request.rb
90
91
  - lib/happening/utils.rb
91
92
  - test/aws_test.rb