-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontent_item_presenter.rb
66 lines (58 loc) · 1.67 KB
/
content_item_presenter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Presenter for generating the public-facing representation of content items.
#
# Any linked content items that exist in the content store are expanded out to
# include their title, base_path, api_url and web_url. See docs/output_examples
# for an example of what this representation looks like.
class ContentItemPresenter
RESOLVER = ContentTypeResolver.new("text/html")
PUBLIC_ATTRIBUTES = %w[
analytics_identifier
base_path
content_id
document_type
first_published_at
locale
phase
public_updated_at
publishing_app
publishing_scheduled_at
rendering_app
scheduled_publishing_delay_seconds
schema_name
title
updated_at
withdrawn_notice
publishing_request_id
].freeze
DATETIME_ATTRIBUTES = %w[
updated_at
public_updated_at
first_published_at
publishing_scheduled_at
].freeze
def initialize(item, api_url_method)
@item = item
@api_url_method = api_url_method
end
def as_json(options = nil)
hash = item.as_json(options).slice(*PUBLIC_ATTRIBUTES).merge(
"links" => RESOLVER.resolve(links),
"description" => RESOLVER.resolve(item.description),
"details" => RESOLVER.resolve(item.details),
).tap { |i|
i["redirects"] = item["redirects"] if i["schema_name"] == "redirect"
render_timestamps_as_iso8601(item, i)
}.deep_stringify_keys
HashSorter.sort(hash)
end
private
attr_reader :item, :api_url_method
def links
ExpandedLinksPresenter.new(item.expanded_links).present
end
def render_timestamps_as_iso8601(item, hash)
DATETIME_ATTRIBUTES.each do |attr|
hash[attr] = item[attr].iso8601 if item[attr].respond_to?(:iso8601)
end
end
end