From 0d58de830ba98fa8bfbfd8b88491e3a4e2353c7b Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 26 Jul 2021 17:47:50 +0200 Subject: [PATCH] API: Add reading_time_minutes to /api/articles/me (#14318) --- app/controllers/api/v0/articles_controller.rb | 2 +- app/views/api/v0/articles/me.json.jbuilder | 1 + spec/requests/api/v0/articles_spec.rb | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v0/articles_controller.rb b/app/controllers/api/v0/articles_controller.rb index 358fe5c62..e787cd3f4 100644 --- a/app/controllers/api/v0/articles_controller.rb +++ b/app/controllers/api/v0/articles_controller.rb @@ -28,7 +28,7 @@ module Api id user_id organization_id title description main_image published published_at cached_tag_list slug path canonical_url comments_count public_reactions_count - page_views_count crossposted_at body_markdown updated_at + page_views_count crossposted_at body_markdown updated_at reading_time ].freeze private_constant :ME_ATTRIBUTES_FOR_SERIALIZATION diff --git a/app/views/api/v0/articles/me.json.jbuilder b/app/views/api/v0/articles/me.json.jbuilder index 60801c6fa..eb0a4c141 100644 --- a/app/views/api/v0/articles/me.json.jbuilder +++ b/app/views/api/v0/articles/me.json.jbuilder @@ -12,6 +12,7 @@ json.array! @articles do |article| json.cover_image cloud_cover_url(article.main_image) json.tag_list article.cached_tag_list_array json.canonical_url article.processed_canonical_url + json.reading_time_minutes article.reading_time json.partial! "api/v0/shared/user", user: article.user diff --git a/spec/requests/api/v0/articles_spec.rb b/spec/requests/api/v0/articles_spec.rb index 15ff320c1..7d3d37519 100644 --- a/spec/requests/api/v0/articles_spec.rb +++ b/spec/requests/api/v0/articles_spec.rb @@ -513,6 +513,22 @@ RSpec.describe "Api::V0::Articles", type: :request do expect(response.parsed_body.length).to eq(1) end + it "has correct keys in the response" do + article = create(:article, user: user) + article.update_columns(organization_id: organization.id) + + get me_api_articles_path, params: { access_token: access_token.token } + + keys = %w[ + type_of id title description published published_at slug path url + comments_count public_reactions_count page_views_count + published_timestamp body_markdown positive_reactions_count cover_image + tag_list canonical_url reading_time_minutes user organization flare_tag + ] + + expect(response.parsed_body.first.keys).to match_array(keys) + end + it "only includes published articles by default" do create(:article, published: false, published_at: nil, user: user) get me_api_articles_path, params: { access_token: access_token.token } @@ -549,6 +565,13 @@ RSpec.describe "Api::V0::Articles", type: :request do expected_order = response.parsed_body.map { |resp| resp["published"] } expect(expected_order).to eq([false, true]) end + + it "correctly returns reading time in minutes" do + create(:article, user: user) + + get me_api_articles_path, params: { access_token: access_token.token } + expect(response.parsed_body.first["reading_time_minutes"]).to eq(article.reading_time) + end end end