fog 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. data/.document +5 -0
  2. data/.gitignore +6 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +98 -0
  6. data/VERSION +1 -0
  7. data/benchs/fog_vs.rb +135 -0
  8. data/benchs/headers_split_vs_match.rb +18 -0
  9. data/benchs/params.rb +43 -0
  10. data/benchs/parse_vs_push.rb +60 -0
  11. data/benchs/stripping.rb +26 -0
  12. data/lib/fog.rb +21 -0
  13. data/lib/fog/aws.rb +145 -0
  14. data/lib/fog/aws/ec2.rb +173 -0
  15. data/lib/fog/aws/models/ec2/address.rb +31 -0
  16. data/lib/fog/aws/models/ec2/addresses.rb +36 -0
  17. data/lib/fog/aws/models/ec2/key_pair.rb +35 -0
  18. data/lib/fog/aws/models/ec2/key_pairs.rb +36 -0
  19. data/lib/fog/aws/models/ec2/volume.rb +47 -0
  20. data/lib/fog/aws/models/ec2/volumes.rb +36 -0
  21. data/lib/fog/aws/models/s3/bucket.rb +77 -0
  22. data/lib/fog/aws/models/s3/buckets.rb +83 -0
  23. data/lib/fog/aws/models/s3/object.rb +76 -0
  24. data/lib/fog/aws/models/s3/objects.rb +97 -0
  25. data/lib/fog/aws/models/s3/owner.rb +18 -0
  26. data/lib/fog/aws/parsers/ec2/allocate_address.rb +19 -0
  27. data/lib/fog/aws/parsers/ec2/attach_volume.rb +22 -0
  28. data/lib/fog/aws/parsers/ec2/basic.rb +25 -0
  29. data/lib/fog/aws/parsers/ec2/create_key_pair.rb +20 -0
  30. data/lib/fog/aws/parsers/ec2/create_snapshot.rb +22 -0
  31. data/lib/fog/aws/parsers/ec2/create_volume.rb +24 -0
  32. data/lib/fog/aws/parsers/ec2/describe_addresses.rb +30 -0
  33. data/lib/fog/aws/parsers/ec2/describe_availability_zones.rb +30 -0
  34. data/lib/fog/aws/parsers/ec2/describe_images.rb +49 -0
  35. data/lib/fog/aws/parsers/ec2/describe_instances.rb +73 -0
  36. data/lib/fog/aws/parsers/ec2/describe_key_pairs.rb +30 -0
  37. data/lib/fog/aws/parsers/ec2/describe_regions.rb +30 -0
  38. data/lib/fog/aws/parsers/ec2/describe_security_groups.rb +75 -0
  39. data/lib/fog/aws/parsers/ec2/describe_snapshots.rb +30 -0
  40. data/lib/fog/aws/parsers/ec2/describe_volumes.rb +57 -0
  41. data/lib/fog/aws/parsers/ec2/detach_volume.rb +22 -0
  42. data/lib/fog/aws/parsers/ec2/get_console_output.rb +28 -0
  43. data/lib/fog/aws/parsers/ec2/run_instances.rb +65 -0
  44. data/lib/fog/aws/parsers/ec2/terminate_instances.rb +55 -0
  45. data/lib/fog/aws/parsers/s3/copy_object.rb +22 -0
  46. data/lib/fog/aws/parsers/s3/get_bucket.rb +46 -0
  47. data/lib/fog/aws/parsers/s3/get_bucket_location.rb +20 -0
  48. data/lib/fog/aws/parsers/s3/get_request_payment.rb +20 -0
  49. data/lib/fog/aws/parsers/s3/get_service.rb +32 -0
  50. data/lib/fog/aws/parsers/simpledb/basic.rb +31 -0
  51. data/lib/fog/aws/parsers/simpledb/domain_metadata.rb +30 -0
  52. data/lib/fog/aws/parsers/simpledb/get_attributes.rb +34 -0
  53. data/lib/fog/aws/parsers/simpledb/list_domains.rb +28 -0
  54. data/lib/fog/aws/parsers/simpledb/select.rb +39 -0
  55. data/lib/fog/aws/requests/ec2/allocate_address.rb +50 -0
  56. data/lib/fog/aws/requests/ec2/associate_address.rb +59 -0
  57. data/lib/fog/aws/requests/ec2/attach_volume.rb +71 -0
  58. data/lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb +32 -0
  59. data/lib/fog/aws/requests/ec2/create_key_pair.rb +60 -0
  60. data/lib/fog/aws/requests/ec2/create_security_group.rb +61 -0
  61. data/lib/fog/aws/requests/ec2/create_snapshot.rb +65 -0
  62. data/lib/fog/aws/requests/ec2/create_volume.rb +66 -0
  63. data/lib/fog/aws/requests/ec2/delete_key_pair.rb +49 -0
  64. data/lib/fog/aws/requests/ec2/delete_security_group.rb +52 -0
  65. data/lib/fog/aws/requests/ec2/delete_snapshot.rb +53 -0
  66. data/lib/fog/aws/requests/ec2/delete_volume.rb +55 -0
  67. data/lib/fog/aws/requests/ec2/describe_addresses.rb +61 -0
  68. data/lib/fog/aws/requests/ec2/describe_availability_zones.rb +64 -0
  69. data/lib/fog/aws/requests/ec2/describe_images.rb +41 -0
  70. data/lib/fog/aws/requests/ec2/describe_instances.rb +114 -0
  71. data/lib/fog/aws/requests/ec2/describe_key_pairs.rb +63 -0
  72. data/lib/fog/aws/requests/ec2/describe_regions.rb +61 -0
  73. data/lib/fog/aws/requests/ec2/describe_security_groups.rb +71 -0
  74. data/lib/fog/aws/requests/ec2/describe_snapshots.rb +78 -0
  75. data/lib/fog/aws/requests/ec2/describe_volumes.rb +85 -0
  76. data/lib/fog/aws/requests/ec2/detach_volume.rb +62 -0
  77. data/lib/fog/aws/requests/ec2/disassociate_address.rb +55 -0
  78. data/lib/fog/aws/requests/ec2/get_console_output.rb +26 -0
  79. data/lib/fog/aws/requests/ec2/reboot_instances.rb +24 -0
  80. data/lib/fog/aws/requests/ec2/release_address.rb +50 -0
  81. data/lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb +32 -0
  82. data/lib/fog/aws/requests/ec2/run_instances.rb +134 -0
  83. data/lib/fog/aws/requests/ec2/terminate_instances.rb +80 -0
  84. data/lib/fog/aws/requests/s3/copy_object.rb +79 -0
  85. data/lib/fog/aws/requests/s3/delete_bucket.rb +53 -0
  86. data/lib/fog/aws/requests/s3/delete_object.rb +52 -0
  87. data/lib/fog/aws/requests/s3/get_bucket.rb +100 -0
  88. data/lib/fog/aws/requests/s3/get_bucket_location.rb +53 -0
  89. data/lib/fog/aws/requests/s3/get_object.rb +82 -0
  90. data/lib/fog/aws/requests/s3/get_request_payment.rb +53 -0
  91. data/lib/fog/aws/requests/s3/get_service.rb +58 -0
  92. data/lib/fog/aws/requests/s3/head_object.rb +61 -0
  93. data/lib/fog/aws/requests/s3/put_bucket.rb +69 -0
  94. data/lib/fog/aws/requests/s3/put_object.rb +76 -0
  95. data/lib/fog/aws/requests/s3/put_request_payment.rb +56 -0
  96. data/lib/fog/aws/requests/simpledb/batch_put_attributes.rb +31 -0
  97. data/lib/fog/aws/requests/simpledb/create_domain.rb +25 -0
  98. data/lib/fog/aws/requests/simpledb/delete_attributes.rb +34 -0
  99. data/lib/fog/aws/requests/simpledb/delete_domain.rb +25 -0
  100. data/lib/fog/aws/requests/simpledb/domain_metadata.rb +30 -0
  101. data/lib/fog/aws/requests/simpledb/get_attributes.rb +35 -0
  102. data/lib/fog/aws/requests/simpledb/list_domains.rb +30 -0
  103. data/lib/fog/aws/requests/simpledb/put_attributes.rb +29 -0
  104. data/lib/fog/aws/requests/simpledb/select.rb +29 -0
  105. data/lib/fog/aws/s3.rb +157 -0
  106. data/lib/fog/aws/simpledb.rb +157 -0
  107. data/lib/fog/collection.rb +77 -0
  108. data/lib/fog/connection.rb +130 -0
  109. data/lib/fog/errors.rb +145 -0
  110. data/lib/fog/model.rb +72 -0
  111. data/lib/fog/parser.rb +30 -0
  112. data/lib/fog/response.rb +12 -0
  113. data/spec/aws/models/s3/bucket_spec.rb +130 -0
  114. data/spec/aws/models/s3/buckets_spec.rb +79 -0
  115. data/spec/aws/models/s3/object_spec.rb +121 -0
  116. data/spec/aws/models/s3/objects_spec.rb +112 -0
  117. data/spec/aws/models/s3/owner_spec.rb +18 -0
  118. data/spec/aws/requests/ec2/allocate_address_spec.rb +18 -0
  119. data/spec/aws/requests/ec2/associate_address_spec.rb +42 -0
  120. data/spec/aws/requests/ec2/attach_volume_spec.rb +53 -0
  121. data/spec/aws/requests/ec2/authorize_security_group_ingress_spec.rb +26 -0
  122. data/spec/aws/requests/ec2/create_key_pair_spec.rb +36 -0
  123. data/spec/aws/requests/ec2/create_security_group_spec.rb +34 -0
  124. data/spec/aws/requests/ec2/create_snapshot_spec.rb +37 -0
  125. data/spec/aws/requests/ec2/create_volume_spec.rb +23 -0
  126. data/spec/aws/requests/ec2/delete_key_pair_spec.rb +21 -0
  127. data/spec/aws/requests/ec2/delete_security_group_spec.rb +26 -0
  128. data/spec/aws/requests/ec2/delete_snapshot_spec.rb +35 -0
  129. data/spec/aws/requests/ec2/delete_volume_spec.rb +26 -0
  130. data/spec/aws/requests/ec2/describe_addresses_spec.rb +38 -0
  131. data/spec/aws/requests/ec2/describe_availability_zones_spec.rb +23 -0
  132. data/spec/aws/requests/ec2/describe_images_spec.rb +39 -0
  133. data/spec/aws/requests/ec2/describe_instances_spec.rb +86 -0
  134. data/spec/aws/requests/ec2/describe_key_pairs_spec.rb +42 -0
  135. data/spec/aws/requests/ec2/describe_regions_spec.rb +21 -0
  136. data/spec/aws/requests/ec2/describe_security_groups_spec.rb +51 -0
  137. data/spec/aws/requests/ec2/describe_snapshots_spec.rb +54 -0
  138. data/spec/aws/requests/ec2/describe_volumes_spec.rb +50 -0
  139. data/spec/aws/requests/ec2/detach_volume_spec.rb +43 -0
  140. data/spec/aws/requests/ec2/disassociate_address_spec.rb +35 -0
  141. data/spec/aws/requests/ec2/get_console_output_spec.rb +23 -0
  142. data/spec/aws/requests/ec2/reboot_instances_spec.rb +21 -0
  143. data/spec/aws/requests/ec2/release_address_spec.rb +26 -0
  144. data/spec/aws/requests/ec2/revoke_security_group_ingress_spec.rb +32 -0
  145. data/spec/aws/requests/ec2/run_instances_spec.rb +41 -0
  146. data/spec/aws/requests/ec2/terminate_instances_spec.rb +35 -0
  147. data/spec/aws/requests/s3/copy_object_spec.rb +62 -0
  148. data/spec/aws/requests/s3/delete_bucket_spec.rb +36 -0
  149. data/spec/aws/requests/s3/delete_object_spec.rb +37 -0
  150. data/spec/aws/requests/s3/get_bucket_location_spec.rb +30 -0
  151. data/spec/aws/requests/s3/get_bucket_spec.rb +110 -0
  152. data/spec/aws/requests/s3/get_object_spec.rb +45 -0
  153. data/spec/aws/requests/s3/get_request_payment_spec.rb +30 -0
  154. data/spec/aws/requests/s3/get_service_spec.rb +33 -0
  155. data/spec/aws/requests/s3/head_object_spec.rb +28 -0
  156. data/spec/aws/requests/s3/put_bucket_spec.rb +19 -0
  157. data/spec/aws/requests/s3/put_object_spec.rb +38 -0
  158. data/spec/aws/requests/s3/put_request_payment_spec.rb +29 -0
  159. data/spec/aws/requests/simpledb/batch_put_attributes_spec.rb +22 -0
  160. data/spec/aws/requests/simpledb/create_domain_spec.rb +21 -0
  161. data/spec/aws/requests/simpledb/delete_attributes_spec.rb +22 -0
  162. data/spec/aws/requests/simpledb/delete_domain_spec.rb +21 -0
  163. data/spec/aws/requests/simpledb/domain_metadata_spec.rb +43 -0
  164. data/spec/aws/requests/simpledb/get_attributes_spec.rb +31 -0
  165. data/spec/aws/requests/simpledb/list_domains_spec.rb +30 -0
  166. data/spec/aws/requests/simpledb/put_attributes_spec.rb +22 -0
  167. data/spec/aws/requests/simpledb/select_spec.rb +9 -0
  168. data/spec/lorem.txt +1 -0
  169. data/spec/spec.opts +1 -0
  170. data/spec/spec_helper.rb +66 -0
  171. metadata +309 -0
@@ -0,0 +1,130 @@
1
+ require 'rubygems'
2
+ require 'openssl'
3
+ require 'socket'
4
+ require 'uri'
5
+
6
+ require "#{File.dirname(__FILE__)}/errors"
7
+ require "#{File.dirname(__FILE__)}/response"
8
+
9
+ unless Fog.mocking?
10
+
11
+ module Fog
12
+ class Connection
13
+
14
+ def initialize(url)
15
+ @uri = URI.parse(url)
16
+ @connection = TCPSocket.open(@uri.host, @uri.port)
17
+ if @uri.scheme == 'https'
18
+ @ssl_context = OpenSSL::SSL::SSLContext.new
19
+ @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
20
+ @connection = OpenSSL::SSL::SSLSocket.new(@connection, @ssl_context)
21
+ @connection.sync_close = true
22
+ @connection.connect
23
+ end
24
+ end
25
+
26
+ def request(params)
27
+ params[:path] ||= ''
28
+ unless params[:path][0] == '/'
29
+ params[:path] = '/' + params[:path].to_s
30
+ end
31
+ if params[:query] && !params[:query].empty?
32
+ params[:path] << "?#{params[:query]}"
33
+ end
34
+ request = "#{params[:method]} #{params[:path]} HTTP/1.1\r\n"
35
+ params[:headers] ||= {}
36
+ params[:headers]['Host'] = params[:host]
37
+ if params[:body]
38
+ params[:headers]['Content-Length'] = params[:body].length
39
+ end
40
+ for key, value in params[:headers]
41
+ request << "#{key}: #{value}\r\n"
42
+ end
43
+ request << "\r\n#{params[:body]}"
44
+ @connection.write(request)
45
+
46
+ response = Fog::Response.new
47
+ response.request = params
48
+ response.status = @connection.readline[9..11].to_i
49
+ if params[:expects] && params[:expects] != response.status
50
+ error = true
51
+ end
52
+ while true
53
+ data = @connection.readline.chomp!
54
+ if data == ""
55
+ break
56
+ end
57
+ header = data.split(': ')
58
+ response.headers[capitalize(header[0])] = header[1]
59
+ end
60
+
61
+ if error || params[:parser]
62
+ if error
63
+ parser = Fog::Errors::Parser.new
64
+ elsif params[:parser]
65
+ parser = params[:parser]
66
+ end
67
+ body = Nokogiri::XML::SAX::PushParser.new(parser)
68
+ else
69
+ body = ''
70
+ end
71
+
72
+ unless params[:method] == 'HEAD'
73
+ if response.headers['Content-Length']
74
+ body << @connection.read(response.headers['Content-Length'].to_i)
75
+ elsif response.headers['Transfer-Encoding'] == 'chunked'
76
+ while true
77
+ # 2 == "/r/n".length
78
+ chunk_size = @connection.readline.chomp!.to_i(16) + 2
79
+ chunk = @connection.read(chunk_size)
80
+ body << chunk[0...-2]
81
+ if chunk_size == 2
82
+ break
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ if parser
89
+ body.finish
90
+ response.body = parser.response
91
+ else
92
+ response.body = body
93
+ end
94
+
95
+ if error
96
+ raise(Fog::Errors.status_error(params[:expects], response.status, response))
97
+ else
98
+ response
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def capitalize(header)
105
+ words = header.split('-')
106
+ header = ''
107
+ for word in words
108
+ header << word[0..0].upcase << word[1..-1] << '-'
109
+ end
110
+ header.chop!
111
+ end
112
+
113
+ end
114
+ end
115
+
116
+ else
117
+
118
+ module Fog
119
+ class Connection
120
+
121
+ def initialize(url)
122
+ end
123
+
124
+ def request(params)
125
+ end
126
+
127
+ end
128
+ end
129
+
130
+ end
@@ -0,0 +1,145 @@
1
+ require "#{File.dirname(__FILE__)}/parser"
2
+
3
+ module Fog
4
+ module Errors
5
+ class Continue < StandardError; end # 100
6
+ class SwitchingProtocols < StandardError; end # 101
7
+ class OK < StandardError; end # 200
8
+ class Created < StandardError; end # 201
9
+ class Accepted < StandardError; end # 202
10
+ class NonAuthoritativeInformation < StandardError; end # 203
11
+ class NoContent < StandardError; end # 204
12
+ class ResetContent < StandardError; end # 205
13
+ class PartialContent < StandardError; end # 206
14
+ class MultipleChoices < StandardError; end # 300
15
+ class MovedPermanently < StandardError; end # 301
16
+ class Found < StandardError; end # 302
17
+ class SeeOther < StandardError; end # 303
18
+ class NotModified < StandardError; end # 304
19
+ class UseProxy < StandardError; end # 305
20
+ class TemporaryRedirect < StandardError; end # 307
21
+ class BadRequest < StandardError; end # 400
22
+ class Unauthorized < StandardError; end # 401
23
+ class PaymentRequired < StandardError; end # 402
24
+ class Forbidden < StandardError; end # 403
25
+ class NotFound < StandardError; end # 404
26
+ class MethodNotAllowed < StandardError; end # 405
27
+ class NotAcceptable < StandardError; end #406
28
+ class ProxyAuthenticationRequired < StandardError; end #407
29
+ class RequestTimeout < StandardError; end # 408
30
+ class Conflict < StandardError; end # 409
31
+ class Gone < StandardError; end # 410
32
+ class LengthRequired < StandardError; end # 411
33
+ class PreconditionFailed < StandardError; end # 412
34
+ class RequestEntityTooLarge < StandardError; end # 412
35
+ class RequestURITooLong < StandardError; end # 414
36
+ class UnsupportedMediaType < StandardError; end # 415
37
+ class RequestedRangeNotSatisfiable < StandardError; end # 416
38
+ class ExpectationFailed < StandardError; end # 417
39
+ class InternalServerError < StandardError; end # 500
40
+ class NotImplemented < StandardError; end # 501
41
+ class BadGateway < StandardError; end # 502
42
+ class ServiceUnavailable < StandardError; end # 503
43
+ class GatewayTimeout < StandardError; end # 504
44
+
45
+ # Messages for nicer exceptions, from rfc2616
46
+ def self.status_error(expected, actual, response)
47
+ @errors ||= {
48
+ 100 => Fog::Errors::Continue,
49
+ 101 => Fog::Errors::SwitchingProtocols,
50
+ 200 => Fog::Errors::OK,
51
+ 201 => Fog::Errors::Created,
52
+ 202 => Fog::Errors::Accepted,
53
+ 203 => Fog::Errors::NonAuthoritativeInformation,
54
+ 204 => Fog::Errors::NoContent,
55
+ 205 => Fog::Errors::ResetContent,
56
+ 206 => Fog::Errors::PartialContent,
57
+ 300 => Fog::Errors::MultipleChoices,
58
+ 301 => Fog::Errors::MovedPermanently,
59
+ 302 => Fog::Errors::Found,
60
+ 303 => Fog::Errors::SeeOther,
61
+ 304 => Fog::Errors::NotModified,
62
+ 305 => Fog::Errors::UseProxy,
63
+ 307 => Fog::Errors::TemporaryRedirect,
64
+ 400 => Fog::Errors::BadRequest,
65
+ 401 => Fog::Errors::Unauthorized,
66
+ 402 => Fog::Errors::PaymentRequired,
67
+ 403 => Fog::Errors::Forbidden,
68
+ 404 => Fog::Errors::NotFound,
69
+ 405 => Fog::Errors::MethodNotAllowed,
70
+ 406 => Fog::Errors::NotAcceptable,
71
+ 407 => Fog::Errors::ProxyAuthenticationRequired,
72
+ 408 => Fog::Errors::RequestTimeout,
73
+ 409 => Fog::Errors::Conflict,
74
+ 410 => Fog::Errors::Gone,
75
+ 411 => Fog::Errors::LengthRequired,
76
+ 412 => Fog::Errors::PreconditionFailed,
77
+ 413 => Fog::Errors::RequestEntityTooLarge,
78
+ 414 => Fog::Errors::RequestURITooLong,
79
+ 415 => Fog::Errors::UnsupportedMediaType,
80
+ 416 => Fog::Errors::RequestedRangeNotSatisfiable,
81
+ 417 => Fog::Errors::ExpectationFailed,
82
+ 500 => Fog::Errors::InternalServerError,
83
+ 501 => Fog::Errors::NotImplemented,
84
+ 502 => Fog::Errors::BadGateway,
85
+ 503 => Fog::Errors::ServiceUnavailable,
86
+ 504 => Fog::Errors::GatewayTimeout
87
+ }
88
+ @messages ||= {
89
+ 100 => 'Continue',
90
+ 101 => 'Switching Protocols',
91
+ 200 => 'OK',
92
+ 201 =>'Created',
93
+ 202 => 'Accepted',
94
+ 203 => 'Non-Authoritative Information',
95
+ 204 => 'No Content',
96
+ 205 => 'Reset Content',
97
+ 206 => 'Partial Content',
98
+ 300 => 'Multiple Choices',
99
+ 301 => 'Moved Permanently',
100
+ 302 => 'Found',
101
+ 303 => 'See Other',
102
+ 304 => 'Not Modified',
103
+ 305 => 'Use Proxy',
104
+ 307 => 'Temporary Redirect',
105
+ 400 => 'Bad Request',
106
+ 401 => 'Unauthorized',
107
+ 402 => 'Payment Required',
108
+ 403 => 'Forbidden',
109
+ 404 => 'Not Found',
110
+ 405 => 'Method Not Allowed',
111
+ 406 => 'Not Acceptable',
112
+ 407 => 'Proxy Authentication Required',
113
+ 408 => 'Request Timeout',
114
+ 409 => 'Conflict',
115
+ 410 => 'Gone',
116
+ 411 => 'Length Required',
117
+ 412 => 'Precondition Failed',
118
+ 413 => 'Request Entity Too Large',
119
+ 414 => 'Request-URI Too Long',
120
+ 415 => 'Unsupported Media Type',
121
+ 416 => 'Requested Range Not Satisfiable',
122
+ 417 => 'Expectation Failed',
123
+ 500 => 'Internal Server Error',
124
+ 501 => 'Not Implemented',
125
+ 502 => 'Bad Gateway',
126
+ 503 => 'Service Unavailable',
127
+ 504 => 'Gateway Timeout'
128
+ }
129
+ response = "#{response.body['Code']} => #{response.body['Message']}"
130
+ @errors[actual].new("Expected(#{expected} #{@messages[expected]}) <=> Actual(#{actual} #{@messages[actual]}): #{response}")
131
+ end
132
+
133
+ class Parser < Fog::Parsers::Base
134
+
135
+ def end_element(name)
136
+ case name
137
+ when 'Code', 'Message'
138
+ @response[name] = @value
139
+ end
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,72 @@
1
+ module Fog
2
+ class Model
3
+
4
+ def self.attribute(name, other_names = [])
5
+ class_eval <<-EOS, __FILE__, __LINE__
6
+ attr_accessor :#{name}
7
+ EOS
8
+ attributes << name
9
+ for other_name in [*other_names]
10
+ aliases[other_name] = name
11
+ end
12
+ end
13
+
14
+ def self.aliases
15
+ @aliases ||= {}
16
+ end
17
+
18
+ def self.attributes
19
+ @attributes ||= []
20
+ end
21
+
22
+ def initialize(new_attributes = {})
23
+ merge_attributes(new_attributes)
24
+ end
25
+
26
+ def inspect
27
+ data = "#<#{self.class.name}"
28
+ for attribute in self.class.attributes
29
+ data << " #{attribute}=#{send(attribute).inspect}"
30
+ end
31
+ data << ">"
32
+ end
33
+
34
+ def attributes
35
+ attributes = {}
36
+ for attribute in self.class.attributes
37
+ attributes[attribute] = send(:"#{attribute}")
38
+ end
39
+ attributes
40
+ end
41
+
42
+ def merge_attributes(new_attributes = {})
43
+ for key, value in new_attributes
44
+ if aliased_key = self.class.aliases[key]
45
+ send(:"#{aliased_key}=", value)
46
+ else
47
+ send(:"#{key}=", value)
48
+ end
49
+ end
50
+ self
51
+ end
52
+
53
+ private
54
+
55
+ def connection=(new_connection)
56
+ @connection = new_connection
57
+ end
58
+
59
+ def connection
60
+ @connection
61
+ end
62
+
63
+ def remap_attributes(attributes, mapping)
64
+ for key, value in mapping
65
+ if attributes.key?(key)
66
+ attributes[value] = attributes.delete(key)
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'base64'
3
+ require 'nokogiri'
4
+ require 'time'
5
+
6
+ module Fog
7
+ module Parsers
8
+ class Base < Nokogiri::XML::SAX::Document
9
+
10
+ attr_reader :response
11
+
12
+ def initialize
13
+ reset
14
+ end
15
+
16
+ def reset
17
+ @response = {}
18
+ end
19
+
20
+ def characters(string)
21
+ @value << string.strip
22
+ end
23
+
24
+ def start_element(name, attrs = [])
25
+ @value = ''
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Fog
2
+ class Response
3
+
4
+ attr_accessor :body, :headers, :request, :status
5
+
6
+ def initialize
7
+ @body = ''
8
+ @headers = {}
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,130 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+
3
+ describe 'Fog::AWS::S3::Bucket' do
4
+
5
+ describe "#initialize" do
6
+
7
+ it "should remap attributes from parser" do
8
+ now = Time.now
9
+ bucket = Fog::AWS::S3::Bucket.new(
10
+ 'CreationDate' => now,
11
+ 'Name' => 'bucketname'
12
+ )
13
+ bucket.creation_date.should == now
14
+ bucket.name.should == 'bucketname'
15
+ end
16
+
17
+ end
18
+
19
+ describe "#buckets" do
20
+
21
+ it "should return a Fog::AWS::S3::Buckets" do
22
+ s3.buckets.new.buckets.should be_a(Fog::AWS::S3::Buckets)
23
+ end
24
+
25
+ it "should be the buckets the bucket is related to" do
26
+ buckets = s3.buckets
27
+ buckets.new.buckets.should == buckets
28
+ end
29
+
30
+ end
31
+
32
+ describe "#destroy" do
33
+
34
+ it "should return true if the bucket is deleted" do
35
+ bucket = s3.buckets.create(:name => 'fogmodelbucket')
36
+ bucket.destroy.should be_true
37
+ end
38
+
39
+ it "should return false if the bucket does not exist" do
40
+ bucket = s3.buckets.new(:name => 'fogmodelbucket')
41
+ bucket.destroy.should be_false
42
+ end
43
+
44
+ end
45
+
46
+ describe "#location" do
47
+
48
+ it "should return the location constraint" do
49
+ bucket = s3.buckets.create(:name => 'fogmodeleubucket', :location => 'EU')
50
+ bucket.location.should == 'EU'
51
+ eu_s3.buckets.new(:name => 'fogmodeleubucket').destroy
52
+ end
53
+
54
+ end
55
+
56
+ describe "#objects" do
57
+
58
+ it "should return a Fog::AWS::S3::Objects" do
59
+ bucket = s3.buckets.new(:name => 'fogmodelbucket')
60
+ bucket.objects.should be_an(Fog::AWS::S3::Objects)
61
+ end
62
+
63
+ end
64
+
65
+ describe "#payer" do
66
+
67
+ it "should return the request payment value" do
68
+ bucket = s3.buckets.create(:name => 'fogmodelbucket')
69
+ bucket.payer.should == 'BucketOwner'
70
+ bucket.destroy.should be_true
71
+ end
72
+
73
+ end
74
+
75
+ describe "#payer=" do
76
+
77
+ it "should set the request payment value" do
78
+ bucket = s3.buckets.create(:name => 'fogmodelbucket')
79
+ (bucket.payer = 'Requester').should == 'Requester'
80
+ bucket.destroy.should
81
+ end
82
+
83
+ end
84
+
85
+ describe "#reload" do
86
+
87
+ before(:each) do
88
+ @bucket = s3.buckets.create(:name => 'fogmodelbucket')
89
+ @reloaded = @bucket.reload
90
+ end
91
+
92
+ after(:each) do
93
+ @bucket.destroy
94
+ end
95
+
96
+ it "should return a Fog::AWS::S3::Bucket" do
97
+ @reloaded.should be_a(Fog::AWS::S3::Bucket)
98
+ end
99
+
100
+ it "should reset attributes to remote state" do
101
+ @bucket.creation_date = Time.now
102
+ @bucket.attributes.should == @reloaded.attributes
103
+ end
104
+
105
+ end
106
+
107
+ describe "#save" do
108
+
109
+ before(:each) do
110
+ @bucket = s3.buckets.new(:name => 'fogmodelbucket')
111
+ end
112
+
113
+ it "should return true when it succeeds" do
114
+ @bucket.save.should be_true
115
+ @bucket.destroy
116
+ end
117
+
118
+ it "should not exist in buckets before save" do
119
+ @bucket.buckets.key?(@bucket.name).should be_false
120
+ end
121
+
122
+ it "should exist in buckets after save" do
123
+ @bucket.save
124
+ @bucket.buckets.key?(@bucket.name).should be_true
125
+ @bucket.destroy
126
+ end
127
+
128
+ end
129
+
130
+ end