Skip to content

Cleanup code styling/quality according to rubocop. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws-sessionstore-dynamodb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency 'aws-sdk-v1'
spec.add_dependency 'aws-sdk'
spec.add_dependency 'rack', '~> 1.0'
end
1 change: 0 additions & 1 deletion lib/aws-sessionstore-dynamodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS
module SessionStore
module DynamoDB; end
Expand Down
10 changes: 4 additions & 6 deletions lib/aws/session_store/dynamo_db/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# language governing permissions and limitations under the License.

require 'yaml'
require 'aws-sdk-v1'
require 'aws-sdk'

module AWS::SessionStore::DynamoDB
# This class provides a Configuration object for all DynamoDB transactions
Expand Down Expand Up @@ -66,8 +66,7 @@ class Configuration
:lock_expiry_time => 500,
:lock_retry_delay => 500,
:lock_max_wait_time => 1,
:secret_key => nil,
:api_version => '2012-08-10'
:secret_key => nil
}

# @return [String] Session table name.
Expand Down Expand Up @@ -134,7 +133,6 @@ class Configuration
# before giving up.
attr_reader :lock_max_wait_time


# Provides configuration object that allows access to options defined
# during Runtime, in a YAML file, in the ENV and by default.
#
Expand All @@ -154,7 +152,7 @@ class Configuration
# See AWS DynamoDB documentation for table write_capacity for more
# information on this setting.
# @option options [DynamoDB Client] :dynamo_db_client
# (AWS::DynamoDB::ClientV2) DynamoDB client used to perform database
# (Aws::DynamoDB::ClientV2) DynamoDB client used to perform database
# operations inside of middleware application.
# @option options [Boolean] :raise_errors (false) If true, all errors are
# raised up the stack when default ErrorHandler. If false, Only specified
Expand Down Expand Up @@ -203,7 +201,7 @@ def to_hash
# @return [Hash] DDB client.
def gen_dynamo_db_client
client_opts = client_subset(@options)
client = AWS::DynamoDB::Client
client = Aws::DynamoDB::Client
dynamo_db_client = @options[:dynamo_db_client] || client.new(client_opts)
{:dynamo_db_client => dynamo_db_client}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/aws/session_store/dynamo_db/errors/base_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class BaseHandler
# error up the stack.
# You may reraise the error passed.
#
# @param [AWS::DynamoDB::Errors::Base] error error passed in from
# @param [Aws::DynamoDB::Errors::Base] error error passed in from
# AWS::SessionStore::DynamoDB::RackMiddleware.
# @param [Rack::Request::Environment,nil] env Rack environment
# @return [false] If exception was handled and will not reraise exception.
# @raise [AWS::DynamoDB::Errors] If error has be reraised.
# @raise [Aws::DynamoDB::Errors] If error has be reraised.
def handle_error(error, env = {})
raise NotImplementedError
end
Expand Down
5 changes: 2 additions & 3 deletions lib/aws/session_store/dynamo_db/errors/default_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS::SessionStore::DynamoDB::Errors
# This class handles errors raised from DynamoDB.
class DefaultHandler < AWS::SessionStore::DynamoDB::Errors::BaseHandler
# Array of errors that will always be passed up the Rack stack.
HARD_ERRORS = [
AWS::DynamoDB::Errors::ResourceNotFoundException,
AWS::DynamoDB::Errors::ConditionalCheckFailedException,
Aws::DynamoDB::Errors::ResourceNotFoundException,
Aws::DynamoDB::Errors::ConditionalCheckFailedException,
AWS::SessionStore::DynamoDB::MissingSecretKeyError,
AWS::SessionStore::DynamoDB::LockWaitTimeoutError
]
Expand Down
2 changes: 1 addition & 1 deletion lib/aws/session_store/dynamo_db/garbage_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

require 'aws-sdk-v1'
require 'aws-sdk'

module AWS::SessionStore::DynamoDB
# Collects and deletes unwanted sessions based on
Expand Down
46 changes: 22 additions & 24 deletions lib/aws/session_store/dynamo_db/locking/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS::SessionStore::DynamoDB::Locking
# This class provides a framework for implementing
# locking strategies.
class Base

# Creates configuration object.
def initialize(cfg)
@config = cfg
Expand All @@ -28,18 +26,18 @@ def set_session_data(env, sid, session, options = {})
packed_session = pack_data(session)
handle_error(env) do
save_opts = update_opts(env, sid, packed_session, options)
result = @config.dynamo_db_client.update_item(save_opts)
_result = @config.dynamo_db_client.update_item(save_opts)
sid
end
end

# Packs session data.
def pack_data(data)
[Marshal.dump(data)].pack("m*")
[Marshal.dump(data)].pack('m*')
end

# Gets session data.
def get_session_data(env, sid)
def get_session_data(_env, _sid)
raise NotImplementedError
end

Expand All @@ -53,12 +51,10 @@ def delete_session(env, sid)
# Each database operation is placed in this rescue wrapper.
# This wrapper will call the method, rescue any exceptions and then pass
# exceptions to the configured error handler.
def handle_error(env = nil, &block)
begin
yield
rescue AWS::DynamoDB::Errors::Base => e
@config.error_handler.handle_error(e, env)
end
def handle_error(env = nil, &_block)
yield
rescue Aws::DynamoDB::Errors::Base => e
@config.error_handler.handle_error(e, env)
end

private
Expand Down Expand Up @@ -94,45 +90,45 @@ def save_exists_opts(env, sid, session, options = {})

# Unmarshal the data.
def unpack_data(packed_data)
Marshal.load(packed_data.unpack("m*").first)
Marshal.load(packed_data.unpack('m*').first)
end

# Table options for client.
def table_opts(sid)
{
:table_name => @config.table_name,
:key => {@config.table_key => {:s => sid}}
table_name: @config.table_name,
key: { @config.table_key => sid }
}
end

# Attributes to update via client.
def attr_updts(env, session, add_attrs = {})
data = data_unchanged?(env, session) ? {} : data_attr(session)
{
:attribute_updates => merge_all(updated_attr, data, add_attrs),
:return_values => "UPDATED_NEW"
attribute_updates: merge_all(updated_attr, data, add_attrs),
return_values: 'UPDATED_NEW'
}
end

# Update client with current time attribute.
def updated_at
{ :value => {:n => "#{(Time.now).to_f}"}, :action => "PUT" }
{ value: Time.now.to_f.to_s, action: 'PUT' }
end

# Attribute for creation of session.
def created_attr
{ "created_at" => updated_at }
{ 'created_at' => updated_at }
end

# Attribute for updating session.
def updated_attr
{
"updated_at" => updated_at
'updated_at' => updated_at
}
end

def data_attr(session)
{ "data" => {:value => {:s => session}, :action => "PUT"} }
{ 'data' => { value: session, action: 'PUT' } }
end

# Determine if data has been manipulated
Expand All @@ -143,19 +139,21 @@ def data_unchanged?(env, session)

# Expected attributes
def expected_attributes(sid)
{ :expected => {@config.table_key => {:value => {:s => sid}, :exists => true}} }
{ expected: { @config.table_key => { value: sid, exists: true } } }
end

# Attributes to be retrieved via client
def attr_opts
{:attributes_to_get => ["data"],
:consistent_read => @config.consistent_read}
{
attributes_to_get: ['data'],
consistent_read: @config.consistent_read
}
end

# @return [Hash] merged hash of all hashes passed in.
def merge_all(*hashes)
new_hash = {}
hashes.each{|hash| new_hash.merge!(hash)}
hashes.each { |hash| new_hash.merge!(hash) }
new_hash
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/aws/session_store/dynamo_db/locking/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS::SessionStore::DynamoDB::Locking
# This class gets and sets sessions
# without a locking strategy.
Expand All @@ -32,9 +31,10 @@ def get_session_opts(sid)

# @return [String] Session data.
def extract_data(env, result = nil)
env['rack.initial_data'] = result[:item]["data"][:s] if result[:item]
unpack_data(result[:item]["data"][:s]) if result[:item]
if result[:item]
env['rack.initial_data'] = result[:item]['data']
unpack_data(result[:item]['data'])
end
end

end
end
43 changes: 25 additions & 18 deletions lib/aws/session_store/dynamo_db/locking/pessimistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.


module AWS::SessionStore::DynamoDB::Locking
# This class implements a pessimistic locking strategy for the
# DynamoDB session handler. Sessions obtain an exclusive lock
Expand Down Expand Up @@ -43,7 +42,7 @@ def get_session_with_lock(env, sid)
exceeded_wait_time?(max_attempt_date)
begin
result = attempt_set_lock(sid)
rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
expires_at ||= get_expire_date(sid)
next if expires_at.nil?
result = bust_lock(sid, expires_at)
Expand All @@ -70,15 +69,15 @@ def get_lock_time_opts(sid)
# @return [Time] Time stamp for which the session was locked.
def lock_time(sid)
result = @config.dynamo_db_client.get_item(get_lock_time_opts(sid))
(result[:item]["locked_at"][:n]).to_f if result[:item]["locked_at"]
result[:item]['locked_at'].to_f if result[:item]['locked_at']
end

# @return [String] Session data.
def get_data(env, result)
lock_time = result[:attributes]["locked_at"][:n]
env["locked_at"] = (lock_time).to_f
env['rack.initial_data'] = result[:item]["data"][:s] if result[:item]
unpack_data(result[:attributes]["data"][:s])
lock_time = result[:attributes]['locked_at']
env['locked_at'] = lock_time.to_f
env['rack.initial_data'] = result[:item]['data'] if result[:item]
unpack_data(result[:attributes]['data'])
end

# Attempt to bust the lock if the expiration date has expired.
Expand Down Expand Up @@ -112,20 +111,21 @@ def attempt_set_lock(sid)
# Lock attribute - time stamp of when session was locked.
def lock_attr
{
:attribute_updates => {"locked_at" => updated_at},
:return_values => "ALL_NEW"
attribute_updates: { 'locked_at' => updated_at },
return_values: 'ALL_NEW'
}
end

# Time in which session was updated.
def updated_at
{ :value => {:n => "#{(Time.now).to_f}"}, :action => "PUT" }
{ value: Time.now.to_f.to_s, action: 'PUT' }
end

# Attributes for locking.
def add_lock_attrs(env)
{
:add_attrs => add_attr, :expect_attr => expect_lock_time(env)
add_attrs: add_attr,
expect_attr: expect_lock_time(env)
}
end

Expand All @@ -136,25 +136,32 @@ def set_lock_options(env, options = {})

# Lock expectation.
def lock_expect
{ :expected => { "locked_at" => { :exists => false } } }
{ expected: { 'locked_at' => { exists: false } } }
end

# Option to delete lock.
def add_attr
{ "locked_at" => {:action => "DELETE"} }
{ 'locked_at' => { action: 'DELETE' } }
end

# Expectation of when lock was set.
def expect_lock_time(env)
{ :expected => {"locked_at" => {
:value => {:n => "#{env["locked_at"]}"}, :exists => true}} }
{
expected: {
'locked_at' => {
value: env['locked_at'].to_s,
exists: true
}
}
}
end

# Attributes to be retrieved via client
def lock_opts
{:attributes_to_get => ["locked_at"],
:consistent_read => @config.consistent_read}
{
attributes_to_get: ['locked_at'],
consistent_read: @config.consistent_read
}
end

end
end
Loading