Class: MkRepo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mk_repo/client.rb

Constant Summary collapse

ATTRIBUTES =
i(token repo_name user)
BASE_URLS =
{
  github:    "https://api.github.com/user/repos",
  gitlab:    "https://gitlab.com/api/v3/projects",
  bitbucket: "https://api.bitbucket.org/2.0/%s/%s"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, &blk) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
# File 'lib/mk_repo/client.rb', line 22

def initialize(provider, &blk)
  @provider = provider
  @uri = URI.parse(url % [user, repo_name])
  instance_eval(&blk)
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



20
21
22
# File 'lib/mk_repo/client.rb', line 20

def provider
  @provider
end

#uriObject (readonly)

Returns the value of attribute uri.



20
21
22
# File 'lib/mk_repo/client.rb', line 20

def uri
  @uri
end

Instance Method Details

#create!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mk_repo/client.rb', line 28

def create!
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    Net::HTTP::Post.new(uri.request_uri, headers).tap do |req|
      req.body = body
      req.basic_auth(user, token) if github?

      response = http.request(req)

      response_json = JSON.parse(response.body, symbolize_names: true)

      if response.kind_of?(Net::HTTPSuccess)
        $stdout.puts response_json[:clone_url]
      else
        error_message = response_json[:errors].first[:message]
        raise StandardError,
          "#{response.code} Unable to create repository: #{error_message}"
      end
    end
  end
end