From b90efc52b11e9b8037095ef2c4962bf39ab41361 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 11 Feb 2019 13:10:04 -0800 Subject: [PATCH 1/3] Add failing test for site.owner hash --- spec/integration_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index e7b014e..e968c02 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -39,4 +39,8 @@ expect(stub).to have_been_requested end end + + it "presents the owner data as a Hash" do + expect(subject["owner"]).to be_a(Hash) + end end From 37f9a0677159fe3c883c188479749e8eb833821a Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 11 Feb 2019 13:49:58 -0800 Subject: [PATCH 2/3] Render site.owner as a Hash so it can be encoded in JSON properly --- lib/jekyll-github-metadata/owner.rb | 16 ++++++++-------- lib/jekyll-github-metadata/repository.rb | 16 ++++++++-------- lib/jekyll-github-metadata/sanitizer.rb | 2 ++ spec/spec_helpers/integration_helper.rb | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/jekyll-github-metadata/owner.rb b/lib/jekyll-github-metadata/owner.rb index 29cda26..e9d4e40 100644 --- a/lib/jekyll-github-metadata/owner.rb +++ b/lib/jekyll-github-metadata/owner.rb @@ -62,21 +62,21 @@ def initialize(owner_login) @owner_login = owner_login end - def to_s - require "json" - JSON.pretty_generate to_h - end - alias_method :to_str, :to_s - def to_h - self.class.content_methods.each_with_object({}) { |method, hash| hash[method] = public_send(method) } + @to_h ||= self.class.content_methods + .each_with_object({}) { |method, hash| hash[method] = public_send(method) } end + alias_method :to_hash, :to_h + def_delegator :to_h, :to_json, :to_json + + def_delegator :to_json, :to_s, :to_s + alias_method :to_str, :to_s private def owner_info @owner_info ||= begin - Value.new( + Value.new("owner", proc do |c| (c.organization(owner_login) || c.user(owner_login) || {}).to_h end diff --git a/lib/jekyll-github-metadata/repository.rb b/lib/jekyll-github-metadata/repository.rb index 76861bc..d303696 100644 --- a/lib/jekyll-github-metadata/repository.rb +++ b/lib/jekyll-github-metadata/repository.rb @@ -46,12 +46,12 @@ def repo_compat def repo_info @repo_info ||= begin options = { :accept => "application/vnd.github.drax-preview+json" } - (Value.new(proc { |c| c.repository(nwo, options) }).render || {}) + (Value.new("repo_info", proc { |c| c.repository(nwo, options) }).render || {}) end end def repo_pages_info - @repo_pages_info ||= (Value.new(proc { |c| c.pages(nwo, repo_pages_info_opts) }).render || {}) + @repo_pages_info ||= (Value.new("repo_pages_info", proc { |c| c.pages(nwo, repo_pages_info_opts) }).render || {}) end def repo_pages_info_opts @@ -103,29 +103,29 @@ def wiki_url end def organization_repository? - memoize_value :@is_organization_repository, Value.new(proc { |c| !!c.organization(owner) }) + memoize_value :@is_organization_repository, Value.new("organization_repository?", proc { |c| !!c.organization(owner) }) end def owner_public_repositories - memoize_value :@owner_public_repositories, Value.new(proc { |c| c.list_repos(owner, "type" => "public") }) + memoize_value :@owner_public_repositories, Value.new("owner_public_repositories", proc { |c| c.list_repos(owner, "type" => "public") }) end def organization_public_members - memoize_value :@organization_public_members, Value.new(proc do |c| + memoize_value :@organization_public_members, Value.new("organization_public_members", proc do |c| c.organization_public_members(owner) if organization_repository? end) end def contributors - memoize_value :@contributors, Value.new(proc { |c| c.contributors(nwo) }) + memoize_value :@contributors, Value.new("contributors", proc { |c| c.contributors(nwo) }) end def releases - memoize_value :@releases, Value.new(proc { |c| c.releases(nwo) }) + memoize_value :@releases, Value.new("releases", proc { |c| c.releases(nwo) }) end def latest_release - memoize_value :@latest_release, Value.new(proc { |c| c.latest_release(nwo) }) + memoize_value :@latest_release, Value.new("latest_release", proc { |c| c.latest_release(nwo) }) end def source diff --git a/lib/jekyll-github-metadata/sanitizer.rb b/lib/jekyll-github-metadata/sanitizer.rb index 4e6d3ca..edaf554 100644 --- a/lib/jekyll-github-metadata/sanitizer.rb +++ b/lib/jekyll-github-metadata/sanitizer.rb @@ -29,6 +29,8 @@ def sanitize(resource) true when NilClass nil + when String + resource else if resource.respond_to?(:to_hash) sanitize_resource(resource) diff --git a/spec/spec_helpers/integration_helper.rb b/spec/spec_helpers/integration_helper.rb index 0147c06..fe9be0e 100644 --- a/spec/spec_helpers/integration_helper.rb +++ b/spec/spec_helpers/integration_helper.rb @@ -15,7 +15,7 @@ def expected_values "build_revision" => %r![a-f0-9]{40}!, "project_title" => "github-metadata", "project_tagline" => ":octocat: `site.github`", - "owner" => Regexp.new('"html_url": "https://github.com/jekyll",\s+"id": 3083652'), + "owner" => Regexp.new('"html_url"=>"https://github.com/jekyll",\s+"id"=>3083652'), "owner_name" => "jekyll", "owner_url" => "https://github.com/jekyll", "owner_gravatar_url" => "https://github.com/jekyll.png", From b64a6d3fbbce08127976972e16be4b9e39c6cee4 Mon Sep 17 00:00:00 2001 From: Parker Moore Date: Mon, 11 Feb 2019 13:57:25 -0800 Subject: [PATCH 3/3] Clarify Owner#to_s and #to_h --- lib/jekyll-github-metadata/owner.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/jekyll-github-metadata/owner.rb b/lib/jekyll-github-metadata/owner.rb index e9d4e40..7e574a2 100644 --- a/lib/jekyll-github-metadata/owner.rb +++ b/lib/jekyll-github-metadata/owner.rb @@ -64,19 +64,20 @@ def initialize(owner_login) def to_h @to_h ||= self.class.content_methods - .each_with_object({}) { |method, hash| hash[method] = public_send(method) } + .each_with_object({}) { |method, hash| hash[method.to_s] = public_send(method) } end alias_method :to_hash, :to_h def_delegator :to_h, :to_json, :to_json - def_delegator :to_json, :to_s, :to_s + def_delegator :to_h, :to_s, :to_s alias_method :to_str, :to_s private def owner_info @owner_info ||= begin - Value.new("owner", + Value.new( + "owner", proc do |c| (c.organization(owner_login) || c.user(owner_login) || {}).to_h end