class NexusCli::Connection

Attributes

configuration[R]
nexus[R]
ssl_verify[R]

Public Class Methods

new(configuration, ssl_verify) click to toggle source
# File lib/nexus_cli/connection.rb, line 7
def initialize(configuration, ssl_verify)
  @configuration = configuration
  @ssl_verify = ssl_verify
  @nexus = setup_nexus(configuration)
end

Public Instance Methods

nexus_url(url) click to toggle source

Joins a given url to the current url stored in the configuraiton and returns the combined String.

@param [String] url

@return [String]

# File lib/nexus_cli/connection.rb, line 19
def nexus_url(url)
  File.join(configuration['url'], url)
end
running_nexus_pro?() click to toggle source

Determines whether or not the Nexus server being connected to is running Nexus Pro.

# File lib/nexus_cli/connection.rb, line 61
def running_nexus_pro?
  status['edition_long'] == "Professional"
end
sanitize_for_id(unsanitized_string) click to toggle source

Transforms a given [String] into a sanitized version by replacing spaces with underscores and downcasing.

@param unsanitized_string [String] the String to sanitize

@return [String] the sanitized String

# File lib/nexus_cli/connection.rb, line 55
def sanitize_for_id(unsanitized_string)
  unsanitized_string.gsub(" ", "_").downcase
end
status() click to toggle source

Gets that current status of the Nexus server. On a non-error status code, returns a Hash of values from the server.

@return [Hash]

# File lib/nexus_cli/connection.rb, line 27
def status
  response = nexus.get(nexus_url("service/local/status"))
  case response.status
  when 200
    doc = REXML::Document.new(response.content).elements["/status/data"]
    data = Hash.new
    data['app_name'] = doc.elements["appName"].text
    data['version'] = doc.elements["version"].text
    data['edition_long'] = doc.elements["editionLong"].text
    data['state'] = doc.elements["state"].text
    data['started_at'] = doc.elements["startedAt"].text
    data['base_url'] = doc.elements["baseUrl"].text
    return data
  when 401
    raise PermissionsException
  when 503
    raise CouldNotConnectToNexusException
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

Private Instance Methods

setup_nexus(configuration) click to toggle source

Returns an HTTPClient instance with settings to connect to a Nexus server.

@return [HTTPClient]

# File lib/nexus_cli/connection.rb, line 71
def setup_nexus(configuration)
  client = HTTPClient.new
  client.send_timeout = 6000
  client.receive_timeout = 6000
  if configuration['username'] and configuration['password']
    # https://github.com/nahi/httpclient/issues/63
    client.set_auth(nil, configuration['username'], configuration['password'])
    client.www_auth.basic_auth.challenge(configuration['url'])
  end
  client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless ssl_verify
  client
end