Class: Fog::Storage::GoogleJSON::Mock
- Inherits:
-
Object
- Object
- Fog::Storage::GoogleJSON::Mock
show all
- Includes:
- Google::Shared, GetObjectHttpUrl, GetObjectHttpsUrl, Utils
- Defined in:
- lib/fog/storage/google_json/requests/get_object_url.rb,
lib/fog/storage/google_json.rb,
lib/fog/storage/google_json/requests/get_bucket.rb,
lib/fog/storage/google_json/requests/get_object.rb,
lib/fog/storage/google_json/requests/put_bucket.rb,
lib/fog/storage/google_json/requests/put_object.rb,
lib/fog/storage/google_json/requests/copy_object.rb,
lib/fog/storage/google_json/requests/get_service.rb,
lib/fog/storage/google_json/requests/head_object.rb,
lib/fog/storage/google_json/requests/delete_bucket.rb,
lib/fog/storage/google_json/requests/delete_object.rb,
lib/fog/storage/google_json/requests/get_bucket_acl.rb,
lib/fog/storage/google_json/requests/get_object_acl.rb,
lib/fog/storage/google_json/requests/put_bucket_acl.rb,
lib/fog/storage/google_json/requests/put_object_url.rb,
lib/fog/storage/google_json/requests/get_object_http_url.rb,
lib/fog/storage/google_json/requests/get_object_https_url.rb
Overview
Instance Attribute Summary
#api_url, #api_version, #project
Instance Method Summary
collapse
-
#copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, _options = {}) ⇒ Object
-
#delete_bucket(bucket_name) ⇒ Object
-
#delete_object(bucket_name, object_name) ⇒ Object
-
#get_bucket(bucket_name, options = {}) ⇒ Object
-
#get_bucket_acl(bucket_name) ⇒ Object
-
#get_object(bucket_name, object_name, options = {}, &block) ⇒ Object
-
#get_object_acl(bucket_name, object_name) ⇒ Object
-
#get_object_url(bucket_name, object_name, expires) ⇒ Object
-
#get_service ⇒ Object
-
#head_object(bucket_name, object_name, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Mock
constructor
-
#put_bucket(bucket_name, options = {}) ⇒ Object
-
#put_bucket_acl(_bucket_name, _acl) ⇒ Object
-
#put_object(bucket_name, object_name, data, options = {}) ⇒ Object
-
#put_object_url(bucket_name, object_name, expires, headers = {}) ⇒ Object
-
#signature(_params) ⇒ Object
#get_object_https_url
#get_object_http_url
#build_excon_response, #create_signing_key, #initialize_google_client, #new_pk12_google_client, #request, #shared_initialize
Methods included from Utils
#http_url, #https_url, #url
Constructor Details
#initialize(options = {}) ⇒ Mock
Returns a new instance of Mock.
77
78
79
|
# File 'lib/fog/storage/google_json.rb', line 77
def initialize(options = {})
shared_initialize(options[:google_project], GOOGLE_STORAGE_JSON_API_VERSION, GOOGLE_STORAGE_JSON_BASE_URL)
end
|
Instance Method Details
#copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, _options = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/fog/storage/google_json/requests/copy_object.rb', line 41
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, _options = {})
response = Excon::Response.new
source_bucket = data[:buckets][source_bucket_name]
source_object = source_bucket && source_bucket[:objects][source_object_name]
target_bucket = data[:buckets][target_bucket_name]
if source_object && target_bucket
response.status = 200
target_object = source_object.dup
target_object.merge!("Name" => target_object_name)
target_bucket[:objects][target_object_name] = target_object
response.body = {
"ETag" => target_object["ETag"],
"LastModified" => Time.parse(target_object["Last-Modified"])
}
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#delete_bucket(bucket_name) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/fog/storage/google_json/requests/delete_bucket.rb', line 25
def delete_bucket(bucket_name)
response = Excon::Response.new
if data[:buckets][bucket_name].nil?
response.status = 404
raise(Excon::Errors.status_error({ :expects => 204 }, response))
elsif data[:buckets][bucket_name] && !data[:buckets][bucket_name][:objects].empty?
response.status = 409
raise(Excon::Errors.status_error({ :expects => 204 }, response))
else
data[:buckets].delete(bucket_name)
response.status = 204
end
response
end
|
#delete_object(bucket_name, object_name) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/fog/storage/google_json/requests/delete_object.rb', line 27
def delete_object(bucket_name, object_name)
response = Excon::Response.new
if bucket = data[:buckets][bucket_name]
if object = bucket[:objects][object_name]
response.status = 204
bucket[:objects].delete(object_name)
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 204 }, response))
end
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 204 }, response))
end
response
end
|
#get_bucket(bucket_name, options = {}) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/fog/storage/google_json/requests/get_bucket.rb', line 40
def get_bucket(bucket_name, options = {})
raise ArgumentError.new("bucket_name is required") unless bucket_name
response = Excon::Response.new
name = /(\w+\.?)*/.match(bucket_name)
if bucket_name == name.to_s
if bucket = data[:buckets][bucket_name]
contents = bucket[:objects].values.sort { |x, y| x["Key"] <=> y["Key"] }.reject do |object|
(options["prefix"] && object["Key"][0...options["prefix"].length] != options["prefix"]) ||
(options["marker"] && object["Key"] <= options["marker"])
end.map do |object|
data = object.reject { |key, _value| !%w(ETag Key).include?(key) }
data.merge!("LastModified" => Time.parse(object["Last-Modified"]),
"Owner" => bucket["Owner"],
"Size" => object["Content-Length"].to_i)
data
end
max_keys = options["max-keys"] || 1000
size = [max_keys, 1000].min
truncated_contents = contents[0...size]
response.status = 200
response.body = {
"CommonPrefixes" => [],
"Contents" => truncated_contents,
"IsTruncated" => truncated_contents.size != contents.size,
"Marker" => options["marker"],
"Name" => bucket["Name"],
"Prefix" => options["prefix"]
}
if options["max-keys"] && options["max-keys"] < response.body["Contents"].length
response.body["IsTruncated"] = true
response.body["Contents"] = response.body["Contents"][0...options["max-keys"]]
end
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
else
response.status = 400
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#get_bucket_acl(bucket_name) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/fog/storage/google_json/requests/get_bucket_acl.rb', line 40
def get_bucket_acl(bucket_name)
response = Excon::Response.new
if acl = data[:acls][:bucket][bucket_name]
response.status = 200
response.body = acl
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#get_object(bucket_name, object_name, options = {}, &block) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/fog/storage/google_json/requests/get_object.rb', line 56
def get_object(bucket_name, object_name, options = {}, &block)
raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name
response = Excon::Response.new
if (bucket = data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
if options["If-Match"] && options["If-Match"] != object["ETag"]
response.status = 412
elsif options["If-Modified-Since"] && options["If-Modified-Since"] >= Time.parse(object["Last-Modified"])
response.status = 304
elsif options["If-None-Match"] && options["If-None-Match"] == object["ETag"]
response.status = 304
elsif options["If-Unmodified-Since"] && options["If-Unmodified-Since"] < Time.parse(object["Last-Modified"])
response.status = 412
else
response.status = 200
for key, value in object
case key
when "Cache-Control", "Content-Disposition", "Content-Encoding", "Content-Length", "Content-MD5", "Content-Type", "ETag", "Expires", "Last-Modified", /^x-goog-meta-/
response.[key] = value
end
end
unless block_given?
response.body = object[:body]
else
data = StringIO.new(object[:body])
remaining = data.length
while remaining > 0
chunk = data.read([remaining, Excon::CHUNK_SIZE].min)
block.call(chunk)
remaining -= Excon::CHUNK_SIZE
end
end
end
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#get_object_acl(bucket_name, object_name) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/fog/storage/google_json/requests/get_object_acl.rb', line 45
def get_object_acl(bucket_name, object_name)
response = Excon::Response.new
if acl = data[:acls][:object][bucket_name] && data[:acls][:object][bucket_name][object_name]
response.status = 200
response.body = acl
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#get_object_url(bucket_name, object_name, expires) ⇒ Object
15
16
17
18
|
# File 'lib/fog/storage/google_json/requests/get_object_url.rb', line 15
def get_object_url(bucket_name, object_name, expires)
Fog::Logger.deprecation("Fog::Storage::Google => ##{get_object_url} is deprecated, use ##{get_object_https_url} instead[/] [light_black](#{caller.first})")
get_object_https_url(bucket_name, object_name, expires)
end
|
#get_service ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/fog/storage/google_json/requests/get_service.rb', line 37
def get_service
end
|
#head_object(bucket_name, object_name, options = {}) ⇒ Object
46
47
48
49
50
|
# File 'lib/fog/storage/google_json/requests/head_object.rb', line 46
def head_object(bucket_name, object_name, options = {})
response = get_object(bucket_name, object_name, options)
response.body = nil
response
end
|
#put_bucket(bucket_name, options = {}) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/fog/storage/google_json/requests/put_bucket.rb', line 36
def put_bucket(bucket_name, options = {})
acl = options["x-goog-acl"] || "private"
if !%w(private publicRead publicReadWrite authenticatedRead).include?(acl)
raise Excon::Errors::BadRequest.new("invalid x-goog-acl")
else
data[:acls][:bucket][bucket_name] = self.class.acls(options[acl])
end
response = Excon::Response.new
response.status = 200
bucket = {
:objects => {},
"Name" => bucket_name,
"CreationDate" => Time.now,
"Owner" => { "DisplayName" => "owner", "ID" => "some_id" },
"Payer" => "BucketOwner"
}
if options["LocationConstraint"]
bucket["LocationConstraint"] = options["LocationConstraint"]
else
bucket["LocationConstraint"] = ""
end
if data[:buckets][bucket_name].nil?
data[:buckets][bucket_name] = bucket
else
response.status = 409
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#put_bucket_acl(_bucket_name, _acl) ⇒ Object
5
6
7
|
# File 'lib/fog/storage/google_json/requests/put_bucket_acl.rb', line 5
def put_bucket_acl(_bucket_name, _acl)
Fog::Mock.not_implemented
end
|
#put_object(bucket_name, object_name, data, options = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/fog/storage/google_json/requests/put_object.rb', line 72
def put_object(bucket_name, object_name, data, options = {})
acl = options["x-goog-acl"] || "private"
if !%w(private publicRead publicReadWrite authenticatedRead).include?(acl)
raise Excon::Errors::BadRequest.new("invalid x-goog-acl")
else
self.data[:acls][:object][bucket_name] ||= {}
self.data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
end
data = Fog::Storage.parse_data(data)
data[:body] = data[:body].read unless data[:body].is_a?(String)
response = Excon::Response.new
if (bucket = self.data[:buckets][bucket_name])
response.status = 200
object = {
:body => data[:body],
"Content-Type" => options["Content-Type"] || data[:headers]["Content-Type"],
"ETag" => Fog::Google::Mock.etag,
"Key" => object_name,
"Last-Modified" => Fog::Time.now.,
"Content-Length" => options["Content-Length"] || data[:headers]["Content-Length"]
}
for key, value in options
case key
when "Cache-Control", "Content-Disposition", "Content-Encoding", "Content-MD5", "Expires", /^x-goog-meta-/
object[key] = value
end
end
bucket[:objects][object_name] = object
response. = {
"Content-Length" => object["Content-Length"],
"Content-Type" => object["Content-Type"],
"ETag" => object["ETag"],
"Last-Modified" => object["Last-Modified"]
}
else
response.status = 404
raise(Excon::Errors.status_error({ :expects => 200 }, response))
end
response
end
|
#put_object_url(bucket_name, object_name, expires, headers = {}) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/fog/storage/google_json/requests/put_object_url.rb', line 32
def put_object_url(bucket_name, object_name, expires, = {})
raise ArgumentError.new("bucket_name is required") unless bucket_name
raise ArgumentError.new("object_name is required") unless object_name
https_url({
:headers => ,
:host => @host,
:method => "PUT",
:path => "#{bucket_name}/#{object_name}"
}, expires)
end
|
#signature(_params) ⇒ Object
81
82
83
|
# File 'lib/fog/storage/google_json.rb', line 81
def signature(_params)
"foo"
end
|