Sha256: aec302a276fd2a1cda2e723692f7d53071631ec493d2b73929634680b9e97a6c

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'schmersion/host'

module Schmersion
  module Hosts
    class GitHub < Host

      SSH_REGEXP = /\Agit@github\.com:([\w-]+)\/([\w-]+)(\.git)?\z/.freeze
      HTTP_REGEXP = /\Ahttps:\/\/github\.com\/([\w-]+)\/([\w-]+)(\.git)?\z/.freeze

      class << self

        def suitable?(url)
          !!(url.match(SSH_REGEXP) || url.match(HTTP_REGEXP))
        end

      end

      def initialize(url)
        super
        get_user_and_repo(url)
        @base_url = "https://github.com/#{@user}/#{@repo}"
      end

      def url_for_commit(ref)
        "#{@base_url}/commit/#{ref}"
      end

      def url_for_comparison(ref1, ref2)
        "#{@base_url}/compare/#{ref1}..#{ref2}"
      end

      private

      def get_user_and_repo(url)
        if m = url.match(SSH_REGEXP)
          @user = m[1]
          @repo = m[2]
        elsif m = url.match(HTTP_REGEXP)
          @user = m[1]
          @repo = m[2]
        else
          raise Error, 'Could not determine appropriate details for repository from URL'
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
schmersion-1.1.3 lib/schmersion/hosts/github.rb
schmersion-1.1.2 lib/schmersion/hosts/github.rb
schmersion-1.1.0 lib/schmersion/hosts/github.rb
schmersion-1.0.1 lib/schmersion/hosts/github.rb
schmersion-1.0.0 lib/schmersion/hosts/github.rb