Sha256: ecdc0f0a25a823b4103bcbe1ed3de73b52e2bbd171f27f972721325742bb943c

Contents?: true

Size: 1.37 KB

Versions: 8

Compression:

Stored size: 1.37 KB

Contents

require 'net/http'
require 'uri'

module GitCompound
  module Repository
    class RemoteFile
      # Git archive strategy
      #
      class GithubStrategy < RemoteFileStrategy
        GITHUB_URI     = 'https://raw.githubusercontent.com'
        GITHUB_PATTERN = 'git@github.com:|https:\/\/github.com'

        def initialize(source, ref, file)
          super
          @uri = github_uri
        end

        def contents
          raise FileUnreachableError unless reachable?
          raise FileNotFoundError unless exists?
          @response.body
        end

        def reachable?
          @source.match(/#{GITHUB_PATTERN}/) ? true : false
        end

        def exists?
          @response ||= http_response(@uri)
          @response.code == 200.to_s
        end

        private

        def github_uri
          github_location = @source.sub(/^#{GITHUB_PATTERN}/, '')
          github_location.gsub!(%r{^/|/$}, '')
          file_uri = "#{GITHUB_URI}/#{github_location}/#{@ref}/#{@file}"
          URI.parse(file_uri)
        end

        def http_response(uri)
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = true
          http.open_timeout = 4
          http.read_timeout = 4
          params = { 'User-Agent' => 'git_compound' }
          req = Net::HTTP::Get.new(uri.request_uri, params)
          http.request(req)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
git_compound-0.2.2 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.2.1 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.2.0 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.1.2 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.1.1 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.1.0 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.0.10 lib/git_compound/repository/remote_file/github_strategy.rb
git_compound-0.0.9 lib/git_compound/repository/remote_file/github_strategy.rb