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: fc390ba19e31be9e7e3457b2f4f3534e3da0023a41854b22b0db5fa11e364588
4
- data.tar.gz: 06c5faed53a2faf13ee59a5f0a304343204c890ece1ddfbdae69da7a8135bfe5
3
+ metadata.gz: dee0e8a1a5e3c9bf401cb8e96ee9e96b4bce2f4776ce2e37af9ac8ac767e0c1a
4
+ data.tar.gz: c9834f96d8c100cf903e524e41944fec14c3ef42ca02372b11fdbae31ad4194b
5
5
  SHA512:
6
- metadata.gz: 929d2167c3e4f3342f21f25923218ca718cdeb00679ac58c652a69baf69b6dbee815266191946f3fad3b52e94b2323de02d7b33d2a71de915d8b9b35a6eb2138
7
- data.tar.gz: e982309742921c2ecbafba1d6a7844d8e4f0371ea77ac2ec99cbf31dc202029013db71b0b454357ecfcb07dc4b031fe100fb3420f2d36d9dc39221ffa15b1183
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PublishingPlatformApi
4
- VERSION = "0.7.1"
4
+ VERSION = "0.8.0"
5
5
  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.7.1
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: 2025-02-13 00:00:00.000000000 Z
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.3
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: []