Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Moved resource class customizations into their own files.
Browse files Browse the repository at this point in the history
Moved `Aws::S3::Object` and `Aws::SQS::Queue` customizations into
'aws-sdk-resources/lib/s3/object.rb' and 'aws-sdk-resources/lib/sqs/queue.rb'
respectively.
  • Loading branch information
trevorrowe committed Oct 20, 2014
1 parent 5217ae9 commit ebe7276
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 181 deletions.
84 changes: 1 addition & 83 deletions aws-sdk-resources/lib/aws-sdk-resources/s3.rb
Original file line number Diff line number Diff line change
@@ -1,87 +1,5 @@
module Aws
module S3

autoload :FilePart, 'aws-sdk-resources/s3/file_part'
autoload :FileUploader, 'aws-sdk-resources/s3/file_uploader'
autoload :MultipartFileUploader, 'aws-sdk-resources/s3/multipart_file_uploader'
autoload :MultipartUploadError, 'aws-sdk-resources/s3/multipart_upload_error'

class Object

# Generates a pre-signed URL for this object.
#
# @example Pre-signed GET URL, valid for one hour
#
# obj.presigned_url(:get, expires_in: 3600)
# #=> "https://bucket-name.s3.amazonaws.com/object-key?..."
#
# @example Pre-signed PUT with a canned ACL
#
# # the object uploaded using this URL will be publicly accessible
# obj.presigned_url(:put, acl: 'public-read')
# #=> "https://bucket-name.s3.amazonaws.com/object-key?..."
#
# @param [Symbol] http_method
# The HTTP method to generate a presigned URL for. Valid values
# are `:get`, `:put`, `:head`, and `:delete`.
#
# @param [Hash] params
# Additional request parameters to use when generating the pre-signed
# URL. See the related documentation in {Client} for accepted
# params.
#
# | HTTP Method | Client Method |
# |---------------|------------------------|
# | `:get` | {Client#put_object} |
# | `:put` | {Client#get_object} |
# | `:head` | {Client#head_object} |
# | `:delete` | {Client#delete_object} |
#
# @option params [Integer] :exipres_in (900) Number of seconds before
# the pre-signed URL expires. This may not exceed one week (604800
# seconds).
#
# @raise [ArgumentError] Raised if `:expires_in` exceeds one week
# (604800 seconds).
#
# @return [String]
#
def presigned_url(http_method, params = {})
presigner = Presigner.new(client: client)
presigner.presigned_url("#{http_method.downcase}_object", params.merge(
bucket: bucket_name,
key: key,
))
end

# Uploads a file from disk to the current object in S3.
#
# @example
#
# obj.upload_file('/path/to/file')
#
# @param [String,Pathname,File,Tempfile] source A file or path to a file
# on the local file system that should be uploaded to this object.
#
# @option options [Integer] :multipart_threshold (15728640) Files larger
# than `:multipart_threshold` are uploaded using the S3 multipart APIs.
# Default threshold is 15MB.
#
# @raise [MultipartUploadError] If an object is being uploaded in
# parts, and the upload can not be completed, then the upload is
# aborted and this error is raised. The raised error has a `#errors`
# method that returns the failures that caused the upload to be
# aborted.
#
# @return [void]
#
def upload_file(source, options = {})
uploader = FileUploader.new(
multipart_threshold: options.delete(:multipart_threshold),
client: client)
uploader.upload(source, options.merge(bucket: bucket_name, key: key))
end

end
require 'aws-sdk-resources/s3/object'
end
end
87 changes: 87 additions & 0 deletions aws-sdk-resources/lib/aws-sdk-resources/s3/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Aws
module S3

autoload :FilePart, 'aws-sdk-resources/s3/file_part'
autoload :FileUploader, 'aws-sdk-resources/s3/file_uploader'
autoload :MultipartFileUploader, 'aws-sdk-resources/s3/multipart_file_uploader'
autoload :MultipartUploadError, 'aws-sdk-resources/s3/multipart_upload_error'

class Object

# Generates a pre-signed URL for this object.
#
# @example Pre-signed GET URL, valid for one hour
#
# obj.presigned_url(:get, expires_in: 3600)
# #=> "https://bucket-name.s3.amazonaws.com/object-key?..."
#
# @example Pre-signed PUT with a canned ACL
#
# # the object uploaded using this URL will be publicly accessible
# obj.presigned_url(:put, acl: 'public-read')
# #=> "https://bucket-name.s3.amazonaws.com/object-key?..."
#
# @param [Symbol] http_method
# The HTTP method to generate a presigned URL for. Valid values
# are `:get`, `:put`, `:head`, and `:delete`.
#
# @param [Hash] params
# Additional request parameters to use when generating the pre-signed
# URL. See the related documentation in {Client} for accepted
# params.
#
# | HTTP Method | Client Method |
# |---------------|------------------------|
# | `:get` | {Client#put_object} |
# | `:put` | {Client#get_object} |
# | `:head` | {Client#head_object} |
# | `:delete` | {Client#delete_object} |
#
# @option params [Integer] :exipres_in (900) Number of seconds before
# the pre-signed URL expires. This may not exceed one week (604800
# seconds).
#
# @raise [ArgumentError] Raised if `:expires_in` exceeds one week
# (604800 seconds).
#
# @return [String]
#
def presigned_url(http_method, params = {})
presigner = Presigner.new(client: client)
presigner.presigned_url("#{http_method.downcase}_object", params.merge(
bucket: bucket_name,
key: key,
))
end

# Uploads a file from disk to the current object in S3.
#
# @example
#
# obj.upload_file('/path/to/file')
#
# @param [String,Pathname,File,Tempfile] source A file or path to a file
# on the local file system that should be uploaded to this object.
#
# @option options [Integer] :multipart_threshold (15728640) Files larger
# than `:multipart_threshold` are uploaded using the S3 multipart APIs.
# Default threshold is 15MB.
#
# @raise [MultipartUploadError] If an object is being uploaded in
# parts, and the upload can not be completed, then the upload is
# aborted and this error is raised. The raised error has a `#errors`
# method that returns the failures that caused the upload to be
# aborted.
#
# @return [void]
#
def upload_file(source, options = {})
uploader = FileUploader.new(
multipart_threshold: options.delete(:multipart_threshold),
client: client)
uploader.upload(source, options.merge(bucket: bucket_name, key: key))
end

end
end
end
99 changes: 1 addition & 98 deletions aws-sdk-resources/lib/aws-sdk-resources/sqs.rb
Original file line number Diff line number Diff line change
@@ -1,102 +1,5 @@
module Aws
module SQS
class Queue
class << self

private

def queue_str_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
attributes[name]
end
end

def queue_int_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
if value = attributes[name]
value.to_i
end
end
end

def queue_time_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
if value = attributes[name]
Time.at(value.to_i)
end
end
end

def attr_name(method_name)
method_name.to_s.split('_').map { |s| s[0].upcase + s[1..-1] }.join
end

end

# @group Queue Attributes
# @return [String] the queue's policy.
queue_str_attr :policy

# @group Queue Attributes
# @return [Integer] the visibility timeout for the queue. For more information about visibility timeout, see [Visibility Timeout](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html) in the Amazon SQS Developer Guide.
queue_int_attr :visibility_timeout

# @group Queue Attributes
# @return [Integer] the limit of how many bytes a message can contain before
# Amazon SQS rejects it.
queue_int_attr :maximum_message_size

# @group Queue Attributes
# @return [Integer] the number of seconds Amazon SQS retains a message.
queue_int_attr :message_retention_period

# @group Queue Attributes
# @return [Integer] the approximate number of visible messages in a queue.
# For more information, see [Resources Required to Process Messages](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ApproximateNumber.html) in the Amazon SQS Developer Guide.
queue_int_attr :approximate_number_of_messages

# @group Queue Attributes
# @return [Integer] returns the approximate number of messages that are not timed-out and not deleted. For more information, see [Resources Required to Process Messages](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ApproximateNumber.html) in the Amazon SQS Developer Guide.
queue_int_attr :approximate_number_of_messages_not_visible

# @group Queue Attributes
# @return [Time] the time when the queue was created.
queue_time_attr :created_timestamp

# @group Queue Attributes
# @return [Time] the time when the queue was last changed.
queue_time_attr :last_modified_timestamp

# @group Queue Attributes
# @return [String] the queue's Amazon resource name (ARN).
queue_str_attr :arn, name: 'QueueArn'

alias queue_arn arn

# @group Queue Attributes
# @return [Integer] returns the approximate number of messages that
# are pending to be added to the queue.
queue_int_attr :approximate_number_of_messages_delayed

# @group Queue Attributes
# @return [Integer] the default delay on the queue in seconds.
queue_int_attr :delay_seconds

# @group Queue Attributes
# @return [Integer] the time for which a {Client#receive_message} call
# will wait for a message to arrive.
queue_int_attr :receive_message_wait_time_seconds

# @group Queue Attributes
# @return [String] the parameters for dead letter queue functionality of
# the source queue. For more information about RedrivePolicy and dead
# letter queues, see [Using Amazon SQS Dead Letter Queues](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html)
# in the Amazon SQS Developer Guide.
queue_str_attr :redrive_policy

end
require 'aws-sdk-resources/sqs/queue.rb'
end
end
102 changes: 102 additions & 0 deletions aws-sdk-resources/lib/aws-sdk-resources/sqs/queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module Aws
module SQS
class Queue
class << self

private

def queue_str_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
attributes[name]
end
end

def queue_int_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
if value = attributes[name]
value.to_i
end
end
end

def queue_time_attr(method_name, options = {})
name = options[:name] || attr_name(method_name)
define_method(method_name) do
if value = attributes[name]
Time.at(value.to_i)
end
end
end

def attr_name(method_name)
method_name.to_s.split('_').map { |s| s[0].upcase + s[1..-1] }.join
end

end

# @group Queue Attributes
# @return [String] the queue's policy.
queue_str_attr :policy

# @group Queue Attributes
# @return [Integer] the visibility timeout for the queue. For more information about visibility timeout, see [Visibility Timeout](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/AboutVT.html) in the Amazon SQS Developer Guide.
queue_int_attr :visibility_timeout

# @group Queue Attributes
# @return [Integer] the limit of how many bytes a message can contain before
# Amazon SQS rejects it.
queue_int_attr :maximum_message_size

# @group Queue Attributes
# @return [Integer] the number of seconds Amazon SQS retains a message.
queue_int_attr :message_retention_period

# @group Queue Attributes
# @return [Integer] the approximate number of visible messages in a queue.
# For more information, see [Resources Required to Process Messages](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ApproximateNumber.html) in the Amazon SQS Developer Guide.
queue_int_attr :approximate_number_of_messages

# @group Queue Attributes
# @return [Integer] returns the approximate number of messages that are not timed-out and not deleted. For more information, see [Resources Required to Process Messages](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/ApproximateNumber.html) in the Amazon SQS Developer Guide.
queue_int_attr :approximate_number_of_messages_not_visible

# @group Queue Attributes
# @return [Time] the time when the queue was created.
queue_time_attr :created_timestamp

# @group Queue Attributes
# @return [Time] the time when the queue was last changed.
queue_time_attr :last_modified_timestamp

# @group Queue Attributes
# @return [String] the queue's Amazon resource name (ARN).
queue_str_attr :arn, name: 'QueueArn'

alias queue_arn arn

# @group Queue Attributes
# @return [Integer] returns the approximate number of messages that
# are pending to be added to the queue.
queue_int_attr :approximate_number_of_messages_delayed

# @group Queue Attributes
# @return [Integer] the default delay on the queue in seconds.
queue_int_attr :delay_seconds

# @group Queue Attributes
# @return [Integer] the time for which a {Client#receive_message} call
# will wait for a message to arrive.
queue_int_attr :receive_message_wait_time_seconds

# @group Queue Attributes
# @return [String] the parameters for dead letter queue functionality of
# the source queue. For more information about RedrivePolicy and dead
# letter queues, see [Using Amazon SQS Dead Letter Queues](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html)
# in the Amazon SQS Developer Guide.
queue_str_attr :redrive_policy

end
end
end

0 comments on commit ebe7276

Please sign in to comment.