Sha256: 2b80a40a3b7234b97f8292f6056430eff0b95830255f33983348597b55efebf5

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

class GithubUrl
  attr_accessor :url, :default_branch

  GITHUB_HOST = "github.com"

  class Invalid < StandardError
  end

  def initialize(url, default_branch = "master")
    @default_branch = default_branch
    @url = url

    validate_url
  end

  def organization
    url_path[0]
  end

  def repository
    url_path[1]
  end

  def branch
    default_branch? ? @default_branch : url_path[3]
  end

  def path
    default_branch? ? File.join(url_path[2..-1]) : File.join(url_path[4..-1].to_a)
  end

  private

  def default_branch?
    (["blob", "tree", "raw"] & url_path).empty?
  end

  def validate_url
    raise(Invalid, "Must contain #{GITHUB_HOST}") unless url.split("/").any? { |e| e.include?(GITHUB_HOST) }
    raise(Invalid, "Missing organization") if organization.nil?
    raise(Invalid, "Missing repository") if repository.nil?
    raise(Invalid, "Missing branch") if !default_branch? && url_path[3].nil?
  end

  def url_path
    url_arr = url.split("/")
    github_index = url_arr.index { |e| e.include?(GITHUB_HOST) }
    url_arr[github_index + 1..-1]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github_url-0.1.0 lib/github_url.rb