uc3-dmp-dynamo 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a3537f8c5fd1ccbf238e6f1229eba643ae138e5e2c5c0231a77bd04981a4e11
4
- data.tar.gz: a62767c144ff42c1c1b8e07b526fb6bb804d1a23a58aaa7585ec0c5a4444b4e7
3
+ metadata.gz: c011576d2723217aae4d7342bf91a859257e4216bd5206e044adf24bb29f7690
4
+ data.tar.gz: da6fa2f726afbfb00c0d55b331b014b29eb52369982ee6298b51c8b9d80ca979
5
5
  SHA512:
6
- metadata.gz: c0bd6548948a7171ad141987d0826b20af106d796ac42ff3ef2327ddb1acf6fd9003e8cdd9abecbf23f791d2ea4387fd169294cbf1582cb7fa72a7bc8c6e7846
7
- data.tar.gz: 69578c3575ba0984ad86330c2179086f7ce930ba2bb05a0e158241796733a112d3099b0b0a2ab12e49ee4f150ebcb9b112eee1f345bde45e6cf1e586c0df0768
6
+ metadata.gz: b267194e66c9970735c40bcd0146f0124d5692ea083954656826dd18a17d043a5b440c76d9d6c1444a6dcd473f7207b685603942bc32e0fa83042d14125080f1
7
+ data.tar.gz: 7ae5ef0d23a3d409075f885657898a3f720eaf0e4aef102af9b0fa881c40e48aa41b98adc971b1c40519884e4934028d3f3390f26d11f060b9d4b67ddbf3db52
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
4
3
  # TODO: Be sure to update the API functions so that they call cleanse_dmp_json before
5
4
  # calling Uc3DmpApiCore::Responder.respond !!!!!!!!!!
6
5
 
7
-
8
6
  module Uc3DmpDynamo
9
- class Uc3DmpDynamoError < StandardError; end
7
+ class ClientError < StandardError; end
10
8
 
11
9
  # Helper functions for working with Dynamo JSON
12
10
  class Client
@@ -18,33 +16,35 @@ module Uc3DmpDynamo
18
16
 
19
17
  attr_accessor :connection, :table
20
18
 
21
- def initialize(**args)
22
- @table = ENV['DYNAMO_TABLE']
23
- raise Uc3DmpDynamoError, MSG_MISSING_TABLE if @table.nil?
19
+ def initialize(**_args)
20
+ @table = ENV.fetch('DYNAMO_TABLE', nil)
21
+ raise ClientError, MSG_MISSING_TABLE if @table.nil?
24
22
 
25
23
  @connection = Aws::DynamoDB::Client.new(region: ENV.fetch('AWS_REGION', 'us-west-2'))
26
24
  end
27
25
 
28
26
  # Fetch a single item
27
+ # rubocop:disable Metrics/AbcSize
29
28
  def get_item(key:, debug: false)
30
- raise Uc3DmpDynamoError, MSG_INVALID_KEY unless key.is_a?(Hash)
29
+ raise ClientError, MSG_INVALID_KEY unless key.is_a?(Hash) && !key[:PK].nil?
31
30
 
32
31
  resp = @connection.get_item(
33
32
  { table_name: @table,
34
33
  key: key,
35
34
  consistent_read: false,
36
- return_consumed_capacity: debug ? 'TOTAL' : 'NONE'
37
- }
35
+ return_consumed_capacity: debug ? 'TOTAL' : 'NONE' }
38
36
  )
37
+
39
38
  # If debug is enabled then write the response to the LogWriter
40
39
  if debug
41
40
  puts "#{SOURCE} => get_item - #{key}"
42
41
  puts resp[:item].first.inspect
43
42
  end
44
- resp[:item].first
43
+ resp[:item].is_a?(Array) ? resp[:item].first : resp[:item]
45
44
  rescue Aws::Errors::ServiceError => e
46
- raise Uc3DmpDynamoError, MSG_DYNAMO_ERROR % { msg: e.message, trace: e.backtrace }
45
+ raise ClientError, format(MSG_DYNAMO_ERROR, msg: e.message, trace: e.backtrace)
47
46
  end
47
+ # rubocop:enable Metrics/AbcSize
48
48
 
49
49
  # Perform a table scan if a filter was specified.
50
50
  # For example:
@@ -52,9 +52,9 @@ module Uc3DmpDynamo
52
52
  # projection_expression: 'title, dmp_id, modified'
53
53
  #
54
54
  # See the DynamoDB docs for examples of key_conditions and projection_expressions
55
- def query(debug: false, **args)
56
- raise Uc3DmpDynamoError, MSG_INVALID_KEY unless args[:key_conditions].is_a?(Hash) &&
57
- !args[:key_conditions].keys.empty?
55
+ # rubocop:disable Metrics/AbcSize
56
+ def query(args:, debug: false)
57
+ raise ClientError, MSG_INVALID_KEY unless args.is_a?(Hash) && args.fetch(:key_conditions, {}).any?
58
58
 
59
59
  hash = {
60
60
  table_name: @table,
@@ -63,7 +63,7 @@ module Uc3DmpDynamo
63
63
  return_consumed_capacity: debug ? 'TOTAL' : 'NONE'
64
64
  }
65
65
  # Look for and add any other filtering or projection args
66
- %i[filter_expression, expression_attribute_values, projection_expression, scan_index_forward].each do |key|
66
+ %i[filter_expression expression_attribute_values projection_expression scan_index_forward].each do |key|
67
67
  next if args[key.to_sym].nil?
68
68
 
69
69
  hash[key.to_sym] = args[key.to_sym]
@@ -72,13 +72,13 @@ module Uc3DmpDynamo
72
72
  resp = @connection.query(hash)
73
73
  # If debug is enabled then write the response to the LogWriter
74
74
  if debug
75
- msg = "query - args: #{args}"
76
- puts "#{SOURCE} => query - args: #{args.inspect}"
75
+ puts "#{SOURCE} => query - args: #{hash.inspect}"
77
76
  puts resp.items.inspect
78
77
  end
79
- resp.items
78
+ resp.items.any? ? resp.items.map(&:item) : []
80
79
  rescue Aws::Errors::ServiceError => e
81
- raise Uc3DmpDynamoError, MSG_DYNAMO_ERROR % { msg: e.message, trace: e.backtrace }
80
+ raise ClientError, format(MSG_DYNAMO_ERROR, msg: e.message, trace: e.backtrace)
82
81
  end
82
+ # rubocop:enable Metrics/AbcSize
83
83
  end
84
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpDynamo
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -5,7 +5,7 @@ require 'aws-sdk-dynamodb'
5
5
 
6
6
  require 'uc3-dmp-dynamo/client'
7
7
 
8
+ # DynamoDB Table helpers for DMPTool
8
9
  module Uc3DmpDynamo
9
-
10
10
  end
11
11
  # rubocop:enable Naming/FileName
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uc3-dmp-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Riley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-09 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json