Sha256: c47e8d08b69fc5bd44e636ed53fc911ed878d621f69bffde23051055bca9ecb7

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

class GithubUrl
  attr_accessor :url

  GITHUB_HOST = "github.com".freeze

  class Invalid < StandardError
  end

  def initialize(url:, default_branch: "master", host: GITHUB_HOST)
    @default_branch = default_branch
    @url = url
    @host = host

    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 #{@host}") unless url.split("/").any? { |e| e.include?(@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("/")
    host_index = url_arr.index { |e| e.include?(@host) }
    url_arr[host_index + 1..-1]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github_url-0.2.0 lib/github_url.rb