-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathblock_content.rb
More file actions
66 lines (53 loc) · 1.72 KB
/
Copy pathblock_content.rb
File metadata and controls
66 lines (53 loc) · 1.72 KB
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
module PublishingApi
module PayloadBuilder
class BlockContent
include GovspeakHelper
def self.for(item)
new(item).call
end
def initialize(item)
@item = item
end
def call
mapping = @item.type_instance.presenter("publishing_api")["details"]
return {} unless mapping
mapping.each_with_object({}) { |(attribute, builder), details|
details[attribute.to_sym] = send(builder, attribute)
}.compact
end
private
attr_reader :item
def raw(attribute)
item.block_content&.public_send(attribute)
end
def govspeak(attribute)
content = item.block_content&.public_send(attribute)
return nil if content.nil?
govspeak_to_html(content, images: item.images, attachments: item.attachments)
end
def rfc3339_date(attribute)
item.block_content&.public_send(attribute)&.rfc3339
end
def social_media_links(attribute)
content = item.block_content&.public_send(attribute)
return [] if content.blank?
content.map do |item|
# `item` looks something like `{"url"=>"foo", "social_media_service_name"=>"Facebook", "title"=> "Optional title"}`
service_name = item["social_media_service_name"]
service_url = item["url"]
title = item["title"]
{
title: title.presence || service_name,
service_type: service_name.parameterize, # "Google Plus" => "google-plus"
href: service_url,
}
end
end
def parts(_attribute)
item.type_instance.parts.map do |part|
PayloadBuilder::MultipleParts.for(item, part)
end
end
end
end
end