uc3-dmp-api-core 0.0.1 → 0.0.3
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 +4 -4
- data/lib/uc3-dmp-api-core/logger.rb +2 -0
- data/lib/uc3-dmp-api-core/paginator.rb +22 -14
- data/lib/uc3-dmp-api-core/responder.rb +33 -19
- data/lib/uc3-dmp-api-core/ssm_reader.rb +2 -0
- data/lib/uc3-dmp-api-core/version.rb +1 -1
- data/lib/uc3-dmp-api-core.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e43205e6d19650a78cf8845c988c4272cd2c50deabdfe2cd36f90d767392b19a
|
4
|
+
data.tar.gz: 20095892d247fa52efd093c37f38daf6a90c410e198e191ea0829de862469a4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 948f688a92a84ae3998283d936cc6e605c28a266df2ee7d3b4dae31e2220b59365ef76e5627afa9e7d66daf3c8636d8a3f2b7d247e40da905500acb204b6feeb
|
7
|
+
data.tar.gz: 8c35c26fe2206c24457fed0e003dba47fe79885351a1b5cdad1d111d82fad41d9491ea89ce02a4290efc5a27fb6743db6b4475e6dceac799daf2fa07e140c139
|
@@ -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:
|
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,
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
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
|
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
|
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) ?
|
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 ==
|
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:
|
52
|
+
{ statusCode: status.to_i, body: body.compact.to_json, headers: headers }
|
61
53
|
rescue StandardError => e
|
62
|
-
puts "
|
54
|
+
puts "Uc3DmpApiCore.Responder.respond - #{e.message}"
|
63
55
|
puts " - STACK: #{e.backtrace}"
|
64
|
-
|
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
|
data/lib/uc3-dmp-api-core.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2023-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|