Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Repository#url_scheme for the pages URL scheme. #35

Merged
merged 3 commits into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/jekyll-github-metadata/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ def wiki_url
def show_downloads?
!!repo_info["has_downloads"]
end

def organization_repository?
memoize_value :@is_organization_repository, Value.new(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") })
end

def organization_public_members
memoize_value :@organization_public_members, Value.new(proc { |c|
c.organization_public_members(owner) if organization_repository?
})
end

def contributors
memoize_value :@contributors, Value.new(proc { |c| c.contributors(nwo) })
end

def releases
memoize_value :@releases, Value.new(proc { |c| c.releases(nwo) })
end
Expand All @@ -106,6 +106,16 @@ def primary?
end
end

def url_scheme
if domain.end_with?(".github.jparrowsec.cn".freeze)
"https".freeze
elsif cname
"http"
else
Pages.scheme
end
end

def default_user_domain
if github_repo?
"#{owner}.#{Pages.github_hostname}".downcase
Expand Down Expand Up @@ -141,14 +151,14 @@ def pages_url
URI.join("#{Pages.github_url}/pages/", path).to_s
end
elsif cname || primary?
"#{Pages.scheme}://#{domain}"
"#{url_scheme}://#{domain}"
else
URI.join("#{Pages.scheme}://#{domain}", name).to_s
URI.join("#{url_scheme}://#{domain}", name).to_s
end
end

def cname
memoize_value :@cname, Value.new(proc { |c|
memoize_value :@cname, Value.new(proc { |c|
if Pages.custom_domains_enabled?
(c.pages(nwo) || {'cname' => nil})['cname']
end
Expand All @@ -167,9 +177,9 @@ def domain
user_domain
end
end

private

def memoize_value(var_name, value)
return instance_variable_get(var_name) if instance_variable_defined?(var_name)
instance_variable_set(var_name, value.render)
Expand Down
56 changes: 56 additions & 0 deletions spec/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'

RSpec.describe(Jekyll::GitHubMetadata::Repository) do
let(:repo) { described_class.new(nwo) }

context "hubot.github.com" do
let(:nwo) { "github/hubot.github.com" }
before(:each) { allow(repo).to receive(:cname).and_return("hubot.github.com") }

it "returns the CNAME as its domain" do
expect(repo.domain).to eql("hubot.github.com")
end

it "always returns HTTP for the scheme" do
expect(repo.url_scheme).to eql("https")
end

it "forces HTTPS for the URL" do
expect(repo.pages_url).to eql("https://hubot.github.com")
end
end

context "ben.balter.com" do
let(:nwo) { "benbalter/benbalter.github.com" }
before(:each) { allow(repo).to receive(:cname).and_return("ben.balter.com") }

it "returns the CNAME as its domain" do
expect(repo.domain).to eql("ben.balter.com")
end

it "always returns HTTP for the scheme" do
expect(repo.url_scheme).to eql("http")
end

it "uses Pages.scheme to determine scheme for domain" do
expect(repo.pages_url).to eql("http://ben.balter.com")
end
end

context "parkr.github.io" do
let(:nwo) { "parkr/parkr.github.io" }
before(:each) { allow(repo).to receive(:cname).and_return(nil) }

it "returns the CNAME as its domain" do
expect(repo.domain).to eql("parkr.github.io")
end

it "returns Pages.scheme for the scheme" do
expect(repo.url_scheme).to eql(Jekyll::GitHubMetadata::Pages.scheme)
end

it "uses Pages.scheme to determine scheme for domain" do
expect(repo.pages_url).to eql("#{Jekyll::GitHubMetadata::Pages.scheme}://parkr.github.io")
end
end
end