From c2f6d1a795319eea1697f96bfd78e6d03ce2780a Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 10:45:23 -0400 Subject: [PATCH 1/3] failing test for site.social and page.seo being arrays --- spec/jekyll_seo_tag/drop_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index f73fdc6..47a24bd 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -122,6 +122,14 @@ RSpec.describe Jekyll::SeoTag::Drop do end end + context "with site.social as an array" do + let(:config) { { "social" => ["a", "b"] } } + + it "uses site.social.name" do + expect(subject.name).to eql("social name") + end + end + it "uses the site title" do expect(subject.name).to eql("site title") end @@ -317,6 +325,14 @@ RSpec.describe Jekyll::SeoTag::Drop do end end + context "with seo as an array" do + let(:page_meta) { { "seo" => ["a", "b"] } } + + it "uses seo.type" do + expect(subject.type).to eql("test") + end + end + context "the homepage" do let(:page_meta) { { "permalink" => "/" } } From b3c2cec0a09da9974865136691951862be0c37ad Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 10:52:03 -0400 Subject: [PATCH 2/3] update test expectations --- spec/jekyll_seo_tag/drop_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index 47a24bd..2eaa915 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -123,10 +123,10 @@ RSpec.describe Jekyll::SeoTag::Drop do end context "with site.social as an array" do - let(:config) { { "social" => ["a", "b"] } } + let(:config) { { "social" => %w(a b) } } it "uses site.social.name" do - expect(subject.name).to eql("social name") + expect(subject.name).to be_nil end end @@ -326,10 +326,10 @@ RSpec.describe Jekyll::SeoTag::Drop do end context "with seo as an array" do - let(:page_meta) { { "seo" => ["a", "b"] } } + let(:page_meta) { { "seo" => %w(a b) } } it "uses seo.type" do - expect(subject.type).to eql("test") + expect(subject.type).to eql("WebPage") end end From ad25053f0b8eccc1bcb6df633453c56f88ce56d2 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 26 Apr 2017 10:52:12 -0400 Subject: [PATCH 3/3] ensure subhashes are hashes --- lib/jekyll-seo-tag/drop.rb | 44 ++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index 03702dd..d12b406 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -55,8 +55,8 @@ module Jekyll seo_name elsif !homepage_or_about? nil - elsif site["social"] && site["social"]["name"] - format_string site["social"]["name"] + elsif site_social["name"] + format_string site_social["name"] elsif site_title format_string site_title end @@ -95,8 +95,8 @@ module Jekyll def date_modified @date_modified ||= begin - date = if page["seo"] && page["seo"]["date_modified"] - page["seo"]["date_modified"] + date = if page_seo["date_modified"] + page_seo["date_modified"] elsif page["last_modified_at"] page["last_modified_at"].to_liquid else @@ -112,8 +112,8 @@ module Jekyll def type @type ||= begin - if page["seo"] && page["seo"]["type"] - page["seo"]["type"] + if page_seo["type"] + page_seo["type"] elsif homepage_or_about? "WebSite" elsif page["date"] @@ -126,10 +126,10 @@ module Jekyll def links @links ||= begin - if page["seo"] && page["seo"]["links"] - page["seo"]["links"] - elsif homepage_or_about? && site["social"] && site["social"]["links"] - site["social"]["links"] + if page_seo["links"] + page_seo["links"] + elsif homepage_or_about? && site_social["links"] + site_social["links"] end end end @@ -237,7 +237,29 @@ module Jekyll end def seo_name - @seo_name ||= format_string(page["seo"]["name"]) if page["seo"] + @seo_name ||= format_string(page_seo["name"]) if page_seo["name"] + end + + def page_seo + @page_seo ||= sub_hash(page, "seo") + end + + def site_social + @site_social ||= sub_hash(site, "social") + end + + # Safely returns a sub hash + # + # hash - the parent hash + # key - the key in the parent hash + # + # Returns the sub hash or an empty hash, if it does not exist + def sub_hash(hash, key) + if hash[key].is_a?(Hash) + hash[key] + else + {} + end end end end