Skip to content

Commit

Permalink
Merge 6e1a820 into 4b47aac
Browse files Browse the repository at this point in the history
  • Loading branch information
hsitter committed Nov 14, 2016
2 parents 4b47aac + 6e1a820 commit be5f136
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
65 changes: 46 additions & 19 deletions lib/aptly/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
require 'rubygems/deprecate'

module Aptly
# Configuration.
class Configuration
extend Gem::Deprecate

# @!attribute uri
# @return [URI] the base URI for the API (http://localhost by default)
attr_accessor :uri

# Creates a new instance.
# @param uri see {#uri}
# @param host DEPRECATED use uri
# @param port DEPRECATED use uri
# @param path DEPRECATED use uri
def initialize(uri: URI::HTTP.build(host: 'localhost',
port: 80,
path: '/'),
host: nil, port: nil, path: nil)
@uri = uri unless host || port || path
return if @uri
@uri = fallback_uri(host, port, path)
end

# @!attribute host
# @deprecated use {#uri}
# @return [String] host name to talk to
attr_accessor :host

# @!attribute port
# @deprecated use {#uri}
# @return [Integer] port to talk to host to on
attr_accessor :port

# @!attribute path
# @deprecated use {#uri}
# @return [String] path to use (defaults to /)
attr_accessor :path

# Creates a new instance.
# @param host see {#host}
# @param port see {#port}
def initialize(host: 'localhost', port: 80, path: '/')
@host = host
@port = port
@path = path
# Fake deprecated attributes and redirect them to @uri
[:host, :port, :path].each do |uri_attr|
define_method(uri_attr.to_s) do
@uri.send(uri_attr)
end
deprecate uri_attr, :uri, 2017, 1
define_method("#{uri_attr}=") do |x|
# Ruby < 2.3 does not manage to handle string ports, so we need
# to manually convert to integer.
@uri.send("#{uri_attr}=", uri_attr == :port ? safe_port(x) : x)
end
deprecate "#{uri_attr}=".to_sym, :uri, 2017, 1
end

private

def safe_port(port)
port ? port.to_i : port
end

def uri
# FIXME: maybe we should simply configure a URI instead of configuring
# each part?
uri = URI.parse('')
uri.scheme = 'http'
uri.host = host
uri.port = port
uri.path = path
uri
def fallback_uri(host, port, path)
URI::HTTP.build(host: host || 'localhost', port: safe_port(port || 80),
path: path || '/')
end
end
end
20 changes: 19 additions & 1 deletion test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_init
refute_nil config.path
end

def test_init_options
def test_init_options # deprecated in favor of uri
config = ::Aptly::Configuration.new(host: 'otherhost',
port: 9055,
path: '/abc')
Expand All @@ -21,4 +21,22 @@ def test_init_options
assert_equal(config.path, '/abc')
assert_equal(config.uri.to_s, 'http://otherhost:9055/abc')
end

# partially providing deprecated params should construct a full uri
params = { host: 'otherhost', port: 9055, path: '/abc' }
params.each do |key, value|
define_method("test_fallback_compat_#{key}") do
config = ::Aptly::Configuration.new(key => value)
reference_params = { host: 'localhost', port: 80, path: '/' }
reference_params[key] = value
reference = URI::HTTP.build(reference_params)
assert_equal(reference, config.uri)
end
end

def test_init_uri
uri = URI.parse('https://example.com:123/xyz')
config = ::Aptly::Configuration.new(uri: uri)
assert_equal(uri, config.uri)
end
end

0 comments on commit be5f136

Please sign in to comment.