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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/labclient/client.rb', line 104

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



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

def api_methods
  subclasses.keys.sort
end

#base_urlObject



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

def base_url
  "#{settings[:url]}/api/v4/"
end

#fill_configurationObject

Load default profile



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

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

#help(help_filter = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/labclient/client.rb', line 86

def help(help_filter = nil)
  puts 'Available Methods'

  shown_subclasses = if help_filter
                       api_methods.grep(/#{help_filter}/)
                     else
                       api_methods
                     end

  puts " - #{shown_subclasses.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



141
142
143
# File 'lib/labclient/client.rb', line 141

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/labclient/client.rb', line 194

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



146
147
148
149
150
151
152
# File 'lib/labclient/client.rb', line 146

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

#prompt_for_tokenObject



130
131
132
133
# File 'lib/labclient/client.rb', line 130

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

#prompt_for_urlObject



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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/labclient/client.rb', line 177

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 unless settings[:quiet]
  resp
end

#setup_profileObject

Support for Named Profiles



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

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



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

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