Class: LabClient::Client

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

Overview

API Specifics rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_settings = nil) ⇒ Client

Default setup, pull in settings



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/labclient/client.rb', line 96

def initialize( = nil)
  @settings = 
  setup_profile if &.key?(:profile) || ENV['LABCLIENT_PROFILE']
  @settings ||= fill_configuration

  # Set Unspecified Defaults
  unspecified_defaults

  prompt_for_url if @settings[:url].blank?

  # Only prompt if explicitly set to nil
  prompt_for_token if @settings[:token].nil?

  self.http = HTTP.new(@settings)
end

Instance Attribute Details

#httpObject

include HTTParty



7
8
9
# File 'lib/labclient/client.rb', line 7

def http
  @http
end

#klassObject

include HTTParty



7
8
9
# File 'lib/labclient/client.rb', line 7

def klass
  @klass
end

include HTTParty



7
8
9
# File 'lib/labclient/client.rb', line 7

def link
  @link
end

#respObject

include HTTParty



7
8
9
# File 'lib/labclient/client.rb', line 7

def resp
  @resp
end

#settingsObject

include HTTParty



7
8
9
# File 'lib/labclient/client.rb', line 7

def settings
  @settings
end

Instance Method Details

#api_methodsObject



81
82
83
# File 'lib/labclient/client.rb', line 81

def api_methods
  subclasses.keys.sort
end

#fill_configurationObject

Load default profile



153
154
155
156
157
158
159
160
161
162
# File 'lib/labclient/client.rb', line 153

def fill_configuration
  if File.exist? home_file
    Oj.load_file(home_file, { symbol_keys: true })
  else
    {
      url: ENV['LABCLIENT_URL'],
      token: ENV['LABCLIENT_TOKEN']
    }
  end
end

#helpObject



85
86
87
88
89
90
91
92
93
# File 'lib/labclient/client.rb', line 85

def help
  puts 'Available Methods'
  puts " - #{subclasses.keys.sort.join(' ')}\n\n"
  puts "See help for each specific sub-category\n"
  puts "- client.users.help\n"
  puts "- client.users.api_methods\n"

  nil
end

#home_fileObject



128
129
130
# File 'lib/labclient/client.rb', line 128

def home_file
  ENV['HOME'] + '/.gitlab-labclient'
end

#inspectObject



9
10
11
# File 'lib/labclient/client.rb', line 9

def inspect
  "#<LabClient::Client url: \"#{@settings[:url]}\">"
end

#process(resp) ⇒ Object

Assume we want LabStruct if @klass is ever nil



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/labclient/client.rb', line 181

def process(resp)
  case resp.data
  when LabStruct
    klass ? klass.new(resp.data, resp, self) : resp.data
  when Array
    if @klass.nil?
      resp.data
    else
      PaginatedResponse.new(@klass, resp, self)
    end
  else
    resp.data
  end
end

#profileObject

Easier Profile Name Access



133
134
135
136
137
138
139
# File 'lib/labclient/client.rb', line 133

def profile
  if settings&.key? :profile
    settings[:profile].to_sym
  else
    ENV['LABCLIENT_PROFILE'].to_sym
  end
end

#prompt_for_tokenObject



118
119
120
121
# File 'lib/labclient/client.rb', line 118

def prompt_for_token
  print 'Enter Personal Access Token: '
  @settings[:token] = STDIN.gets.chomp
end

#prompt_for_urlObject



112
113
114
115
116
# File 'lib/labclient/client.rb', line 112

def prompt_for_url
  print 'Enter GitLab URL (e.g. https://gitlab.com): '
  @settings[:url] = STDIN.gets.chomp
  raise 'LabClient Error - Missing URL!' if @settings[:url].blank?
end

#request(method, path, klass = nil, body = {}, dump_json = true) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/labclient/client.rb', line 164

def request(method, path, klass = nil, body = {}, dump_json = true)
  @klass = klass

  resp = http.request(method, path, body, dump_json)

  raise LabClient::Error.new(resp), resp.friendly_error unless resp.success?

  # Drop in raw path
  resp.instance_variable_set(:@path, path)

  process resp
rescue LabClient::Error => e
  puts e.message
  resp
end

#setup_profileObject

Support for Named Profiles



142
143
144
145
146
147
148
149
150
# File 'lib/labclient/client.rb', line 142

def setup_profile
  return false unless File.exist? home_file

  config = Oj.load_file(home_file, { symbol_keys: true })
  return false unless config.key? profile

  self.settings ||= {}
  settings.merge! config[profile]
end

#subclassesObject

Helper to make subclasses directly accessible



14
15
16
# File 'lib/labclient/client.rb', line 14

def subclasses
  self.class.instance_variable_get(:@subclasses)
end

#unspecified_defaultsObject



123
124
125
126
# File 'lib/labclient/client.rb', line 123

def unspecified_defaults
  @settings[:paginate] = true if @settings[:paginate].nil?
  @settings[:ssl_verify] = true if @settings[:ssl_verify].nil?
end