Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from walski/add_key_option_to_yank

Makes yank understand the --key paramter to use different API keys
  • Loading branch information...
commit c1c57325d545695c3b058474069964338afaeb72 2 parents 2fa9adb + cfb4217
@sferik sferik authored
View
25 lib/rubygems/commands/yank_command.rb
@@ -16,7 +16,7 @@ def arguments
end
def usage
- "#{program_name} GEM -v VERSION [-p PLATFORM] [--undo]"
+ "#{program_name} GEM -v VERSION [-p PLATFORM] [--undo] [--key KEY_NAME]"
end
def initialize
@@ -26,18 +26,25 @@ def initialize
add_option('--undo') do |value, options|
options[:undo] = true
end
+
+ add_option('-k', '--key KEY_NAME',
+ 'Use API key from your gem credentials file') do |value, options|
+ options[:key] = value
+ end
end
def execute
sign_in
version = get_version_from_requirements(options[:version])
platform = get_platform_from_requirements(options)
+ api_key = Gem.configuration.rubygems_api_key
+ api_key = Gem.configuration.api_keys[options[:key].to_sym] if options[:key]
if !version.nil?
if options[:undo]
- unyank_gem(version, platform)
+ unyank_gem(version, platform, api_key)
else
- yank_gem(version, platform)
+ yank_gem(version, platform, api_key)
end
else
say "A version argument is required: #{usage}"
@@ -45,21 +52,21 @@ def execute
end
end
- def yank_gem(version, platform)
+ def yank_gem(version, platform, api_key)
say "Yanking gem from RubyGems.org..."
- yank_api_request(:delete, version, platform, "api/v1/gems/yank")
+ yank_api_request(:delete, version, platform, "api/v1/gems/yank", api_key)
end
- def unyank_gem(version, platform)
+ def unyank_gem(version, platform, api_key)
say "Unyanking gem from RubyGems.org..."
- yank_api_request(:put, version, platform, "api/v1/gems/unyank")
+ yank_api_request(:put, version, platform, "api/v1/gems/unyank", api_key)
end
private
- def yank_api_request(method, version, platform, api)
+ def yank_api_request(method, version, platform, api, api_key)
name = get_one_gem_name
response = rubygems_api_request(method, api) do |request|
- request.add_field("Authorization", Gem.configuration.rubygems_api_key)
+ request.add_field("Authorization", api_key)
request.set_form_data({'gem_name' => name, 'version' => version, 'platform' => platform})
end
say response.body
View
1  test/helper.rb
@@ -24,6 +24,7 @@ def teardown
def stub_api_key(api_key)
file = Gem::ConfigFile.new({})
stub(file).rubygems_api_key { api_key }
+ yield file if block_given?
stub(Gem).configuration { file }
end
View
38 test/yank_command_test.rb
@@ -20,7 +20,8 @@ class YankCommandTest < CommandTest
context 'yanking a gem' do
setup do
- stub_api_key("key")
+ @api_key = "key"
+ stub_api_key(@api_key)
@api = "https://rubygems.org/api/v1/gems/yank"
stub_request(:delete, @api).to_return(:body => "Successfully yanked")
@command.handle_options([@gem, "-v", @version])
@@ -35,10 +36,10 @@ class YankCommandTest < CommandTest
end
should 'invoke yank_gem' do
- stub(@command).yank_gem(@version, @platform)
+ stub(@command).yank_gem(@version, @platform, @api_key)
@command.execute
assert_received(@command) do |command|
- command.yank_gem(@version, @platform)
+ command.yank_gem(@version, @platform, @api_key)
end
end
@@ -50,6 +51,19 @@ class YankCommandTest < CommandTest
:headers => { 'Authorization' => 'key' })
end
+ should 'use given API key' do
+ other_key = 'another_key'
+
+ stub_api_key('key') {|config| stub(config).api_keys {{:another_name => other_key}}}
+
+ @command.handle_options([@gem, "-v", @version, "-k", "another_name"])
+ @command.execute
+ assert_requested(:delete, @api,
+ :times => 1)
+ assert_requested(:delete, @api,
+ :headers => { 'Authorization' => other_key })
+ end
+
context 'with a platform specified' do
setup do
stub_api_key("key")
@@ -68,10 +82,10 @@ class YankCommandTest < CommandTest
end
should 'invoke yank_gem' do
- stub(@command).yank_gem(@version, @platform)
+ stub(@command).yank_gem(@version, @platform, @api_key)
@command.execute
assert_received(@command) do |command|
- command.yank_gem(@version, @platform)
+ command.yank_gem(@version, @platform, @api_key)
end
end
end
@@ -80,7 +94,8 @@ class YankCommandTest < CommandTest
context 'unyanking a gem' do
setup do
- stub_api_key("key")
+ @api_key = "key"
+ stub_api_key(@api_key)
@api = "https://rubygems.org/api/v1/gems/unyank"
stub_request(:put, @api).to_return(:body => "Successfully unyanked")
@command.handle_options([@gem, "-v", @version, "--undo"])
@@ -95,10 +110,10 @@ class YankCommandTest < CommandTest
end
should 'invoke unyank_gem' do
- stub(@command).unyank_gem(@version, @platform)
+ stub(@command).unyank_gem(@version, @platform, @api_key)
@command.execute
assert_received(@command) do |command|
- command.unyank_gem(@version, @platform)
+ command.unyank_gem(@version, @platform, @api_key)
end
end
@@ -112,7 +127,8 @@ class YankCommandTest < CommandTest
context 'with a platform specified' do
setup do
- stub_api_key("key")
+ @api_key = 'key'
+ stub_api_key(@api_key)
@api = "https://rubygems.org/api/v1/gems/unyank"
@platform = "x86-darwin-10"
stub_request(:put, @api).to_return(:body => "Successfully unyanked")
@@ -128,10 +144,10 @@ class YankCommandTest < CommandTest
end
should 'invoke unyank_gem' do
- stub(@command).unyank_gem(@version, @platform)
+ stub(@command).unyank_gem(@version, @platform, @api_key)
@command.execute
assert_received(@command) do |command|
- command.unyank_gem(@version, @platform)
+ command.unyank_gem(@version, @platform, @api_key)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.