Class: Artifactory::Client
- Inherits:
-
Object
- Object
- Artifactory::Client
- Includes:
- Configurable
- Defined in:
- lib/artifactory/client.rb
Overview
Client for the Artifactory API.
Class Method Summary collapse
Instance Method Summary collapse
-
#agent ⇒ HTTPClient
The actually HTTPClient agent.
-
#delete(path, *args, &block) ⇒ Object
Make a HTTP DELETE request.
-
#get(path, *args, &block) ⇒ Object
Make a HTTP GET request.
-
#head(path, *args, &block) ⇒ Object
Make a HTTP HEAD request.
-
#initialize(options = {}) ⇒ Artifactory::Client
constructor
Create a new Artifactory Client with the given options.
-
#parse_response(response) ⇒ String, Hash
Parse the response object and manipulate the result based on the given
Content-Type
header. -
#patch(path, *args, &block) ⇒ Object
Make a HTTP PATCH request.
-
#post(path, *args, &block) ⇒ Object
Make a HTTP POST request.
-
#put(path, *args, &block) ⇒ Object
Make a HTTP PUT request.
-
#request(verb, path, *args, &block) ⇒ Object
Make an HTTP reequest with the given verb and path.
-
#same_options?(opts) ⇒ Boolean
Determine if the given options are the same as ours.
Methods included from Configurable
Constructor Details
#initialize(options = {}) ⇒ Artifactory::Client
Create a new Artifactory Client with the given options. Any options given take precedence over the default options.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/artifactory/client.rb', line 46 def initialize( = {}) # Use any options given, but fall back to the defaults set on the module Artifactory::Configurable.keys.each do |key| value = if [key].nil? Artifactory.instance_variable_get(:"@#{key}") else [key] end instance_variable_set(:"@#{key}", value) end end |
Class Method Details
.proxy(klass) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/artifactory/client.rb', line 17 def proxy(klass) namespace = klass.name.split('::').last.downcase klass.singleton_methods(false).each do |name| define_method("#{namespace}_#{name}") do |*args| if args.last.is_a?(Hash) args.last[:client] = self else args << { client: self } end klass.send(name, *args) end end end |
Instance Method Details
#agent ⇒ HTTPClient
The actually HTTPClient agent.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/artifactory/client.rb', line 133 def agent @agent ||= begin agent = HTTPClient.new(endpoint) agent.agent_name = user_agent # Check if authentication was given if username && password agent.set_auth(endpoint, username, password) # https://github.com/nahi/httpclient/issues/63#issuecomment-2377919 agent.www_auth.basic_auth.challenge(endpoint) end # Check if proxy settings were given if proxy agent.proxy = proxy end agent end end |
#delete(path, *args, &block) ⇒ Object
Make a HTTP DELETE request
114 115 116 |
# File 'lib/artifactory/client.rb', line 114 def delete(path, *args, &block) request(:delete, path, *args, &block) end |
#get(path, *args, &block) ⇒ Object
Make a HTTP GET request
74 75 76 |
# File 'lib/artifactory/client.rb', line 74 def get(path, *args, &block) request(:get, path, *args, &block) end |
#head(path, *args, &block) ⇒ Object
Make a HTTP HEAD request
124 125 126 |
# File 'lib/artifactory/client.rb', line 124 def head(path, *args, &block) request(:head, path, *args, &block) end |
#parse_response(response) ⇒ String, Hash
Parse the response object and manipulate the result based on the given Content-Type
header. For now, this method only parses JSON, but it could be expanded in the future to accept other content types.
214 215 216 217 218 219 220 221 222 |
# File 'lib/artifactory/client.rb', line 214 def parse_response(response) content_type = response.headers['Content-Type'] if content_type && content_type.include?('json') JSON.parse(response.body) else response.body end end |
#patch(path, *args, &block) ⇒ Object
Make a HTTP PATCH request
104 105 106 |
# File 'lib/artifactory/client.rb', line 104 def patch(path, *args, &block) request(:patch, path, *args, &block) end |
#post(path, *args, &block) ⇒ Object
Make a HTTP POST request
84 85 86 |
# File 'lib/artifactory/client.rb', line 84 def post(path, *args, &block) request(:post, path, *args, &block) end |
#put(path, *args, &block) ⇒ Object
Make a HTTP PUT request
94 95 96 |
# File 'lib/artifactory/client.rb', line 94 def put(path, *args, &block) request(:put, path, *args, &block) end |
#request(verb, path, *args, &block) ⇒ Object
Make an HTTP reequest with the given verb and path.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/artifactory/client.rb', line 166 def request(verb, path, *args, &block) url = URI.parse(path) # Don't merge absolute URLs unless url.absolute? url = URI.parse(File.join(endpoint, path)).to_s end # Covert the URL back into a string url = url.to_s # Make the actual request response = agent.send(verb, url, *args, &block) case response.status.to_i when 200..399 parse_response(response) when 400 raise Error::BadRequest.new(url: url, body: response.body) when 401 raise Error::Unauthorized.new(url: url) when 403 raise Error::Forbidden.new(url: url) when 404 raise Error::NotFound.new(url: url) when 405 raise Error::MethodNotAllowed.new(url: url) else raise Error::ConnectionError.new(url: url, body: response.body) end rescue SocketError, Errno::ECONNREFUSED, EOFError raise Error::ConnectionError.new(url: url, body: <<-EOH.gsub(/^ {8}/, '')) The server is not currently accepting connections. EOH end |
#same_options?(opts) ⇒ Boolean
Determine if the given options are the same as ours.
64 65 66 |
# File 'lib/artifactory/client.rb', line 64 def (opts) opts.hash == .hash end |