Skip to content

Commit

Permalink
Host and port configuration
Browse files Browse the repository at this point in the history
* Allow host to be configurable again
* Allow port to be configurable
* Logic to handle full uri from Config
* Use config#uri in http requests
  • Loading branch information
mtchavez committed Feb 27, 2016
1 parent 55b1e43 commit 8acfa38
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ CircleCi.configure do |config|
end
```

Optionally you can configure your own host and/or port if using an enterprise
CircleCi host. The port will default to `80` if not set.

```ruby
CircleCi.configure do |config|
config.token = 'my-token'
config.host = 'https://ci.mycompany.com'
config.port = 1234
end
```

## API Endpoints

* [User](#user)
Expand Down
25 changes: 21 additions & 4 deletions lib/circleci/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module CircleCi
# Config class used internally.
# Configure API calls using AlPapi.configure
class Config
VERSION = 'v1'.freeze
DEFAULT_HOST = "https://circleci.com/api/#{VERSION}".freeze
DEFAULT_VERSION = 'v1'.freeze
DEFAULT_HOST = 'https://circleci.com'.freeze
DEFAULT_URI = "#{DEFAULT_HOST}/api/#{DEFAULT_VERSION}".freeze
DEFAULT_PORT = 80

attr_accessor :token
attr_reader :host, :port
attr_accessor :token, :host, :port, :version

##
#
Expand All @@ -18,6 +18,23 @@ class Config
def initialize
@host = DEFAULT_HOST
@port = DEFAULT_PORT
@version = DEFAULT_VERSION
end

def uri
base = @host
base += ":#{@port}" unless port_80? || host_has_port?
base + "/api/#{@version}"
end

private

def port_80?
@port == DEFAULT_PORT
end

def host_has_port?
@host =~ /:\d{1,7}$/
end
end
end
2 changes: 1 addition & 1 deletion lib/circleci/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create_request_args(http_verb, url, body)
end

def request(http_verb, path, body = {})
url = "#{@config.host}#{path}"
url = "#{@config.uri}#{path}"
args = create_request_args http_verb, url, body

RestClient.send(*args) do |res, _, raw_res|
Expand Down
48 changes: 47 additions & 1 deletion spec/circleci/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,54 @@
config.host.should eql CircleCi::Config::DEFAULT_HOST
end

it 'sets default version' do
config.version.should eql CircleCi::Config::DEFAULT_VERSION
end

it 'sets default port' do
config.port.should eql 80
config.port.should eql CircleCi::Config::DEFAULT_PORT
end
end

describe 'uri' do
it 'includes port if not 80' do
config.port = 1234
config.uri.should match(/:1234/)
end

it 'does not include default port' do
config.uri.should_not match(/:80/)
host_and_port = CircleCi::Config::DEFAULT_HOST + ':80'
config.uri.should_not match(host_and_port)
end

it 'includes port if set on host' do
test_host = 'https://mycircle.com:1234'
config.host = test_host
config.uri.should match(test_host)
end
end

describe 'is port 80' do
it 'returns true if default port' do
config.should be_port_80
end

it 'returns false if not default port' do
config.port = 1234
config.should_not be_port_80
end
end

describe 'host has port' do
it 'returns true if host has port set' do
test_host = 'https://mycircle.com:1234'
config.host = test_host
config.should be_host_has_port
end

it 'returns false if host does not have port set' do
config.should_not be_host_has_port
end
end
end

0 comments on commit 8acfa38

Please sign in to comment.