Skip to content

Commit

Permalink
Allow setting a custom host
Browse files Browse the repository at this point in the history
Closes #2.
  • Loading branch information
sferik committed Jul 2, 2011
1 parent ab7ec9a commit c852933
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
4 changes: 4 additions & 0 deletions lib/gems/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Configuration
# An array of valid keys in the options hash when configuring a {Gems::Client}
VALID_OPTIONS_KEYS = [
:format,
:host,
:key,
:password,
:user_agent,
Expand All @@ -18,6 +19,8 @@ module Configuration
# @note JSON is preferred over XML because it is more concise and faster to parse.
DEFAULT_FORMAT = :json

DEFAULT_HOST = ENV['RUBYGEMS_HOST'] ? ENV['RUBYGEMS_HOST'] : 'https://rubygems.org'

# Set the default credentials
DEFAULT_KEY = Gem.configuration.rubygems_api_key

Expand Down Expand Up @@ -46,6 +49,7 @@ def options
# Reset all configuration options to defaults
def reset
self.format = DEFAULT_FORMAT
self.host = DEFAULT_HOST
self.key = DEFAULT_KEY
self.password = nil
self.user_agent = DEFAULT_USER_AGENT
Expand Down
2 changes: 1 addition & 1 deletion lib/gems/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def connection(format=format)
:user_agent => user_agent,
},
:ssl => {:verify => false},
:url => 'https://rubygems.org',
:url => host,
}

options[:headers].merge!({:authorization => key}) if key
Expand Down
16 changes: 6 additions & 10 deletions spec/gems/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

describe Gems::Client do
before do
Gems.configure do |config|
config.key = '701243f217cdf23b1370c7b66b65ca97'
config.username = '[email protected]'
config.password = 'schwwwwing'
end
end

after do
Gems.reset
end

Expand Down Expand Up @@ -149,13 +141,17 @@

describe ".api_key" do
before do
stub_get("/api/v1/api_key").
Gems.configure do |config|
config.username = '[email protected]'
config.password = 'schwwwwing'
end
stub_get("https://nick%40gemcutter.org:[email protected]/api/v1/api_key").
to_return(:body => fixture("api_key"))
end

it "should retrieve an API key" do
api_key = Gems.api_key
a_get("/api/v1/api_key").
a_get("https://nick%40gemcutter.org:[email protected]/api/v1/api_key").
should have_been_made
api_key.should == "701243f217cdf23b1370c7b66b65ca97"
end
Expand Down
10 changes: 2 additions & 8 deletions spec/gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
describe Gems do
context "when delegating to a client" do
before do
Gems.configure do |config|
config.username = '[email protected]'
config.password = 'schwwwwing'
end
stub_get("/api/v1/gems/rails.json").to_return(:body => fixture("rails.json"))
end

after do
stub_get("/api/v1/gems/rails.json").
to_return(:body => fixture("rails.json"))
Gems.reset
end

Expand Down
36 changes: 20 additions & 16 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,40 @@
require 'rspec'
require 'webmock/rspec'

def a_delete(path)
a_request(:delete, 'https://nick%40gemcutter.org:schwwwwing@rubygems.org' + path)
def rubygems_url(url)
url =~ /^http/ ? url : 'https://rubygems.org' + url
end

def a_get(path)
a_request(:get, 'https://nick%40gemcutter.org:[email protected]' + path)
def a_delete(url)
a_request(:delete, rubygems_url(url))
end

def a_post(path)
a_request(:post, 'https://nick%40gemcutter.org:[email protected]' + path)
def a_get(url)
a_request(:get, rubygems_url(url))
end

def a_put(path)
a_request(:put, 'https://nick%40gemcutter.org:[email protected]' + path)
def a_post(url)
a_request(:post, rubygems_url(url))
end

def stub_delete(path)
stub_request(:delete, 'https://nick%40gemcutter.org:[email protected]' + path)
def a_put(url)
a_request(:put, rubygems_url(url))
end

def stub_get(path)
stub_request(:get, 'https://nick%40gemcutter.org:[email protected]' + path)
def stub_delete(url)
stub_request(:delete, rubygems_url(url))
end

def stub_post(path)
stub_request(:post, 'https://nick%40gemcutter.org:[email protected]' + path)
def stub_get(url)
stub_request(:get, rubygems_url(url))
end

def stub_put(path)
stub_request(:put, 'https://nick%40gemcutter.org:[email protected]' + path)
def stub_post(url)
stub_request(:post, rubygems_url(url))
end

def stub_put(url)
stub_request(:put, rubygems_url(url))
end

def fixture_path
Expand Down

0 comments on commit c852933

Please sign in to comment.