Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite connection class to be better #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/nexus_cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'forwardable'
require 'httpclient'
require 'addressable/uri'
require 'faraday'
require 'faraday_middleware'
require 'nexus_cli/errors'
require 'rexml/document'
require 'yaml'
Expand Down
13 changes: 7 additions & 6 deletions lib/nexus_cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def from_overrides(overrides)

# Creates a new instance of the Configuration object from the config file
#
#
# @return [NexusCli::Configuration]
def from_file
config = YAML.load_file(file_path)
Expand Down Expand Up @@ -57,22 +56,24 @@ def validate!(config)

attribute :repository,
type: String,
required: true,
coerce: lambda { |m|
m = m.is_a?(String) ? m.gsub(' ', '_').downcase : m
}

attribute :username,
type: String,
required: true
type: String

attribute :password,
type: String,
required: true
type: String

attribute :ssl_verify,
type: [ TrueClass, FalseClass ],
default: true

def initialize(options)
mass_assign(options)
self.repository = options[:repository]
self.class.validate!(self)
end
end
end
93 changes: 22 additions & 71 deletions lib/nexus_cli/connection.rb
Original file line number Diff line number Diff line change
@@ -1,81 +1,32 @@
module NexusCli
class Connection
attr_reader :nexus
attr_reader :configuration
attr_reader :ssl_verify
class Connection < Faraday::Connection

def initialize(configuration, ssl_verify)
@configuration = configuration
@ssl_verify = ssl_verify
@nexus = setup_nexus(configuration)
end
NEXUS_REST_ENDPOINT = "service/local".freeze

# Returns an HTTPClient instance with settings to connect
# to a Nexus server.
#
# @return [HTTPClient]
def setup_nexus(configuration)
client = HTTPClient.new
client.send_timeout = 6000
client.receive_timeout = 6000
# https://github.com/nahi/httpclient/issues/63
client.set_auth(nil, configuration['username'], configuration['password'])
client.www_auth.basic_auth.challenge(configuration['url'])
client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE unless ssl_verify

client
end

# Joins a given url to the current url stored in the configuraiton
# and returns the combined String.
#
# @param [String] url
# Creates a new instance of the Connection class
#
# @return [String]
def nexus_url(url)
File.join(configuration['url'], url)
end
# @param server_url [String] the nexus server url
# @param configuration [NexusCli::Configuration] the nexus configuration
def initialize(server_url, configuration = Configuration.from_file)
options = {}
options[:ssl] = {verify: configuration.ssl_verify}

# Gets that current status of the Nexus server. On a non-error
# status code, returns a Hash of values from the server.
#
# @return [Hash]
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)
options[:builder] = Faraday::Builder.new do |builder|
builder.request :json
builder.request :url_encoded
builder.request :basic_auth, configuration.username, configuration.password
builder.response :json
builder.adapter :net_http_persistent
end
end

# 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
def sanitize_for_id(unsanitized_string)
unsanitized_string.gsub(" ", "_").downcase
end
server_uri = Addressable::URI.parse(server_url).to_hash

server_uri[:path] = File.join(server_uri[:path], NEXUS_REST_ENDPOINT)

# Determines whether or not the Nexus server being
# connected to is running Nexus Pro.
def running_nexus_pro?
status['edition_long'] == "Professional"
super(Addressable::URI.new(server_uri), options)
@headers[:accept] = 'application/json'
@headers[:content_type] = 'application/json'
@headers[:user_agent] = "NexusCli v#{NexusCli::VERSION}"
end
end
end
end
7 changes: 2 additions & 5 deletions lib/nexus_cli/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module NexusCli
# @return [String]
def self.version
@version ||= File.read(File.expand_path("../../../VERSION", __FILE__)).strip
end
end
VERSION = "4.0.0"
end
8 changes: 6 additions & 2 deletions nexus_cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'nexus_cli/version'

Gem::Specification.new do |s|
s.name = "nexus_cli"
s.version = NexusCli.version
s.version = NexusCli::VERSION
s.authors = ["Kyle Allan"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/RiotGames/nexus_cli"
Expand All @@ -17,11 +17,15 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency 'thor'
s.add_dependency 'httpclient', '= 2.2.5'
s.add_dependency 'extlib'
s.add_dependency 'json'
s.add_dependency 'highline'
s.add_dependency 'jsonpath'
s.add_runtime_dependency 'faraday', '>= 0.8.4'
s.add_runtime_dependency 'faraday', '>= 0.8.4'
s.add_runtime_dependency 'faraday_middleware', '>= 0.9.0'
s.add_runtime_dependency 'net-http-persistent', '>= 2.8'
s.add_runtime_dependency 'addressable', '~> 2.3.4'
s.add_runtime_dependency 'chozo', '>= 0.6.0'
s.add_runtime_dependency 'activesupport', '>= 3.2.0'

Expand Down
21 changes: 21 additions & 0 deletions spec/unit/nexus_cli/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@
expect(config_instance.password).to eq("password")
end
end

describe "#ssl_verify" do
it "defaults to true" do
expect(config_instance.ssl_verify).to eq(true)
end

context "when ssl_verify is set to false" do
let(:valid_config) do
{
"url" => "http://somewebsite.com",
"repository" => "foo",
"username" => "admin",
"password" => "password",
"ssl_verify" => false
}
end
it "returns false" do
expect(config_instance.ssl_verify).to eq(false)
end
end
end
end
34 changes: 34 additions & 0 deletions spec/unit/nexus_cli/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe NexusCli::Connection do
let(:server_url) { "https://repository.apache.org" }
let(:configuration) { NexusCli::Configuration.new(config_options) }
let(:config_options) do
{
"url" => "http://somewebsite.com",
"repository" => "foo",
"username" => "admin",
"password" => "password"
}
end

describe "#new" do
let(:connection) { described_class.new(server_url, configuration) }

it "sets an accept header of application/json" do
expect(connection.headers).to include("Accept" => "application/json")
end

it "sets a content-type header of application/json" do
expect(connection.headers).to include("Content-Type" => "application/json")
end

it "sets a user-agent header of NexusCli" do
expect(connection.headers).to include("User-Agent")
end

it "sets ssl_verify" do
expect(connection.ssl).to include(:verify)
end
end
end