Class: GithubCLI::API

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/api.rb

Overview

The API class is the main entry point for creating GithubCLI APIs.

Class Method Summary collapse

Class Method Details

.configure(options = {}) ⇒ Object

Configures api options if provided in the configuration file


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/github_cli/api.rb', line 18

def self.configure(options={})
  api    = Github.new
  config = GithubCLI.config

  api.basic_auth  = set_basic_auth(config, options)
  api.ssl         = config['core.ssl']      if config['core.ssl']
  api.adapter     = config['core.adapter']  if config['core.adapter']
  api.oauth_token = config['user.token']    if config['user.token']
  api.site        = config['core.site']     if config['core.site']
  api.endpoint    = config['core.endpoint'] if config['core.endpoint']
  api.auto_pagination = options['auto_pagination'] || config['core.auto_pagination']

  if (test_env = ENV['TEST_HOST'])
    api.endpoint = 'http://' + test_env
  end
  api
end

.github_api(options = {}) ⇒ Object

Access or initialize Github API client


11
12
13
# File 'lib/github_cli/api.rb', line 11

def self.github_api(options={})
  @github_api ||= configure(options)
end

.output(options, &block) ⇒ Object

Procoess response and output to shell

Parameters:

  • options (Hash)

    the options to format the output

Options Hash (options):

  • :format (String)

    The format type

  • :quiet (String)

    The flag for reducing the output


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/github_cli/api.rb', line 58

def self.output(options, &block)
  config = GithubCLI.config
  format = options.fetch(:format) { config['core.format'] || :table }
  quiet  = options.fetch(:quiet)  { config['core.quiet'] }

  GithubCLI.on_error do
    response  = block.call
    if response.respond_to?(:body)
      formatter = Formatter.new response, :format => format, :quiet => quiet
      formatter.render_output
    else
      Terminal.line response
    end
    response
  end
end

.set_basic_auth(config, options) ⇒ Object

Set user basic authentication


39
40
41
42
43
44
45
46
47
# File 'lib/github_cli/api.rb', line 39

def self.set_basic_auth(config, options)
  if ( = options['login']) && (password = options['password'])
    "#{}:#{password}"
  elsif ( = config['user.login']) && (password = config['user.password'])
    "#{}:#{password}"
  else
    nil
  end
end