Skip to content

Commit

Permalink
fix(storage): address keyword argument warnings in Ruby 2.7 and later
Browse files Browse the repository at this point in the history
  • Loading branch information
yahonda authored Mar 11, 2020
1 parent 858ab53 commit 77bdc4a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 58 deletions.
48 changes: 27 additions & 21 deletions google-cloud-storage/lib/google/cloud/storage/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1254,22 +1254,27 @@ def create_file file, path = nil, acl: nil, cache_control: nil,
storage_class: nil, encryption_key: nil, kms_key: nil,
temporary_hold: nil, event_based_hold: nil
ensure_service!
options = { acl: File::Acl.predefined_rule_for(acl), md5: md5,
cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, crc32c: crc32c,
content_encoding: content_encoding, metadata: metadata,
content_language: content_language, key: encryption_key,
kms_key: kms_key,
storage_class: storage_class_for(storage_class),
temporary_hold: temporary_hold,
event_based_hold: event_based_hold,
user_project: user_project }
ensure_io_or_file_exists! file
path ||= file.path if file.respond_to? :path
path ||= file if file.is_a? String
raise ArgumentError, "must provide path" if path.nil?

gapi = service.insert_file name, file, path, options

gapi = service.insert_file name, file, path, acl: File::Acl.predefined_rule_for(acl),
md5: md5,
cache_control: cache_control,
content_type: content_type,
content_disposition: content_disposition,
crc32c: crc32c,
content_encoding: content_encoding,
metadata: metadata,
content_language: content_language,
key: encryption_key,
kms_key: kms_key,
storage_class: storage_class_for(storage_class),
temporary_hold: temporary_hold,
event_based_hold: event_based_hold,
user_project: user_project
File.from_gapi gapi, service, user_project: user_project
end
alias upload_file create_file
Expand Down Expand Up @@ -1368,18 +1373,18 @@ def compose sources, destination, acl: nil, encryption_key: nil
raise ArgumentError, "must provide at least two source files"
end

options = { acl: File::Acl.predefined_rule_for(acl),
key: encryption_key,
user_project: user_project }
destination_gapi = nil
if block_given?
destination_gapi = API::Object.new
updater = File::Updater.new destination_gapi
yield updater
updater.check_for_changed_metadata!
end
gapi = service.compose_file name, sources, destination,
destination_gapi, options

acl_rule = File::Acl.predefined_rule_for acl
gapi = service.compose_file name, sources, destination, destination_gapi, acl: acl_rule,
key: encryption_key,
user_project: user_project
File.from_gapi gapi, service, user_project: user_project
end
alias compose_file compose
Expand Down Expand Up @@ -2159,11 +2164,12 @@ def notification id
def create_notification topic, custom_attrs: nil, event_types: nil,
prefix: nil, payload: nil
ensure_service!
options = { custom_attrs: custom_attrs, event_types: event_types,
prefix: prefix, payload: payload,
user_project: user_project }

gapi = service.insert_notification name, topic, options
gapi = service.insert_notification name, topic, custom_attrs: custom_attrs,
event_types: event_types,
prefix: prefix,
payload: payload,
user_project: user_project
Notification.from_gapi name, gapi, service, user_project: user_project
end
alias new_notification create_notification
Expand Down Expand Up @@ -2249,7 +2255,7 @@ def patch_gapi! *attributes
patch_args = Hash[attributes.map do |attr|
[attr, @gapi.send(attr)]
end]
patch_gapi = API::Bucket.new patch_args
patch_gapi = API::Bucket.new(**patch_args)
@gapi = service.patch_bucket name, patch_gapi,
user_project: user_project
@lazy = nil
Expand Down
6 changes: 3 additions & 3 deletions google-cloud-storage/lib/google/cloud/storage/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ def self.gapi_from_attrs gapi, attributes
# Sending nil metadata results in an Apiary runtime error:
# NoMethodError: undefined method `each' for nil:NilClass
attr_params.reject! { |k, v| k == :metadata && v.nil? }
Google::Apis::StorageV1::Object.new attr_params
Google::Apis::StorageV1::Object.new(**attr_params)
end

protected
Expand Down Expand Up @@ -1817,12 +1817,12 @@ def rewrite_gapi bucket, name, updated_gapi,
user_project: user_project }.delete_if { |_k, v| v.nil? }

resp = service.rewrite_file \
bucket, name, new_bucket, new_name, updated_gapi, options
bucket, name, new_bucket, new_name, updated_gapi, **options
until resp.done
sleep 1
retry_options = options.merge token: resp.rewrite_token
resp = service.rewrite_file \
bucket, name, new_bucket, new_name, updated_gapi, retry_options
bucket, name, new_bucket, new_name, updated_gapi, **retry_options
end
resp.resource
end
Expand Down
12 changes: 7 additions & 5 deletions google-cloud-storage/lib/google/cloud/storage/file/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ def next?
def next
return nil unless next?
ensure_service!
options = {
prefix: @prefix, delimiter: @delimiter, token: @token, max: @max,
versions: @versions, user_project: @user_project
}
gapi = @service.list_files @bucket, options

gapi = @service.list_files @bucket, prefix: @prefix,
delimiter: @delimiter,
token: @token,
max: @max,
versions: @versions,
user_project: @user_project
File::List.from_gapi gapi, @service, @bucket, @prefix,
@delimiter, @max, @versions,
user_project: @user_project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,12 @@ def hash
##
# @private
def to_gapi
Google::Apis::StorageV1::Policy::Binding.new({
params = {
role: @role,
members: @members,
condition: @condition&.to_gapi
}.delete_if { |_, v| v.nil? })
}.delete_if { |_, v| v.nil? }
Google::Apis::StorageV1::Policy::Binding.new(**params)
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions google-cloud-storage/lib/google/cloud/storage/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,11 @@ def create_bucket bucket_name, acl: nil, default_acl: nil,
logging_bucket: nil, logging_prefix: nil,
website_main: nil, website_404: nil, versioning: nil,
requester_pays: nil, user_project: nil
new_bucket = Google::Apis::StorageV1::Bucket.new({
params = {
name: bucket_name,
location: location
}.delete_if { |_, v| v.nil? })
}.delete_if { |_, v| v.nil? }
new_bucket = Google::Apis::StorageV1::Bucket.new(**params)
storage_class = storage_class_for storage_class
updater = Bucket::Updater.new(new_bucket).tap do |b|
b.logging_bucket = logging_bucket unless logging_bucket.nil?
Expand Down
23 changes: 10 additions & 13 deletions google-cloud-storage/lib/google/cloud/storage/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,8 @@ def list_bucket_acls bucket_name, user_project: nil
##
# Creates a new bucket ACL.
def insert_bucket_acl bucket_name, entity, role, user_project: nil
new_acl = Google::Apis::StorageV1::BucketAccessControl.new(
{ entity: entity, role: role }.delete_if { |_k, v| v.nil? }
)
params = { entity: entity, role: role }.delete_if { |_k, v| v.nil? }
new_acl = Google::Apis::StorageV1::BucketAccessControl.new(**params)
execute do
service.insert_bucket_access_control \
bucket_name, new_acl, user_project: user_project(user_project)
Expand Down Expand Up @@ -182,9 +181,8 @@ def list_default_acls bucket_name, user_project: nil
##
# Creates a new default ACL.
def insert_default_acl bucket_name, entity, role, user_project: nil
new_acl = Google::Apis::StorageV1::ObjectAccessControl.new(
{ entity: entity, role: role }.delete_if { |_k, v| v.nil? }
)
param = { entity: entity, role: role }.delete_if { |_k, v| v.nil? }
new_acl = Google::Apis::StorageV1::ObjectAccessControl.new(**param)
execute do
service.insert_default_object_access_control \
bucket_name, new_acl, user_project: user_project(user_project)
Expand Down Expand Up @@ -243,13 +241,13 @@ def list_notifications bucket_name, user_project: nil
def insert_notification bucket_name, topic_name, custom_attrs: nil,
event_types: nil, prefix: nil, payload: nil,
user_project: nil
new_notification = Google::Apis::StorageV1::Notification.new(
params =
{ custom_attributes: custom_attrs,
event_types: event_types(event_types),
object_name_prefix: prefix,
payload_format: payload_format(payload),
topic: topic_path(topic_name) }.delete_if { |_k, v| v.nil? }
)
new_notification = Google::Apis::StorageV1::Notification.new(**params)

execute do
service.insert_notification \
Expand Down Expand Up @@ -298,14 +296,14 @@ def insert_file bucket_name, source, path = nil, acl: nil,
storage_class: nil, key: nil, kms_key: nil,
temporary_hold: nil, event_based_hold: nil,
user_project: nil
file_obj = Google::Apis::StorageV1::Object.new(
params =
{ cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, md5_hash: md5,
content_encoding: content_encoding, crc32c: crc32c,
content_language: content_language, metadata: metadata,
storage_class: storage_class, temporary_hold: temporary_hold,
event_based_hold: event_based_hold }.delete_if { |_k, v| v.nil? }
)
file_obj = Google::Apis::StorageV1::Object.new(**params)
content_type ||= mime_type_for(path || Pathname(source).to_path)

execute do
Expand Down Expand Up @@ -432,9 +430,8 @@ def list_file_acls bucket_name, file_name, user_project: nil
# Creates a new file ACL.
def insert_file_acl bucket_name, file_name, entity, role,
generation: nil, user_project: nil
new_acl = Google::Apis::StorageV1::ObjectAccessControl.new(
{ entity: entity, role: role }.delete_if { |_k, v| v.nil? }
)
params = { entity: entity, role: role }.delete_if { |_k, v| v.nil? }
new_acl = Google::Apis::StorageV1::ObjectAccessControl.new(**params)
execute do
service.insert_object_access_control \
bucket_name, file_name, new_acl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ def empty_file_gapi cache_control: nil, content_disposition: nil,
content_encoding: nil, content_language: nil,
content_type: nil, crc32c: nil, md5: nil, metadata: nil,
storage_class: nil
Google::Apis::StorageV1::Object.new({
params = {
cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, md5_hash: md5,
content_encoding: content_encoding, crc32c: crc32c,
content_language: content_language, metadata: metadata,
storage_class: storage_class }.delete_if { |_k, v| v.nil? })
storage_class: storage_class }.delete_if { |_k, v| v.nil? }
Google::Apis::StorageV1::Object.new(**params)
end

def find_file_gapi bucket=nil, name = nil
Expand Down
9 changes: 5 additions & 4 deletions google-cloud-storage/test/google/cloud/storage/bucket_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@

mock = Minitest::Mock.new
mock.expect :insert_object, create_file_gapi(bucket.name, new_file_name),
[bucket.name, empty_file_gapi(options), name: new_file_name, predefined_acl: nil, upload_source: tmpfile, content_encoding: options[:content_encoding], content_type: options[:content_type], kms_key_name: nil, user_project: nil, options: {}]
[bucket.name, empty_file_gapi(**options), name: new_file_name, predefined_acl: nil, upload_source: tmpfile, content_encoding: options[:content_encoding], content_type: options[:content_type], kms_key_name: nil, user_project: nil, options: {}]

bucket.service.mocked_service = mock

bucket.create_file tmpfile, new_file_name, options
bucket.create_file tmpfile, new_file_name, **options

mock.verify
end
Expand Down Expand Up @@ -1086,13 +1086,14 @@ def empty_file_gapi cache_control: nil, content_disposition: nil,
content_type: nil, crc32c: nil, md5: nil, metadata: nil,
storage_class: nil, temporary_hold: nil,
event_based_hold: nil
Google::Apis::StorageV1::Object.new({
params = {
cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, md5_hash: md5,
content_encoding: content_encoding, crc32c: crc32c,
content_language: content_language, metadata: metadata,
storage_class: storage_class, temporary_hold: temporary_hold,
event_based_hold: event_based_hold }.delete_if { |_k, v| v.nil? })
event_based_hold: event_based_hold }.delete_if { |_k, v| v.nil? }
Google::Apis::StorageV1::Object.new(**params)
end

def find_file_gapi bucket=nil, name = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@

mock = Minitest::Mock.new
mock.expect :insert_object, create_file_gapi(bucket.name, new_file_name),
[bucket.name, empty_file_gapi(options), name: new_file_name, predefined_acl: nil, upload_source: tmpfile, content_encoding: options[:content_encoding], content_type: options[:content_type], kms_key_name: nil, user_project: nil, options: {}]
[bucket.name, empty_file_gapi(**options), name: new_file_name, predefined_acl: nil, upload_source: tmpfile, content_encoding: options[:content_encoding], content_type: options[:content_type], kms_key_name: nil, user_project: nil, options: {}]

bucket.service.mocked_service = mock

bucket.create_file tmpfile, new_file_name, options
bucket.create_file tmpfile, new_file_name, **options

mock.verify
end
Expand Down Expand Up @@ -1039,12 +1039,13 @@ def empty_file_gapi cache_control: nil, content_disposition: nil,
content_encoding: nil, content_language: nil,
content_type: nil, crc32c: nil, md5: nil, metadata: nil,
storage_class: nil
Google::Apis::StorageV1::Object.new({
params = {
cache_control: cache_control, content_type: content_type,
content_disposition: content_disposition, md5_hash: md5,
content_encoding: content_encoding, crc32c: crc32c,
content_language: content_language, metadata: metadata,
storage_class: storage_class }.delete_if { |_k, v| v.nil? })
storage_class: storage_class }.delete_if { |_k, v| v.nil? }
Google::Apis::StorageV1::Object.new(**params)
end

def find_file_gapi bucket=nil, name = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
origin: ["http://example.org", "https://example.org"],
http_method: ["*"],
response_header: ["X-My-Custom-Header"] }] }
let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new c } }
let(:bucket_cors_gapi) { bucket_cors.map { |c| Google::Apis::StorageV1::Bucket::CorsConfiguration.new **c } }
let(:kms_key) { "path/to/encryption_key_name" }
let(:bucket_retention_period) { 86400 }

Expand Down Expand Up @@ -765,7 +765,7 @@ def create_bucket_gapi name = nil, location: nil, storage_class: nil,
versioning: versioning, logging: logging, website: website,
cors_configurations: cors, billing: billing, lifecycle: lifecycle
}.delete_if { |_, v| v.nil? }
Google::Apis::StorageV1::Bucket.new options
Google::Apis::StorageV1::Bucket.new **options
end

def find_bucket_gapi name = nil
Expand Down

0 comments on commit 77bdc4a

Please sign in to comment.