Class: LabClient::Client
- Inherits:
-
Object
- Object
- LabClient::Client
- Defined in:
- lib/labclient/client.rb
Overview
API Specifics rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#http ⇒ Object
include HTTParty.
-
#klass ⇒ Object
include HTTParty.
-
#link ⇒ Object
include HTTParty.
-
#resp ⇒ Object
include HTTParty.
-
#settings ⇒ Object
include HTTParty.
Instance Method Summary collapse
- #api_methods ⇒ Object
-
#fill_configuration ⇒ Object
Load default profile.
- #help ⇒ Object
- #home_file ⇒ Object
-
#initialize(user_settings = nil) ⇒ Client
constructor
Default setup, pull in settings.
- #inspect ⇒ Object
-
#process(resp) ⇒ Object
Assume we want LabStruct if @klass is ever nil.
-
#profile ⇒ Object
Easier Profile Name Access.
- #prompt_for_token ⇒ Object
- #prompt_for_url ⇒ Object
- #request(method, path, klass = nil, body = {}, dump_json = true) ⇒ Object
-
#setup_profile ⇒ Object
Support for Named Profiles.
-
#subclasses ⇒ Object
Helper to make subclasses directly accessible.
- #unspecified_defaults ⇒ Object
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(user_settings = nil) @settings = user_settings setup_profile if user_settings&.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
#http ⇒ Object
include HTTParty
7 8 9 |
# File 'lib/labclient/client.rb', line 7 def http @http end |
#klass ⇒ Object
include HTTParty
7 8 9 |
# File 'lib/labclient/client.rb', line 7 def klass @klass end |
#link ⇒ Object
include HTTParty
7 8 9 |
# File 'lib/labclient/client.rb', line 7 def link @link end |
#resp ⇒ Object
include HTTParty
7 8 9 |
# File 'lib/labclient/client.rb', line 7 def resp @resp end |
#settings ⇒ Object
include HTTParty
7 8 9 |
# File 'lib/labclient/client.rb', line 7 def settings @settings end |
Instance Method Details
#api_methods ⇒ Object
81 82 83 |
# File 'lib/labclient/client.rb', line 81 def api_methods subclasses.keys.sort end |
#fill_configuration ⇒ Object
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 |
#help ⇒ Object
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_file ⇒ Object
128 129 130 |
# File 'lib/labclient/client.rb', line 128 def home_file ENV['HOME'] + '/.gitlab-labclient' end |
#inspect ⇒ Object
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 |
#profile ⇒ Object
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_token ⇒ Object
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_url ⇒ Object
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. resp end |
#setup_profile ⇒ Object
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 |
#subclasses ⇒ Object
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_defaults ⇒ Object
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 |