Class: LabClient::Client

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

Overview

API Specifics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = nil) ⇒ Client

Default setup, pull in settings



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/labclient/client.rb', line 94

def initialize(settings = nil)
  @settings = settings
  @settings ||= fill_configuration

  # Set Unspecified Defaults
  unspecified_defaults

  raise 'LabClient Error - Missing URL!' if @settings[:url].nil?

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

Instance Attribute Details

#httpObject

include HTTParty



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

def http
  @http
end

#klassObject

include HTTParty



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

def klass
  @klass
end

include HTTParty



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

def link
  @link
end

#respObject

include HTTParty



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

def resp
  @resp
end

#settingsObject

include HTTParty



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

def settings
  @settings
end

Instance Method Details

#api_methodsObject



79
80
81
# File 'lib/labclient/client.rb', line 79

def api_methods
  subclasses.keys.sort
end

#fill_configurationObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/labclient/client.rb', line 115

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



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

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



111
112
113
# File 'lib/labclient/client.rb', line 111

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

#inspectObject



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

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

#process(resp) ⇒ Object

Assume we want OpenStruct if @klass is ever nil



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/labclient/client.rb', line 143

def process(resp)
  case resp.data
  when OpenStruct
    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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/labclient/client.rb', line 126

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

#subclassesObject

Helper to make subclasses directly accessible



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

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

#unspecified_defaultsObject



106
107
108
109
# File 'lib/labclient/client.rb', line 106

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