diff --git a/spec/classes/page_converter_spec.cr b/spec/classes/page_converter_spec.cr index e067784..27bc613 100644 --- a/spec/classes/page_converter_spec.cr +++ b/spec/classes/page_converter_spec.cr @@ -80,6 +80,31 @@ describe PageConverter do page.author.should eq "Author" end + it "sets the publish date/time" do + data_json = <<-JSON + { + "post": { + "title": "This is a story", + "createdAt": 1000, + "creator": { + "id": "abc123", + "name": "Author" + }, + "content": { + "bodyModel": { + "paragraphs": [] + } + } + } + } + JSON + data = PostResponse::Data.from_json(data_json) + + page = PageConverter.new.convert(data) + + page.created_at.should eq Time.utc(1970, 1, 1, 0, 0, 1) + end + it "calls ParagraphConverter to convert the remaining paragraph content" do paragraph_json = <<-JSON [ diff --git a/spec/components/page_content_spec.cr b/spec/components/page_content_spec.cr index cce44ef..e5673aa 100644 --- a/spec/components/page_content_spec.cr +++ b/spec/components/page_content_spec.cr @@ -8,6 +8,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ Text.new(content: "hi"), @@ -25,6 +26,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ Text.new(content: "Hello, "), @@ -53,6 +55,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Anchor.new(children: [Text.new("link")] of Child, href: "https://example.com"), ] of Child @@ -68,6 +71,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ BlockQuote.new(children: [ Text.new("Wayne Gretzky. Michael Scott."), @@ -85,6 +89,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Code.new(children: [ Text.new("foo = bar"), @@ -102,6 +107,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ Text.new(content: "This is "), @@ -123,6 +129,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Figure.new(children: [ Image.new(src: "image.png", originalWidth: 100, originalHeight: 200), @@ -150,6 +157,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Heading2.new(children: [ Text.new(content: "Title!"), @@ -167,6 +175,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Heading3.new(children: [ Text.new(content: "In Conclusion..."), @@ -184,6 +193,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ Image.new(src: "image.png", originalWidth: 100, originalHeight: 200), @@ -205,6 +215,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ IFrame.new(href: "https://example.com"), @@ -228,6 +239,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ OrderedList.new(children: [ ListItem.new(children: [Text.new("One")] of Child), @@ -246,6 +258,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Paragraph.new(children: [ Text.new("Hello, world!"), @@ -263,6 +276,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Preformatted.new(children: [ Text.new("New\nline"), @@ -280,6 +294,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ Strong.new(children: [ Text.new("Oh yeah!"), @@ -297,6 +312,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ UnorderedList.new(children: [ ListItem.new(children: [Text.new("Apple")] of Child), @@ -315,6 +331,7 @@ describe PageContent do title: "Title", subtitle: nil, author: "Author", + created_at: Time.local, nodes: [ UserAnchor.new(children: [Text.new("Some User")] of Child, userId: "abc123"), ] of Child diff --git a/src/classes/page_converter.cr b/src/classes/page_converter.cr index b8a3615..43c01ba 100644 --- a/src/classes/page_converter.cr +++ b/src/classes/page_converter.cr @@ -20,6 +20,7 @@ class PageConverter def convert(data : PostResponse::Data) : Page paragraphs = data.post.content.bodyModel.paragraphs author = data.post.creator.name + created_at = Time.unix_ms(data.post.createdAt) header = header_data(paragraphs) if header.first_content_paragraph_index.zero? content = [] of PostResponse::Paragraph @@ -30,6 +31,7 @@ class PageConverter title: header.title, subtitle: header.subtitle, author: author, + created_at: Time.unix_ms(data.post.createdAt), nodes: ParagraphConverter.new.convert(content) ) end diff --git a/src/clients/medium_client.cr b/src/clients/medium_client.cr index d2b54ec..60fe41a 100644 --- a/src/clients/medium_client.cr +++ b/src/clients/medium_client.cr @@ -19,6 +19,7 @@ class MediumClient query { post(id: "#{post_id}") { title + createdAt creator { id name diff --git a/src/models/page.cr b/src/models/page.cr index a7bc9dd..5a9041e 100644 --- a/src/models/page.cr +++ b/src/models/page.cr @@ -3,11 +3,13 @@ class Page getter title : String getter subtitle : String? getter author : String + getter created_at : Time def initialize( @title : String, @subtitle : String?, @author : String, + @created_at : Time, @nodes : Nodes::Children = [] of Nodes::Child ) end diff --git a/src/models/post_response.cr b/src/models/post_response.cr index 51506cf..f1882a9 100644 --- a/src/models/post_response.cr +++ b/src/models/post_response.cr @@ -13,6 +13,7 @@ class PostResponse class Post < Base property title : String + property createdAt : Int64 property creator : Creator property content : Content end diff --git a/src/pages/articles/show_page.cr b/src/pages/articles/show_page.cr index 8b50eb6..466171a 100644 --- a/src/pages/articles/show_page.cr +++ b/src/pages/articles/show_page.cr @@ -11,7 +11,7 @@ class Articles::ShowPage < MainLayout para subtitle, class: "subtitle" end para do - text "#{page.author}" + text "#{page.author} #{created_at(page.created_at)}" end article do section do @@ -19,4 +19,8 @@ class Articles::ShowPage < MainLayout end end end + + def created_at(time : Time) : String + "at #{time.to_s("%I:%M%p")} on #{time.to_s("%F")}" + end end