Sha256: cf9bf0425923db1068eaaff549955f4caa8036f22076a9fed3fdb51e856ee2b0

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true
require 'httparty'

module Circlemator
  class GithubRepo
    include HTTParty

    class InvalidPath < StandardError
      def initialize(path)
        super "Path #{path} is not valid for repo"
      end
    end

    class WrongRepo < StandardError
      def initialize(url, repo)
        super "URL #{url} does not belong to repo #{repo}"
      end
    end

    base_uri 'https://api.github.com/repos'

    def initialize(user:, repo:, github_auth_token:, **_opts)
      @user = user
      @repo = repo
      @auth_token = github_auth_token
    end

    def get(path, opts = {})
      self.class.get(fix_path(path), opts.merge(basic_auth: auth))
    end

    def put(path, opts = {})
      self.class.put(fix_path(path), opts.merge(basic_auth: auth))
    end

    def post(path, opts = {})
      self.class.post(fix_path(path), opts.merge(basic_auth: auth))
    end

    private

    def fix_path(path)
      case path
      when %r(\A#{self.class.base_uri}(/#{@user}/#{@repo}.*)\z)
        $1
      when %r(\A#{self.class.base_uri})
        raise WrongRepo.new(path, "#{@user}/#{@repo}")
      when %r(\A/.*)
        "/#{@user}/#{@repo}#{path}"
      else
        raise InvalidPath, path
      end
    end

    def auth
      { username: @auth_token, password: 'x-oauth-basic' }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
circlemator-0.6.0 lib/circlemator/github_repo.rb
circlemator-0.5.0 lib/circlemator/github_repo.rb
circlemator-0.4.2 lib/circlemator/github_repo.rb
circlemator-0.4.1 lib/circlemator/github_repo.rb
circlemator-0.4.0 lib/circlemator/github_repo.rb