From c681d2e2eeac08f9830deb31065dd0fadaaa9257 Mon Sep 17 00:00:00 2001 From: Edward Loveall Date: Sat, 4 Sep 2021 17:15:30 -0400 Subject: [PATCH] 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. --- spec/classes/page_converter_spec.cr | 64 +++++++++++++++++++++++++--- spec/components/page_content_spec.cr | 17 ++++++++ src/actions/articles/show.cr | 4 +- src/classes/page_converter.cr | 46 +++++++++++++++++--- src/models/page.cr | 2 + src/pages/articles/show_page.cr | 3 ++ 6 files changed, 122 insertions(+), 14 deletions(-) diff --git a/spec/classes/page_converter_spec.cr b/spec/classes/page_converter_spec.cr index fffb577..e067784 100644 --- a/spec/classes/page_converter_spec.cr +++ b/spec/classes/page_converter_spec.cr @@ -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 diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index 2d67575..cce44ef 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -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 diff --git a/src/actions/articles/show.cr b/src/actions/articles/show.cr index 4a8ba44..867a84a 100644 --- a/src/actions/articles/show.cr +++ b/src/actions/articles/show.cr @@ -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 diff --git a/src/classes/page_converter.cr b/src/classes/page_converter.cr index f3a8e1b..b8a3615 100644 --- a/src/classes/page_converter.cr +++ b/src/classes/page_converter.cr @@ -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 diff --git a/src/models/page.cr b/src/models/page.cr index a350981..a7bc9dd 100644 --- a/src/models/page.cr +++ b/src/models/page.cr @@ -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 diff --git a/src/pages/articles/show_page.cr b/src/pages/articles/show_page.cr index 7167702..8b50eb6 100644 --- a/src/pages/articles/show_page.cr +++ b/src/pages/articles/show_page.cr @@ -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