From 6ad02a583d82a6365393a70a10eb86bf03602edd Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 11:31:54 -0400 Subject: [PATCH 1/5] add failing test for site.authors as an array --- spec/jekyll_seo_tag/drop_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 0fab8847..49e683f4 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -231,6 +231,15 @@ site end + context "with site.authors as an array" do + let("data") { ["foo", "bar"] } + let(:page_meta) { {"author" => "foo"} } + + it "doesn't error" do + expect(subject.author).to eql("") + end + end + %i[with without].each do |site_data_type| context "#{site_data_type} site.author data" do let(:data) do From f78999819f04c89a5e9e3e0a54e94a76ce1fd892 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 11:39:52 -0400 Subject: [PATCH 2/5] ensure site.data.authors is in the expected format before resolving author meta --- lib/jekyll-seo-tag/drop.rb | 21 +++++++++++++-------- spec/jekyll_seo_tag/drop_spec.rb | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index c69f70d9..e5cd5f74 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -239,15 +239,20 @@ def author_string_or_hash end end + # Given a string representing the current document's author, return + # a normalized hash representing that author. Will try to pull from + # site.authors if present and in the proper format. def author_hash(author_string) - if site.data["authors"] && site.data["authors"][author_string] - hash = site.data["authors"][author_string] - hash["name"] ||= author_string - hash["twitter"] ||= author_string - hash - else - { "name" => author_string } - end + site_author_hash(author_string) || { "name" => author_string } + end + + def site_author_hash(author_string) + return unless site.data["authors"] && site.data["authors"].is_a?(Hash) + author_hash = site.data["authors"][author_string] + return unless author_hash.is_a?(Hash) + author_hash["name"] ||= author_string + author_hash["twitter"] ||= author_string + author_hash end def seo_name diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 49e683f4..a47ad67c 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -232,11 +232,11 @@ end context "with site.authors as an array" do - let("data") { ["foo", "bar"] } + let("data") { { "authors" => ["foo", "bar"] } } let(:page_meta) { {"author" => "foo"} } it "doesn't error" do - expect(subject.author).to eql("") + expect(subject.author).to eql({"name"=>"foo", "twitter"=>"foo"}) end end From a59b596a2bcf2501ea407fc4aba43c8a8dd7b5d0 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 11:41:02 -0400 Subject: [PATCH 3/5] comment site_author_hash --- lib/jekyll-seo-tag/drop.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index e5cd5f74..632c2d43 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -246,6 +246,10 @@ def author_hash(author_string) site_author_hash(author_string) || { "name" => author_string } end + # Given a string representing the current document's author, attempt + # to retrieve additional metadata from site.data.authors, if present + # + # Returns the author hash def site_author_hash(author_string) return unless site.data["authors"] && site.data["authors"].is_a?(Hash) author_hash = site.data["authors"][author_string] From 27392129c4aa99e01be9e56274b9f95bb66039a8 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 13:05:19 -0400 Subject: [PATCH 4/5] add explicit test for site.data.authors[author] as a string --- spec/jekyll_seo_tag/drop_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index a47ad67c..54ff9bd1 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -240,6 +240,15 @@ end end + context "with site.authors[author] as string" do + let("data") { { "authors" => { "foo" => "bar" } } } + let(:page_meta) { {"author" => "foo"} } + + it "doesn't error" do + expect(subject.author).to eql({"name"=>"foo", "twitter"=>"foo"}) + end + end + %i[with without].each do |site_data_type| context "#{site_data_type} site.author data" do let(:data) do From 7b3820f95639f78bdf6001c54c502819c1c27dcb Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 22 Aug 2017 13:05:49 -0400 Subject: [PATCH 5/5] correct rubocop offenses --- spec/jekyll_seo_tag/drop_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 7d9a3985..fe541619 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -232,20 +232,20 @@ end context "with site.authors as an array" do - let("data") { { "authors" => ["foo", "bar"] } } - let(:page_meta) { {"author" => "foo"} } + let("data") { { "authors" => %w(foo bar) } } + let(:page_meta) { { "author" => "foo" } } it "doesn't error" do - expect(subject.author).to eql({"name"=>"foo", "twitter"=>"foo"}) + expect(subject.author).to eql({ "name" => "foo", "twitter" => "foo" }) end end context "with site.authors[author] as string" do let("data") { { "authors" => { "foo" => "bar" } } } - let(:page_meta) { {"author" => "foo"} } + let(:page_meta) { { "author" => "foo" } } it "doesn't error" do - expect(subject.author).to eql({"name"=>"foo", "twitter"=>"foo"}) + expect(subject.author).to eql({ "name" => "foo", "twitter" => "foo" }) end end