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:
parent
083abc5ef1
commit
c681d2e2ee
6 changed files with 122 additions and 14 deletions
|
@ -4,7 +4,7 @@ include Nodes
|
|||
|
||||
describe PageConverter do
|
||||
it "sets the title and subtitle if present" do
|
||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||
paragraph_json = <<-JSON
|
||||
[
|
||||
{
|
||||
"text": "Title",
|
||||
|
@ -24,15 +24,17 @@ describe PageConverter do
|
|||
}
|
||||
]
|
||||
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.subtitle.should eq "Subtitle"
|
||||
end
|
||||
|
||||
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",
|
||||
|
@ -44,14 +46,42 @@ describe PageConverter do
|
|||
}
|
||||
]
|
||||
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.subtitle.should eq nil
|
||||
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
|
||||
paragraphs = Array(PostResponse::Paragraph).from_json <<-JSON
|
||||
paragraph_json = <<-JSON
|
||||
[
|
||||
{
|
||||
"text": "Title",
|
||||
|
@ -79,8 +109,10 @@ describe PageConverter do
|
|||
}
|
||||
]
|
||||
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 [
|
||||
Paragraph.new([
|
||||
|
@ -89,3 +121,23 @@ describe PageConverter do
|
|||
]
|
||||
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
|
||||
|
|
|
@ -7,6 +7,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
Text.new(content: "hi"),
|
||||
|
@ -23,6 +24,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
Text.new(content: "Hello, "),
|
||||
|
@ -50,6 +52,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"),
|
||||
] of Child
|
||||
|
@ -64,6 +67,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
BlockQuote.new(children: [
|
||||
Text.new("Wayne Gretzky. Michael Scott."),
|
||||
|
@ -80,6 +84,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Code.new(children: [
|
||||
Text.new("foo = bar"),
|
||||
|
@ -96,6 +101,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
Text.new(content: "This is "),
|
||||
|
@ -116,6 +122,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Figure.new(children: [
|
||||
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
|
||||
|
@ -142,6 +149,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Heading2.new(children: [
|
||||
Text.new(content: "Title!"),
|
||||
|
@ -158,6 +166,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Heading3.new(children: [
|
||||
Text.new(content: "In Conclusion..."),
|
||||
|
@ -174,6 +183,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
Image.new(src: "image.png", originalWidth: 100, originalHeight: 200),
|
||||
|
@ -194,6 +204,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
IFrame.new(href: "https://example.com"),
|
||||
|
@ -216,6 +227,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
OrderedList.new(children: [
|
||||
ListItem.new(children: [Text.new("One")] of Child),
|
||||
|
@ -233,6 +245,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Paragraph.new(children: [
|
||||
Text.new("Hello, world!"),
|
||||
|
@ -249,6 +262,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Preformatted.new(children: [
|
||||
Text.new("New\nline"),
|
||||
|
@ -265,6 +279,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
Strong.new(children: [
|
||||
Text.new("Oh yeah!"),
|
||||
|
@ -281,6 +296,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
UnorderedList.new(children: [
|
||||
ListItem.new(children: [Text.new("Apple")] of Child),
|
||||
|
@ -298,6 +314,7 @@ describe PageContent do
|
|||
page = Page.new(
|
||||
title: "Title",
|
||||
subtitle: nil,
|
||||
author: "Author",
|
||||
nodes: [
|
||||
UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"),
|
||||
] of Child
|
||||
|
|
|
@ -7,9 +7,7 @@ class Articles::Show < BrowserAction
|
|||
else
|
||||
response = MediumClient.post_data(post_id)
|
||||
end
|
||||
page = PageConverter.new.convert(
|
||||
response.data.post.content.bodyModel.paragraphs
|
||||
)
|
||||
page = PageConverter.new.convert(response.data)
|
||||
html ShowPage, page: page
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
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_types = first_two_paragraphs.map(&.type)
|
||||
if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4]
|
||||
Page.new(
|
||||
HeaderData.new(
|
||||
title: first_two_paragraphs[0].text,
|
||||
subtitle: first_two_paragraphs[1].text,
|
||||
nodes: ParagraphConverter.new.convert(paragraphs[2..]),
|
||||
)
|
||||
else
|
||||
Page.new(
|
||||
HeaderData.new(
|
||||
title: first_two_paragraphs[0].text,
|
||||
subtitle: nil,
|
||||
nodes: ParagraphConverter.new.convert(paragraphs[1..]),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,10 +2,12 @@ class Page
|
|||
getter nodes : Nodes::Children
|
||||
getter title : String
|
||||
getter subtitle : String?
|
||||
getter author : String
|
||||
|
||||
def initialize(
|
||||
@title : String,
|
||||
@subtitle : String?,
|
||||
@author : String,
|
||||
@nodes : Nodes::Children = [] of Nodes::Child
|
||||
)
|
||||
end
|
||||
|
|
|
@ -10,6 +10,9 @@ class Articles::ShowPage < MainLayout
|
|||
if subtitle = page.subtitle
|
||||
para subtitle, class: "subtitle"
|
||||
end
|
||||
para do
|
||||
text "#{page.author}"
|
||||
end
|
||||
article do
|
||||
section do
|
||||
mount PageContent, page: page
|
||||
|
|
Loading…
Reference in a new issue