uc3-dmp-api-core 0.0.1 → 0.0.3

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: 7033c9283948ba0b3b9a2cea45d17ba843f42b2d78f8a32f003d88cce9532c9e
4
- data.tar.gz: 7fbfe97abe42ee3005c37b246eac7eece4358cceb42a9c7d8fe4e1ae13f5c8ac
3
+ metadata.gz: e43205e6d19650a78cf8845c988c4272cd2c50deabdfe2cd36f90d767392b19a
4
+ data.tar.gz: 20095892d247fa52efd093c37f38daf6a90c410e198e191ea0829de862469a4b
5
5
  SHA512:
6
- metadata.gz: d253494718f77f3cbf5e72d65f331eebe00a5fc327551c6478c1c144c29028b6bbab79395a76a15883049ff89482011015bb8387116407cb1d27edc64c8e24ae
7
- data.tar.gz: fc0776170a8a1237f4b66564b83b12ff991548d6f98f4e8f43cce35414c3ba6a70e5e86f62155f710d09d230626510c46dcd64ad144d740dd7f5814e73abcf2b
6
+ metadata.gz: 948f688a92a84ae3998283d936cc6e605c28a266df2ee7d3b4dae31e2220b59365ef76e5627afa9e7d66daf3c8636d8a3f2b7d247e40da905500acb204b6feeb
7
+ data.tar.gz: 8c35c26fe2206c24457fed0e003dba47fe79885351a1b5cdad1d111d82fad41d9491ea89ce02a4290efc5a27fb6743db6b4475e6dceac799daf2fa07e140c139
@@ -2,6 +2,8 @@
2
2
 
3
3
  require_relative './notifier'
4
4
 
5
+ require 'notifier'
6
+
5
7
  module Uc3DmpApiCore
6
8
  # Standardized ways for logging messages and errors to CloudWatch
7
9
  #
@@ -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)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'paginator'
4
+
3
5
  module Uc3DmpApiCore
4
6
  # Use Rails' ActiveResource to communicate with the DMPHub REST API
5
7
  class Responder
@@ -28,46 +30,48 @@ module Uc3DmpApiCore
28
30
  # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
29
31
  def respond(status: DEFAULT_STATUS_CODE, items: [], errors: [], **args)
30
32
  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?
33
+ return _standard_error(url: url) if url.nil?
32
34
 
35
+ args = JSON.parse(args.to_json)
33
36
  errors = [errors] unless errors.nil? || errors.is_a?(Array)
34
37
  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
38
 
46
39
  body = {
47
40
  status: status.to_i,
48
41
  requested: url,
49
42
  requested_at: Time.now.strftime(TIMESTAMP_FORMAT),
50
43
  total_items: item_count,
51
- items: items.is_a?(Array) ? items.compact : []
44
+ items: items.is_a?(Array) ? Paginator.paginate(params: args, results: items) : [],
45
+ errors: errors
52
46
  }
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)
47
+ body = body.merge(Paginator.pagination_meta(url: url, item_count: item_count, params: args))
56
48
 
57
49
  # 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
50
+ log_error(source: url, message: errors, details: body, event: args[:event]) if status.to_s[0] == '5'
59
51
 
60
- { statusCode: status.to_i, body: body.to_json, headers: cors_headers.nil? ? {} : cors_headers }
52
+ { statusCode: status.to_i, body: body.compact.to_json, headers: headers }
61
53
  rescue StandardError => e
62
- puts "LambdaLayer: Responder.respond - #{e.message}"
54
+ puts "Uc3DmpApiCore.Responder.respond - #{e.message}"
63
55
  puts " - STACK: #{e.backtrace}"
64
- { statusCode: DEFAULT_STATUS_CODE, body: { errors: ["#{MSG_INVALID_ARGS} - resp err"] } }
56
+ _standard_error(url: url)
65
57
  end
66
58
  # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
67
59
  # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
68
60
 
69
61
  private
70
62
 
63
+ # Format an error response for any issues within this Responder!
64
+ def _standard_error(url:, status: DEFAULT_STATUS_CODE, message: Uc3DmpApiCore::MSG_SERVER_ERROR)
65
+ body = {
66
+ status: status.to_i,
67
+ requested: url,
68
+ requested_at: Time.now.strftime(TIMESTAMP_FORMAT),
69
+ total_items: 0,
70
+ errors: [message]
71
+ }
72
+ { statusCode: DEFAULT_STATUS_CODE, body: body.compact.to_json, headers: headers }
73
+ end
74
+
71
75
  # Figure out the requested URL from the Lambda event hash
72
76
  # --------------------------------------------------------------------------------
73
77
  def _url_from_event(event:)
@@ -78,6 +82,16 @@ module Uc3DmpApiCore
78
82
 
79
83
  "#{url}?#{event['queryStringParameters'].map { |k, v| "#{k}=#{v}" }.join('&')}"
80
84
  end
85
+
86
+ def headers
87
+ return {} if ENV['CORS_ORIGIN'].nil?
88
+
89
+ {
90
+ 'Access-Control-Allow-Headers': ENV['CORS_HEADERS'],
91
+ 'Access-Control-Allow-Origin': ENV['CORS_ORIGIN'],
92
+ 'Access-Control-Allow-Methods': ENV['CORS_METHODS']
93
+ }
94
+ end
81
95
  end
82
96
  end
83
97
  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.3'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'aws-sdk-sns'
4
4
  require 'aws-sdk-ssm'
5
5
 
6
+ require 'uc3-dmp-api-core/logger'
6
7
  require 'uc3-dmp-api-core/notifier'
7
8
  require 'uc3-dmp-api-core/paginator'
8
9
  require 'uc3-dmp-api-core/responder'
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.3
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