Skip to content

Commit

Permalink
allow connection instances to override the global uri
Browse files Browse the repository at this point in the history
  • Loading branch information
hsitter committed Feb 22, 2017
1 parent d9e0530 commit 595a109
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/aptly/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class Connection

def initialize(**kwords)
@query = kwords.fetch(:query, DEFAULT_QUERY)
@base_uri = kwords.delete(:uri) { ::Aptly.configuration.uri.clone }

uri = ::Aptly.configuration.uri
@connection = Faraday.new(uri.to_s) do |c|
raise if uri.nil?
@connection = Faraday.new(uri) do |c|
c.request :multipart
c.request :url_encoded
c.adapter :net_http
Expand All @@ -45,6 +46,12 @@ def method_missing(symbol, *args, **kwords)

private

def uri
@uri ||= begin
uri = @base_uri.clone
end
end

def build_query(kwords)
query = @query.merge(kwords.delete(:query) { {} })
if kwords.delete(:query_mangle) { true }
Expand Down
6 changes: 6 additions & 0 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ def test_faraday_options_override
# Make sure connection options are reset to default at the end of the test
Faraday.default_connection_options = Faraday::ConnectionOptions.new
end

def test_uri_option
c = ::Aptly::Connection.new(uri: URI::HTTP.build(host: 'otherhost'))
stub_request(:get, 'http://otherhost/api').to_return(status: 200)
c.get
end
end

0 comments on commit 595a109

Please sign in to comment.