gds-api-adapters 51.1.1 → 51.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gds_api/content_store.rb +92 -0
- data/lib/gds_api/version.rb +1 -1
- 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: 13bc5ea4fc372321c69ee203e127d0085826a0f298a570567872738ad67a6282
|
4
|
+
data.tar.gz: 3d5941c5f8cc9d998e2c40c7c5f4b2ebd2ef245bf7685046ba052987af142a2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c77fd55ff062baf4329a301fffabc9d8fdf6593e7c3a66ebe3763ffdf27bb962d2b2279c0d9e3506f160fe741042126800a9ae0cc0219774369efb96f66152b
|
7
|
+
data.tar.gz: 8522b34d180b03fa7e538dedc6ba22ce7495686e49d42d78f1664b90b34c717cd8fcf747a4476618b341b5febf800fbfb5886aa26701673a3c7fd9dd6955ff55
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require "plek"
|
2
|
+
|
1
3
|
require_relative 'base'
|
2
4
|
require_relative 'exceptions'
|
3
5
|
|
@@ -18,9 +20,99 @@ class GdsApi::ContentStore < GdsApi::Base
|
|
18
20
|
raise "`ContentStore#content_item!` is deprecated. Use `ContentStore#content_item` instead"
|
19
21
|
end
|
20
22
|
|
23
|
+
# Returns an array tuple of destination url with status code e.g
|
24
|
+
# ["https://www.gov.uk/destination", 301]
|
25
|
+
def self.redirect_for_path(content_item, request_path, request_query = "")
|
26
|
+
RedirectResolver.call(content_item, request_path, request_query)
|
27
|
+
end
|
28
|
+
|
21
29
|
private
|
22
30
|
|
23
31
|
def content_item_url(base_path)
|
24
32
|
"#{endpoint}/content#{base_path}"
|
25
33
|
end
|
34
|
+
|
35
|
+
class RedirectResolver
|
36
|
+
def initialize(content_item, request_path, request_query = "")
|
37
|
+
@content_item = content_item
|
38
|
+
@request_path = request_path
|
39
|
+
@request_query = request_query.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.call(*args)
|
43
|
+
new(*args).call
|
44
|
+
end
|
45
|
+
|
46
|
+
def call
|
47
|
+
redirect = redirect_for_path(request_path)
|
48
|
+
raise UnresolvedRedirect, "Could not find a matching redirect" unless redirect
|
49
|
+
|
50
|
+
destination_uri = URI.parse(
|
51
|
+
resolve_destination(redirect, request_path, request_query)
|
52
|
+
)
|
53
|
+
|
54
|
+
url = if destination_uri.absolute?
|
55
|
+
destination_uri.to_s
|
56
|
+
else
|
57
|
+
"#{Plek.new.website_root}#{destination_uri}"
|
58
|
+
end
|
59
|
+
|
60
|
+
[url, status_code(redirect)]
|
61
|
+
end
|
62
|
+
|
63
|
+
private_class_method :new
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
attr_reader :content_item, :request_path, :request_query
|
68
|
+
|
69
|
+
def redirect_for_path(path)
|
70
|
+
redirects_by_segments.find do |r|
|
71
|
+
next true if r["path"] == path
|
72
|
+
route_prefix_match?(r["path"], path) if r["type"] == "prefix"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def redirects_by_segments
|
77
|
+
redirects = content_item["redirects"] || []
|
78
|
+
redirects.sort_by { |r| r["path"].split("/").count * -1 }
|
79
|
+
end
|
80
|
+
|
81
|
+
def route_prefix_match?(prefix_path, path_to_match)
|
82
|
+
prefix_regex = %r{^#{Regexp.escape(prefix_path)}/}
|
83
|
+
path_to_match.match prefix_regex
|
84
|
+
end
|
85
|
+
|
86
|
+
def resolve_destination(redirect, path, query)
|
87
|
+
return redirect["destination"] unless redirect["segments_mode"] == "preserve"
|
88
|
+
|
89
|
+
if redirect["type"] == "prefix"
|
90
|
+
prefix_destination(redirect, path, query)
|
91
|
+
else
|
92
|
+
redirect["destination"] + (query.empty? ? "" : "?#{query}")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def prefix_destination(redirect, path, query)
|
97
|
+
uri = URI.parse(redirect["destination"])
|
98
|
+
start_char = redirect["path"].length
|
99
|
+
suffix = path[start_char..-1]
|
100
|
+
|
101
|
+
if uri.path == "" && suffix[0] != "/"
|
102
|
+
uri.path = "/#{suffix}"
|
103
|
+
else
|
104
|
+
uri.path += suffix
|
105
|
+
end
|
106
|
+
|
107
|
+
uri.query = query if uri.query.nil? && !query.empty?
|
108
|
+
|
109
|
+
uri.to_s
|
110
|
+
end
|
111
|
+
|
112
|
+
def status_code(redirect)
|
113
|
+
redirect["redirect_type"] == "temporary" ? 302 : 301
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class UnresolvedRedirect < GdsApi::BaseError; end
|
26
118
|
end
|
data/lib/gds_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gds-api-adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 51.
|
4
|
+
version: 51.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: plek
|