uc3-dmp-dynamo 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a3537f8c5fd1ccbf238e6f1229eba643ae138e5e2c5c0231a77bd04981a4e11
4
- data.tar.gz: a62767c144ff42c1c1b8e07b526fb6bb804d1a23a58aaa7585ec0c5a4444b4e7
3
+ metadata.gz: b0b0c89e4e576fc8416abb7a2b1f8d131bc5b75107c387a81d7f60e416ef0887
4
+ data.tar.gz: 3a278bf8304c366ee9ec4768e174b5918d4958c008d8106e58166dfac9a30892
5
5
  SHA512:
6
- metadata.gz: c0bd6548948a7171ad141987d0826b20af106d796ac42ff3ef2327ddb1acf6fd9003e8cdd9abecbf23f791d2ea4387fd169294cbf1582cb7fa72a7bc8c6e7846
7
- data.tar.gz: 69578c3575ba0984ad86330c2179086f7ce930ba2bb05a0e158241796733a112d3099b0b0a2ab12e49ee4f150ebcb9b112eee1f345bde45e6cf1e586c0df0768
6
+ metadata.gz: 84fc086e335afb9eed5c8acf57987658bd2580baf2dfa8bcb3edd44d0bdb0d38e0e8d68800e0ec3530706039894984af6bac882548389fa4c8139d915330081d
7
+ data.tar.gz: 1e1a9900aadbd51119408dfdfa0de0d66b0eb1cf588de109222587c7e63581f8bca87b9c6e5c1cb2b9c184c11d6316d7bb4ae6a661b550c03b81f2a790aeac18
@@ -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,17 @@ 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
+
79
+ puts "QUERY RESULT:"
80
+ puts resp.items.first.inspect
81
+
82
+ resp.items.any? ? resp.items.map(&:item) : []
80
83
  rescue Aws::Errors::ServiceError => e
81
- raise Uc3DmpDynamoError, MSG_DYNAMO_ERROR % { msg: e.message, trace: e.backtrace }
84
+ raise ClientError, format(MSG_DYNAMO_ERROR, msg: e.message, trace: e.backtrace)
82
85
  end
86
+ # rubocop:enable Metrics/AbcSize
83
87
  end
84
88
  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.6'
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.6
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-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json