publishing_platform_api_adapters 0.7.1 → 0.8.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dee0e8a1a5e3c9bf401cb8e96ee9e96b4bce2f4776ce2e37af9ac8ac767e0c1a
|
4
|
+
data.tar.gz: c9834f96d8c100cf903e524e41944fec14c3ef42ca02372b11fdbae31ad4194b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf30d7ce4c640a5c681b6128f902a257d9ffd5b46a4e4b81602fddb0385537a4aaf5f410856322f4f558e75928771cc2cc9db334794058b1cd8fe56c859ed396
|
7
|
+
data.tar.gz: 103d05a4628054a263f32644c74f95f7525ec60308c1c37dcfd0c51d4f4165bbf4710ad2f11b547d0bf16946ac85692f52b879df124e21491aa73fb4a75e9ff2
|
@@ -16,9 +16,96 @@ class PublishingPlatformApi::ContentStore < PublishingPlatformApi::Base
|
|
16
16
|
raise ItemNotFound.build_from(e)
|
17
17
|
end
|
18
18
|
|
19
|
+
# Returns an array tuple of destination url with status code e.g
|
20
|
+
# ["https://www.publishing-platform.co.uk/destination", 301]
|
21
|
+
def self.redirect_for_path(content_item, request_path, request_query = "")
|
22
|
+
RedirectResolver.call(content_item, request_path, request_query)
|
23
|
+
end
|
24
|
+
|
19
25
|
private
|
20
26
|
|
21
27
|
def content_item_url(base_path)
|
22
28
|
"#{endpoint}/content#{base_path}"
|
23
29
|
end
|
30
|
+
|
31
|
+
class RedirectResolver
|
32
|
+
def initialize(content_item, request_path, request_query = "")
|
33
|
+
@content_item = content_item
|
34
|
+
@request_path = request_path
|
35
|
+
@request_query = request_query.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.call(*args)
|
39
|
+
new(*args).call
|
40
|
+
end
|
41
|
+
|
42
|
+
def call
|
43
|
+
redirect = redirect_for_path(request_path)
|
44
|
+
raise UnresolvedRedirect, "Could not find a matching redirect" unless redirect
|
45
|
+
|
46
|
+
destination_uri = URI.parse(
|
47
|
+
resolve_destination(redirect, request_path, request_query),
|
48
|
+
)
|
49
|
+
|
50
|
+
url = if destination_uri.absolute?
|
51
|
+
destination_uri.to_s
|
52
|
+
else
|
53
|
+
"#{PublishingPlatformLocation.new.website_root}#{destination_uri}"
|
54
|
+
end
|
55
|
+
|
56
|
+
[url, 301]
|
57
|
+
end
|
58
|
+
|
59
|
+
private_class_method :new
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
attr_reader :content_item, :request_path, :request_query
|
64
|
+
|
65
|
+
def redirect_for_path(path)
|
66
|
+
redirects_by_segments.find do |r|
|
67
|
+
next true if r["path"] == path
|
68
|
+
|
69
|
+
route_prefix_match?(r["path"], path) if r["type"] == "prefix"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def redirects_by_segments
|
74
|
+
redirects = content_item["redirects"] || []
|
75
|
+
redirects.sort_by { |r| r["path"].split("/").count * -1 }
|
76
|
+
end
|
77
|
+
|
78
|
+
def route_prefix_match?(prefix_path, path_to_match)
|
79
|
+
prefix_regex = %r{^#{Regexp.escape(prefix_path)}/}
|
80
|
+
path_to_match.match prefix_regex
|
81
|
+
end
|
82
|
+
|
83
|
+
def resolve_destination(redirect, path, query)
|
84
|
+
return redirect["destination"] unless redirect["segments_mode"] == "preserve"
|
85
|
+
|
86
|
+
if redirect["type"] == "prefix"
|
87
|
+
prefix_destination(redirect, path, query)
|
88
|
+
else
|
89
|
+
redirect["destination"] + (query.empty? ? "" : "?#{query}")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def prefix_destination(redirect, path, query)
|
94
|
+
uri = URI.parse(redirect["destination"])
|
95
|
+
start_char = redirect["path"].length
|
96
|
+
suffix = path[start_char..]
|
97
|
+
|
98
|
+
if uri.path == "" && suffix[0] != "/"
|
99
|
+
uri.path = "/#{suffix}"
|
100
|
+
else
|
101
|
+
uri.path += suffix
|
102
|
+
end
|
103
|
+
|
104
|
+
uri.query = query if uri.query.nil? && !query.empty?
|
105
|
+
|
106
|
+
uri.to_s
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class UnresolvedRedirect < PublishingPlatformApi::BaseError; end
|
24
111
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: publishing_platform_api_adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Publishing Platform
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: addressable
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
- !ruby/object:Gem::Version
|
134
134
|
version: '0'
|
135
135
|
requirements: []
|
136
|
-
rubygems_version: 3.6.
|
136
|
+
rubygems_version: 3.6.7
|
137
137
|
specification_version: 4
|
138
138
|
summary: Adapters to work with Publishing Platform APIs
|
139
139
|
test_files: []
|