Add support for missing posts

Posts, like 8661f4724aa9, can go missing if the account or post was
removed. In this case, the API returns data like this:

```json
{
  "data": {
    "post": null
  }
}
```

When this happens, we can detect it because the parsed response now has
a nil value: `response.data.post == nil` and construct an `EmptyPage`
instead of a `Page`. The `Articles::Show` action can then render
conditionally based on if the response from `PageConverter` is a `Page`
or an `EmptyPage`.
This commit is contained in:
Edward Loveall
2022-06-17 13:26:35 -04:00
parent 1dcded9153
commit f05a12a880
11 changed files with 102 additions and 59 deletions

View File

@@ -17,8 +17,8 @@ describe PageConverter do
}
]
JSON
data_json = default_data_json(title, paragraph_json)
data = PostResponse::Data.from_json(data_json)
data_json = default_post_json(title, paragraph_json)
data = PostResponse::Post.from_json(data_json)
page = PageConverter.new.convert(data)
@@ -26,52 +26,48 @@ describe PageConverter do
end
it "sets the author" do
data_json = <<-JSON
post_json = <<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 0,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
"title": "This is a story",
"createdAt": 0,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
JSON
data = PostResponse::Data.from_json(data_json)
post = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(data)
page = PageConverter.new.convert(post)
page.author.name.should eq "Author"
page.author.id.should eq "abc123"
end
it "sets the publish date/time" do
data_json = <<-JSON
post_json = <<-JSON
{
"post": {
"title": "This is a story",
"createdAt": 1000,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
"title": "This is a story",
"createdAt": 1000,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": []
}
}
}
JSON
data = PostResponse::Data.from_json(data_json)
post = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(data)
page = PageConverter.new.convert(post)
page.created_at.should eq Time.utc(1970, 1, 1, 0, 0, 1)
end
@@ -98,8 +94,8 @@ describe PageConverter do
}
]
JSON
data_json = default_data_json(title, paragraph_json)
data = PostResponse::Data.from_json(data_json)
post_json = default_post_json(title, paragraph_json)
data = PostResponse::Post.from_json(post_json)
page = PageConverter.new.convert(data)
@@ -115,23 +111,21 @@ def default_paragraph_json
"[]"
end
def default_data_json(
def default_post_json(
title : String = "This is a story",
paragraph_json : String = default_paragraph_json
)
<<-JSON
{
"post": {
"title": "#{title}",
"createdAt": 1628974309758,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": #{paragraph_json}
}
"title": "#{title}",
"createdAt": 1628974309758,
"creator": {
"id": "abc123",
"name": "Author"
},
"content": {
"bodyModel": {
"paragraphs": #{paragraph_json}
}
}
}