Add author to post

Instead of passing Paragraphs to the PageConverter, it now receives all
the data from the response. This has the author so it can be parsed out.
This commit is contained in:
Edward Loveall 2021-09-04 17:15:30 -04:00
parent 083abc5ef1
commit c681d2e2ee
No known key found for this signature in database
GPG Key ID: 789A4AE983AC8901
6 changed files with 122 additions and 14 deletions

View File

@ -4,7 +4,7 @@ include Nodes
describe PageConverter do describe PageConverter do
it "sets the title and subtitle if present" do it "sets the title and subtitle if present" do
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON paragraph_json = <<-JSON
[ [
{ {
"text": "Title", "text": "Title",
@ -24,15 +24,17 @@ describe PageConverter do
} }
] ]
JSON JSON
data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(paragraphs) page = PageConverter.new.convert(data)
page.title.should eq "Title" page.title.should eq "Title"
page.subtitle.should eq "Subtitle" page.subtitle.should eq "Subtitle"
end end
it "sets the title to the first paragraph if no title" do it "sets the title to the first paragraph if no title" do
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON paragraph_json = <<-JSON
[ [
{ {
"text": "Not a title", "text": "Not a title",
@ -44,14 +46,42 @@ describe PageConverter do
} }
] ]
JSON JSON
page = PageConverter.new.convert(paragraphs) data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.title.should eq "Not a title" page.title.should eq "Not a title"
page.subtitle.should eq nil page.subtitle.should eq nil
end end
it "sets the author" do
data_json = <<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 0,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
}
JSON
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(data)
page.author.should eq "Author"
end
it "calls ParagraphConverter to convert the remaining paragraph content" do it "calls ParagraphConverter to convert the remaining paragraph content" do
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON paragraph_json = <<-JSON
[ [
{ {
"text": "Title", "text": "Title",
@ -79,8 +109,10 @@ describe PageConverter do
} }
] ]
JSON JSON
data_json = default_data_json(paragraph_json)
data = PostResponse::Data.from_json(data_json)
page = PageConverter.new.convert(paragraphs) page = PageConverter.new.convert(data)
page.nodes.should eq [ page.nodes.should eq [
Paragraph.new([ Paragraph.new([
@ -89,3 +121,23 @@ describe PageConverter do
] ]
end end
end end
def default_data_json(paragraph_json : String)
<<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 1628974309758,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": #{paragraph_json}
}
}
}
}
JSON
end

View File

@ -7,6 +7,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
Text.new(content: "hi"), Text.new(content: "hi"),
@ -23,6 +24,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
Text.new(content: "Hello, "), Text.new(content: "Hello, "),
@ -50,6 +52,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"), Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"),
] of Child ] of Child
@ -64,6 +67,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
BlockQuote.new(children: [ BlockQuote.new(children: [
Text.new("Wayne Gretzky. Michael Scott."), Text.new("Wayne Gretzky. Michael Scott."),
@ -80,6 +84,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Code.new(children: [ Code.new(children: [
Text.new("foo = bar"), Text.new("foo = bar"),
@ -96,6 +101,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
Text.new(content: "This is "), Text.new(content: "This is "),
@ -116,6 +122,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Figure.new(children: [ Figure.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200), Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
@ -142,6 +149,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Heading2.new(children: [ Heading2.new(children: [
Text.new(content: "Title!"), Text.new(content: "Title!"),
@ -158,6 +166,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Heading3.new(children: [ Heading3.new(children: [
Text.new(content: "In Conclusion..."), Text.new(content: "In Conclusion..."),
@ -174,6 +183,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200), Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
@ -194,6 +204,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
IFrame.new(href: "https://example.com"), IFrame.new(href: "https://example.com"),
@ -216,6 +227,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
OrderedList.new(children: [ OrderedList.new(children: [
ListItem.new(children: [Text.new("One")] of Child), ListItem.new(children: [Text.new("One")] of Child),
@ -233,6 +245,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Paragraph.new(children: [ Paragraph.new(children: [
Text.new("Hello, world!"), Text.new("Hello, world!"),
@ -249,6 +262,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Preformatted.new(children: [ Preformatted.new(children: [
Text.new("New\nline"), Text.new("New\nline"),
@ -265,6 +279,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
Strong.new(children: [ Strong.new(children: [
Text.new("Oh yeah!"), Text.new("Oh yeah!"),
@ -281,6 +296,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
UnorderedList.new(children: [ UnorderedList.new(children: [
ListItem.new(children: [Text.new("Apple")] of Child), ListItem.new(children: [Text.new("Apple")] of Child),
@ -298,6 +314,7 @@ describe PageContent do
page = Page.new( page = Page.new(
title: "Title", title: "Title",
subtitle: nil, subtitle: nil,
author: "Author",
nodes: [ nodes: [
UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"), UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"),
] of Child ] of Child

View File

@ -7,9 +7,7 @@ class Articles::Show < BrowserAction
else else
response = MediumClient.post_data(post_id) response = MediumClient.post_data(post_id)
end end
page = PageConverter.new.convert( page = PageConverter.new.convert(response.data)
response.data.post.content.bodyModel.paragraphs
)
html ShowPage, page: page html ShowPage, page: page
end end
end end

View File

@ -1,18 +1,54 @@
struct HeaderData
getter title : String
getter subtitle : String?
def initialize(@title : String, @subtitle : String?)
end
def first_content_paragraph_index : Int32
if title.blank?
0
elsif subtitle.nil? || subtitle.blank?
1
else
2
end
end
end
class PageConverter class PageConverter
def convert(paragraphs : Array(PostResponse::Paragraph)) : Page def convert(data : PostResponse::Data) : Page
paragraphs = data.post.content.bodyModel.paragraphs
author = data.post.creator.name
header = header_data(paragraphs)
if header.first_content_paragraph_index.zero?
content = [] of PostResponse::Paragraph
else
content = paragraphs[header.first_content_paragraph_index..]
end
Page.new(
title: header.title,
subtitle: header.subtitle,
author: author,
nodes: ParagraphConverter.new.convert(content)
)
end
def header_data(paragraphs : Array(PostResponse::Paragraph)) : HeaderData
if paragraphs.empty?
return HeaderData.new("", nil)
end
first_two_paragraphs = paragraphs.first(2) first_two_paragraphs = paragraphs.first(2)
first_two_types = first_two_paragraphs.map(&.type) first_two_types = first_two_paragraphs.map(&.type)
if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4] if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4]
Page.new( HeaderData.new(
title: first_two_paragraphs[0].text, title: first_two_paragraphs[0].text,
subtitle: first_two_paragraphs[1].text, subtitle: first_two_paragraphs[1].text,
nodes: ParagraphConverter.new.convert(paragraphs[2..]),
) )
else else
Page.new( HeaderData.new(
title: first_two_paragraphs[0].text, title: first_two_paragraphs[0].text,
subtitle: nil, subtitle: nil,
nodes: ParagraphConverter.new.convert(paragraphs[1..]),
) )
end end
end end

View File

@ -2,10 +2,12 @@ class Page
getter nodes : Nodes::Children getter nodes : Nodes::Children
getter title : String getter title : String
getter subtitle : String? getter subtitle : String?
getter author : String
def initialize( def initialize(
@title : String, @title : String,
@subtitle : String?, @subtitle : String?,
@author : String,
@nodes : Nodes::Children = [] of Nodes::Child @nodes : Nodes::Children = [] of Nodes::Child
) )
end end

View File

@ -10,6 +10,9 @@ class Articles::ShowPage < MainLayout
if subtitle = page.subtitle if subtitle = page.subtitle
para subtitle, class: "subtitle" para subtitle, class: "subtitle"
end end
para do
text "#{page.author}"
end
article do article do
section do section do
mount PageContent, page: page mount PageContent, page: page