-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters