Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring requests made all out to AbstractCommand#request

  • Loading branch information...
commit 8dbebf8dedecaf0a94c24dfedeb41e7ae54a6653 1 parent 4c51ce6
@qrush qrush authored
View
11 Rakefile
@@ -6,7 +6,16 @@ require 'rake/rdoctask'
require 'tasks/rails'
+namespace :test do
+ Rake::TestTask.new(:commands) do |t|
+ t.libs << "test"
+ t.test_files = FileList['test/commands/*_test.rb']
+ t.verbose = true
+ end
+end
+
+
desc "Run all tests and features"
-task :default => ['gemcutter:index:create', :test, :features]
+task :default => ['gemcutter:index:create', :test, :features, 'test:commands']
task :cron => ['gemcutter:update']
View
41 lib/commands/abstract_command.rb
@@ -26,14 +26,9 @@ def sign_in
email = ask("Email: ")
password = ask_for_password("Password: ")
- url = URI.parse("#{gemcutter_url}/api_key")
-
- http = Net::HTTP.new(url.host, url.port)
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- http.use_ssl = (url.scheme == 'https')
- request = Net::HTTP::Get.new(url.path)
- request.basic_auth email, password
- response = http.request(request)
+ response = make_request(:get, "api_key") do |request|
+ request.basic_auth email, password
+ end
case response
when Net::HTTPSuccess
@@ -46,6 +41,36 @@ def sign_in
end
end
+ def make_request(method, path)
+ require 'net/http'
+ require 'net/https'
+
+ url = URI.parse("#{gemcutter_url}/#{path}")
+
+ http = proxy_class.new(url.host, url.port)
+
+ if url.scheme == 'https'
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ http.use_ssl = true
+ end
+
+ request_method =
+ case method
+ when :get
+ proxy_class::Get
+ when :post
+ proxy_class::Post
+ when :put
+ proxy_class::Put
+ else
+ raise ArgumentError
+ end
+
+ request = request_method.new(url.path)
+ yield request if block_given?
+ http.request(request)
+ end
+
def use_proxy!
proxy_uri = http_proxy
@proxy_class = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
View
55 lib/commands/migrate.rb
@@ -10,11 +10,6 @@ def initialize
end
def execute
- require 'open-uri'
- require 'net/scp'
- require 'json'
- require 'tempfile'
-
setup
migrate
end
@@ -27,30 +22,31 @@ def migrate
end
def find(name)
- begin
- data = open("#{gemcutter_url}/gems/#{name}.json")
- @rubygem = JSON.parse(data.string)
- rescue OpenURI::HTTPError
+ require 'json'
+
+ response = make_request(:get, "gems/#{name}.json")
+
+ case response
+ when Net::HTTPSuccess
+ begin
+ @rubygem = JSON.parse(response.body)
+ rescue JSON::ParserError => json_error
+ say "There was a problem parsing the data: #{json_error}"
+ terminate_interaction
+ end
+ else
say "This gem is currently not hosted on Gemcutter."
terminate_interaction
- rescue JSON::ParserError => json_error
- say "There was a problem parsing the data: #{json_error}"
- terminate_interaction
end
end
def get_token
say "Starting migration of #{rubygem["name"]} from RubyForge..."
- url = URI.parse("#{gemcutter_url}/gems/#{rubygem["slug"]}/migrate")
-
- http = proxy_class.new(url.host, url.port)
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- http.use_ssl = (url.scheme == 'https')
- request = proxy_class::Post.new(url.path)
- request.add_field("Content-Length", 0)
- request.add_field("Authorization", api_key)
- response = http.request(request)
+ response = make_request(:post, "gems/#{rubygem["slug"]}/migrate") do |request|
+ request.add_field("Content-Length", 0)
+ request.add_field("Authorization", api_key)
+ end
case response
when Net::HTTPSuccess
@@ -63,6 +59,9 @@ def get_token
end
def upload_token(token)
+ require 'tempfile'
+ require 'net/scp'
+
url = "#{self.rubygem['rubyforge_project']}.rubyforge.org"
say "Uploading the migration token to #{url}. Please enter your RubyForge login:"
login = ask("Login: ")
@@ -84,15 +83,11 @@ def upload_token(token)
def check_for_approved
say "Asking Gemcutter to verify the upload..."
- url = URI.parse("#{gemcutter_url}/gems/#{rubygem["slug"]}/migrate")
-
- http = proxy_class.new(url.host, url.port)
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- http.use_ssl = (url.scheme == 'https')
- request = proxy_class::Put.new(url.path)
- request.add_field("Content-Length", 0)
- request.add_field("Authorization", api_key)
- response = http.request(request)
+
+ response = make_request(:put, "gems/#{rubygem["slug"]}/migrate") do |request|
+ request.add_field("Content-Length", 0)
+ request.add_field("Authorization", api_key)
+ end
say response.body
end
View
19 lib/commands/push.rb
@@ -18,9 +18,6 @@ def initialize
end
def execute
- require 'net/http'
- require 'net/https'
-
setup
send_gem
end
@@ -29,17 +26,11 @@ def send_gem
say "Pushing gem to Gemcutter..."
name = get_one_gem_name
- url = URI.parse("#{gemcutter_url}/gems")
-
- http = proxy_class.new(url.host, url.port)
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- http.use_ssl = (url.scheme == 'https')
- request = proxy_class::Post.new(url.path)
- request.body = File.open(name).read
- request.add_field("Content-Length", request.body.size)
- request.add_field("Authorization", api_key)
-
- response = http.request(request)
+ response = make_request(:post, "gems") do |request|
+ request.body = File.open(name).read
+ request.add_field("Content-Length", request.body.size)
+ request.add_field("Authorization", api_key)
+ end
say response.body
end
View
2  test/unit/commands/abstract_command_test.rb → test/commands/abstract_command_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../../command_helper'
+require File.dirname(__FILE__) + '/../command_helper'
class Gem::Commands::FakeCommand < Gem::AbstractCommand
def description
View
3  test/unit/commands/migrate_command_test.rb → test/commands/migrate_command_test.rb
@@ -1,4 +1,5 @@
-require File.dirname(__FILE__) + '/../../command_helper'
+require File.dirname(__FILE__) + '/../command_helper'
+require 'net/scp'
class MigrateCommandTest < CommandTest
context "executing the command" do
View
2  test/unit/commands/push_command_test.rb → test/commands/push_command_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../../command_helper'
+require File.dirname(__FILE__) + '/../command_helper'
class PushCommandTest < CommandTest
context "pushing" do
View
2  test/unit/commands/tumble_command_test.rb → test/commands/tumble_command_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../../command_helper'
+require File.dirname(__FILE__) + '/../command_helper'
class TumbleCommandTest < CommandTest
context "with a tumbler and some sources" do
Please sign in to comment.
Something went wrong with that request. Please try again.