uc3-dmp-api-core 0.0.1 → 0.0.2

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: 7033c9283948ba0b3b9a2cea45d17ba843f42b2d78f8a32f003d88cce9532c9e
4
- data.tar.gz: 7fbfe97abe42ee3005c37b246eac7eece4358cceb42a9c7d8fe4e1ae13f5c8ac
3
+ metadata.gz: b92c31a979f57739e53362d70fdb4e2a66f00ccad9666877c43d27ac5bf71afd
4
+ data.tar.gz: 6ea0e5d8d5d3f398f84678cb760b1b92bd7f8f66d471bb4c8ec545f7a4584017
5
5
  SHA512:
6
- metadata.gz: d253494718f77f3cbf5e72d65f331eebe00a5fc327551c6478c1c144c29028b6bbab79395a76a15883049ff89482011015bb8387116407cb1d27edc64c8e24ae
7
- data.tar.gz: fc0776170a8a1237f4b66564b83b12ff991548d6f98f4e8f43cce35414c3ba6a70e5e86f62155f710d09d230626510c46dcd64ad144d740dd7f5814e73abcf2b
6
+ metadata.gz: d4e33da9e043242cc900cac29557daef62b11cbe24a5475915920229aacc0f13a36935d4647587c3b027da1468baf608a89dd0f19bd165cd718633eacb64d5bf
7
+ data.tar.gz: d363a24996fc8962c45dba649f8aa096cc710cf16c0ad28fd2ad3c43fe0a1e8f971e2ec59365d32aa8eec6bdfcb39e63b8e335b71efc7d13eae58ff342018d3d
@@ -11,27 +11,35 @@ module Uc3DmpApiCore
11
11
  def paginate(params: {}, results:)
12
12
  return results unless results.is_a?(Array) && results.any? && params.is_a?(Hash)
13
13
 
14
- current = _current_page(item_count: item_count, params: params)
14
+ current = _current_page(item_count: results.length, params: params)
15
15
  # Just return as is if there is only one page
16
16
  return results if current[:total_pages] == 1 || current[:per_page] >= results.length
17
17
 
18
18
  # Calculate the offset and extract those results
19
19
  offset = current[:page] == 1 ? 0 : (current[:page] - 1) * current[:per_page]
20
- results[offset,(current[:per_page] - 1)]
20
+ results[offset,current[:per_page]]
21
21
  end
22
22
 
23
23
  # Construct the pagination meta information that will be included in the response
24
24
  def pagination_meta(url:, item_count: 0, params: {})
25
- current = _current_page(item_count: item_count, params: params)
26
- {
27
- page: current[:page],
28
- per_page: current[:per_page],
29
- total_items: item_count,
30
- first: _pagination_link(url: url, target_page: 1, per_page: per_page),
31
- prev: _pagination_link(url: url, target_page: page - 1, per_page: per_page),
32
- next: _pagination_link(url: url, target_page: page + 1, per_page: per_page),
33
- last: _pagination_link(url: url, target_page: total_pages, per_page: per_page)
34
- }.compact
25
+ prms = _current_page(item_count: item_count, params: params)
26
+
27
+ hash = {
28
+ page: prms[:page],
29
+ per_page: prms[:per_page],
30
+ total_items: item_count
31
+ }
32
+ return hash if prms[:total_pages] == 1 || item_count <= prms[:per_page]
33
+
34
+ prv = prms[:page] - 1
35
+ nxt = prms[:page] + 1
36
+ last = prms[:total_pages]
37
+
38
+ hash[:first] = _build_link(url: url, target_page: 1, per_page: prms[:per_page]) if prms[:page] > 1
39
+ hash[:prev] = _build_link(url: url, target_page: prv, per_page: prms[:per_page]) if prms[:page] > 1
40
+ hash[:next] = _build_link(url: url, target_page: nxt, per_page: prms[:per_page]) if prms[:page] < last
41
+ hash[:last] = _build_link(url: url, target_page: last, per_page: prms[:per_page]) if prms[:page] < last
42
+ hash.compact
35
43
  end
36
44
 
37
45
  private
@@ -41,7 +49,7 @@ module Uc3DmpApiCore
41
49
  page = params.fetch('page', DEFAULT_PAGE)
42
50
  page = DEFAULT_PAGE if page <= 1
43
51
  per_page = params.fetch('per_page', DEFAULT_PER_PAGE)
44
- per_page = DEFAULT_PER_PAGE if per_page >= MAXIMUM_PER_PAGE || per_page <= 1
52
+ per_page = DEFAULT_PER_PAGE if per_page >= MAXIMUM_PER_PAGE || per_page < 1
45
53
 
46
54
  total_pages = _page_count(total: item_count, per_page: per_page)
47
55
  page = total_pages if page > total_pages
@@ -51,7 +59,7 @@ module Uc3DmpApiCore
51
59
 
52
60
  # Generate a pagination link
53
61
  # --------------------------------------------------------------------------------
54
- def _pagination_link(url:, target_page:, per_page: DEFAULT_PER_PAGE)
62
+ def _build_link(url:, target_page:, per_page: DEFAULT_PER_PAGE)
55
63
  return nil if url.nil? || target_page.nil?
56
64
 
57
65
  link = _url_without_pagination(url: url)
@@ -28,46 +28,48 @@ module Uc3DmpApiCore
28
28
  # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
29
29
  def respond(status: DEFAULT_STATUS_CODE, items: [], errors: [], **args)
30
30
  url = _url_from_event(event: args[:event]) || SsmReader.get_ssm_value(key: 'api_base_url')
31
- return { statusCode: DEFAULT_STATUS_CODE, body: { errors: ["#{MSG_INVALID_ARGS} - resp"] } } if url.nil?
31
+ return _standard_error(url: url) if url.nil?
32
32
 
33
+ args = JSON.parse(args.to_json)
33
34
  errors = [errors] unless errors.nil? || errors.is_a?(Array)
34
35
  item_count = items.is_a?(Array) ? items.length : 0
35
- page = args[:page] || DEFAULT_PAGE
36
- per_page = args[:per_page] || DEFAULT_PER_PAGE
37
-
38
- unless ENV['CORS_ORIGIN'].nil?
39
- cors_headers = {
40
- 'Access-Control-Allow-Headers': ENV['CORS_HEADERS'],
41
- 'Access-Control-Allow-Origin': ENV['CORS_ORIGIN'],
42
- 'Access-Control-Allow-Methods': ENV['CORS_METHODS']
43
- }
44
- end
45
36
 
46
37
  body = {
47
38
  status: status.to_i,
48
39
  requested: url,
49
40
  requested_at: Time.now.strftime(TIMESTAMP_FORMAT),
50
41
  total_items: item_count,
51
- items: items.is_a?(Array) ? items.compact : []
42
+ items: items.is_a?(Array) ? Paginator.paginate(params: args, results: items) : [],
43
+ errors: errors
52
44
  }
53
-
54
- body[:errors] = errors if errors.is_a?(Array) && errors.any?
55
- body = _paginate(url: url, item_count: item_count, body: body, page: page, per_page: per_page)
45
+ body = body.merge(Paginator.pagination_meta(url: url, item_count: item_count, params: args))
56
46
 
57
47
  # If this is a server error, then notify the administrator!
58
- log_error(source: url, message: errors, details: body, event: args[:event]) if status == 500
48
+ log_error(source: url, message: errors, details: body, event: args[:event]) if status.to_s[0] == '5'
59
49
 
60
- { statusCode: status.to_i, body: body.to_json, headers: cors_headers.nil? ? {} : cors_headers }
50
+ { statusCode: status.to_i, body: body.compact.to_json, headers: headers }
61
51
  rescue StandardError => e
62
- puts "LambdaLayer: Responder.respond - #{e.message}"
52
+ puts "Uc3DmpApiCore.Responder.respond - #{e.message}"
63
53
  puts " - STACK: #{e.backtrace}"
64
- { statusCode: DEFAULT_STATUS_CODE, body: { errors: ["#{MSG_INVALID_ARGS} - resp err"] } }
54
+ _standard_error(url: url)
65
55
  end
66
56
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
67
57
  # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
68
58
 
69
59
  private
70
60
 
61
+ # Format an error response for any issues within this Responder!
62
+ def _standard_error(url:, status: DEFAULT_STATUS_CODE, message: Uc3DmpApiCore::MSG_SERVER_ERROR)
63
+ body = {
64
+ status: status.to_i,
65
+ requested: url,
66
+ requested_at: Time.now.strftime(TIMESTAMP_FORMAT),
67
+ total_items: 0,
68
+ errors: [message]
69
+ }
70
+ { statusCode: DEFAULT_STATUS_CODE, body: body.compact.to_json, headers: headers }
71
+ end
72
+
71
73
  # Figure out the requested URL from the Lambda event hash
72
74
  # --------------------------------------------------------------------------------
73
75
  def _url_from_event(event:)
@@ -78,6 +80,16 @@ module Uc3DmpApiCore
78
80
 
79
81
  "#{url}?#{event['queryStringParameters'].map { |k, v| "#{k}=#{v}" }.join('&')}"
80
82
  end
83
+
84
+ def headers
85
+ return {} if ENV['CORS_ORIGIN'].nil?
86
+
87
+ {
88
+ 'Access-Control-Allow-Headers': ENV['CORS_HEADERS'],
89
+ 'Access-Control-Allow-Origin': ENV['CORS_ORIGIN'],
90
+ 'Access-Control-Allow-Methods': ENV['CORS_METHODS']
91
+ }
92
+ end
81
93
  end
82
94
  end
83
95
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'aws-sdk-ssm'
4
4
 
5
+ require 'logger'
6
+
5
7
  module Uc3DmpApiCore
6
8
  # ----------------------------------------------------
7
9
  # SSM Parameter Store Helper
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpApiCore
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uc3-dmp-api-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-04-28 00:00:00.000000000 Z
11
+ date: 2023-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json