From 2f1dad4e374bc32084107a0cb7e9d68ca4a44a8f Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Mon, 19 Apr 2021 17:11:55 -0500 Subject: [PATCH] Add reading_time to articles api responses (#13446) * Add reading_time to articles api responses Fixes #13436 * Update api docs to match changes to the articles response * Follow up docs updates - increment api docs version - add reading_time to articles/me responses - add reading time to the show response example format * Change reading_time attribute to reading_time_minutes Update the article builder to use `article.reading_time` and present it as `"reading_time_minutes"`. Update the api spec to expect the new property name instead of the attribute. * Update api documentation to reflect the new name - Change reading_time -> reading_time_minutes. - I decided to keep the (possibly redundant) description text here. --- app/controllers/api/v0/articles_controller.rb | 2 +- .../api/v0/articles/_article.json.jbuilder | 1 + docs/api_v0.yml | 19 ++++++++++++++++++- spec/requests/api/v0/articles_spec.rb | 4 ++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v0/articles_controller.rb b/app/controllers/api/v0/articles_controller.rb index 9450d652a..358fe5c62 100644 --- a/app/controllers/api/v0/articles_controller.rb +++ b/app/controllers/api/v0/articles_controller.rb @@ -16,7 +16,7 @@ module Api title description main_image published_at crossposted_at social_image cached_tag_list slug path canonical_url comments_count public_reactions_count created_at edited_at last_comment_at published - updated_at video_thumbnail_url + updated_at video_thumbnail_url reading_time ].freeze SHOW_ATTRIBUTES_FOR_SERIALIZATION = [ diff --git a/app/views/api/v0/articles/_article.json.jbuilder b/app/views/api/v0/articles/_article.json.jbuilder index 30a67282a..e04b0d3f4 100644 --- a/app/views/api/v0/articles/_article.json.jbuilder +++ b/app/views/api/v0/articles/_article.json.jbuilder @@ -24,3 +24,4 @@ json.edited_at utc_iso_timestamp(article.edited_at) json.crossposted_at utc_iso_timestamp(article.crossposted_at) json.published_at utc_iso_timestamp(article.published_at) json.last_comment_at utc_iso_timestamp(article.last_comment_at) +json.reading_time_minutes article.reading_time diff --git a/docs/api_v0.yml b/docs/api_v0.yml index 05c3e1001..15d7e77b2 100644 --- a/docs/api_v0.yml +++ b/docs/api_v0.yml @@ -17,7 +17,7 @@ info: Dates and date times, unless otherwise specified, must be in the [RFC 3339](https://tools.ietf.org/html/rfc3339) format. - version: "0.9.5" + version: "0.9.6" termsOfService: https://dev.to/terms contact: name: DEV Team @@ -186,6 +186,7 @@ components: - last_comment_at - published_timestamp - user + - reading_time_minutes properties: type_of: type: string @@ -254,6 +255,10 @@ components: format: date-time user: $ref: "#/components/schemas/SharedUser" + reading_time_minutes: + description: Reading time, in minutes + type: integer + format: int32 organization: $ref: "#/components/schemas/SharedOrganization" flare_tag: @@ -287,6 +292,7 @@ components: - body_html - body_markdown - user + - reading_time_minutes properties: type_of: type: string @@ -359,6 +365,10 @@ components: type: string user: $ref: "#/components/schemas/SharedUser" + reading_time_minutes: + description: Reading time, in minutes + type: integer + format: int32 organization: $ref: "#/components/schemas/SharedOrganization" flare_tag: @@ -516,6 +526,7 @@ components: - published_timestamp - body_markdown - user + - reading_time_minutes properties: type_of: type: string @@ -571,6 +582,10 @@ components: type: string user: $ref: "#/components/schemas/SharedUser" + reading_time_minutes: + description: Reading time, in minutes + type: integer + format: int32 organization: $ref: "#/components/schemas/SharedOrganization" flare_tag: @@ -1240,6 +1255,7 @@ components: published_at: '2019-10-24T13:52:17Z' last_comment_at: '2019-10-25T08:12:43Z' published_timestamp: '2019-10-24T13:52:17Z' + reading_time_minutes: 15 user: name: Ben Halpern username: ben @@ -1284,6 +1300,7 @@ components: published_at: "2019-08-01T15:47:54Z" last_comment_at: "2019-08-06T16:48:10Z" published_timestamp: "2019-08-01T15:47:54Z" + reading_time_minutes: 15 body_html: |+

Today's episode of Byte Sized is about Leonhard Euler and the creation of Graph Theory.

diff --git a/spec/requests/api/v0/articles_spec.rb b/spec/requests/api/v0/articles_spec.rb index 31bc62d91..15ff320c1 100644 --- a/spec/requests/api/v0/articles_spec.rb +++ b/spec/requests/api/v0/articles_spec.rb @@ -30,7 +30,7 @@ RSpec.describe "Api::V0::Articles", type: :request do type_of id title description cover_image readable_publish_date social_image tag_list tags slug path url canonical_url comments_count public_reactions_count positive_reactions_count collection_id created_at edited_at crossposted_at published_at last_comment_at - published_timestamp user organization flare_tag + published_timestamp user organization flare_tag reading_time_minutes ] expect(response.parsed_body.first.keys).to match_array index_keys @@ -334,7 +334,7 @@ RSpec.describe "Api::V0::Articles", type: :request do type_of id title description cover_image readable_publish_date social_image tag_list tags slug path url canonical_url comments_count public_reactions_count positive_reactions_count collection_id created_at edited_at crossposted_at published_at last_comment_at - published_timestamp body_html body_markdown user organization flare_tag + published_timestamp body_html body_markdown user organization flare_tag reading_time_minutes ] expect(response.parsed_body.keys).to match_array show_keys