From 777c1f5af3ae760e02e82c0820775481bfda94c2 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sun, 3 Jul 2011 01:10:19 -0700 Subject: [PATCH] Remove faraday dependency Progress toward #4. --- gems.gemspec | 1 - lib/gems/client.rb | 4 +-- lib/gems/connection.rb | 27 ------------------- lib/gems/request.rb | 59 ++++++++++++++++++++++++++++-------------- 4 files changed, 41 insertions(+), 50 deletions(-) delete mode 100644 lib/gems/connection.rb diff --git a/gems.gemspec b/gems.gemspec index c1bb208..7a90bce 100644 --- a/gems.gemspec +++ b/gems.gemspec @@ -9,7 +9,6 @@ Gem::Specification.new do |gem| gem.add_development_dependency 'simplecov', '~> 0.4' gem.add_development_dependency 'webmock', '~> 1.6' gem.add_development_dependency 'yard', '~> 0.7' - gem.add_runtime_dependency 'faraday', '~> 0.6.1' gem.add_runtime_dependency 'multi_json', '~> 1.0.3' gem.authors = ["Erik Michaels-Ober"] diff --git a/lib/gems/client.rb b/lib/gems/client.rb index 89e4f36..1494c0e 100644 --- a/lib/gems/client.rb +++ b/lib/gems/client.rb @@ -1,13 +1,11 @@ require 'date' require 'gems/configuration' -require 'gems/connection' require 'gems/request' require 'multi_json' require 'yaml' module Gems class Client - include Gems::Connection include Gems::Request attr_accessor *Configuration::VALID_OPTIONS_KEYS @@ -185,7 +183,7 @@ def fire_web_hook(gem_name, url) # @param gem [File] A built gem. # @return [String] # @example - # Gems.push File.new 'pkg/gemcutter-0.2.1.gem', 'rb' + # Gems.push File.new 'pkg/gemcutter-0.2.1.gem' def push(gem) post("/api/v1/gems", gem.read, 'application/octet-stream') end diff --git a/lib/gems/connection.rb b/lib/gems/connection.rb deleted file mode 100644 index fd60e0a..0000000 --- a/lib/gems/connection.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'faraday' - -module Gems - module Connection - def connection(content_length=nil, content_type=nil) - options = { - :headers => { - :user_agent => user_agent, - }, - :ssl => {:verify => false}, - :url => host, - } - - options[:headers].merge!({:content_length => content_length}) if content_length - options[:headers].merge!({:content_type => content_type}) if content_type - options[:headers].merge!({:authorization => key}) if key - - connection = Faraday.new(options) do |connection| - connection.use Faraday::Request::UrlEncoded unless content_type - connection.use Faraday::Response::RaiseError - connection.adapter Faraday.default_adapter - end - connection.basic_auth username, password if username && password - connection - end - end -end diff --git a/lib/gems/request.rb b/lib/gems/request.rb index 6e048dc..eb19048 100644 --- a/lib/gems/request.rb +++ b/lib/gems/request.rb @@ -1,40 +1,61 @@ +require 'net/http' require 'rubygems' +require 'uri' module Gems module Request - def delete(path, options={}, content_type=nil) - request(:delete, path, options, content_type) + def delete(path, data={}, content_type='application/x-www-form-urlencoded') + request(:delete, path, data, content_type) end - def get(path, options={}, content_type=nil) - request(:get, path, options, content_type) + def get(path, data={}, content_type='application/x-www-form-urlencoded') + request(:get, path, data, content_type) end - def post(path, options={}, content_type=nil) - request(:post, path, options, content_type) + def post(path, data={}, content_type='application/x-www-form-urlencoded') + request(:post, path, data, content_type) end - def put(path, options={}, content_type=nil) - request(:put, path, options, content_type) + def put(path, data={}, content_type='application/x-www-form-urlencoded') + request(:put, path, data, content_type) end private - def request(method, path, options, content_type) - content_length = case content_type + def request(method, path, data, content_type) + path = [path, hash_to_query_string(data)[/.+/]].compact.join('?') if [:delete, :get].include? method + uri = URI.parse [host, path].join + request_class = Net::HTTP.const_get method.to_s.capitalize + request = request_class.new uri.request_uri + request.add_field 'Authorization', key if key + request.add_field 'Connection', 'keep-alive' + request.add_field 'Keep-Alive', '30' + request.add_field 'User-Agent', user_agent + request.basic_auth username, password if username && password + request.content_type = content_type + case content_type + when 'application/x-www-form-urlencoded' + request.form_data = data if [:post, :put].include? method when 'application/octet-stream' - options.size + request.body = data + request.content_length = data.size end - response = connection(content_length, content_type).send(method) do |request| - case method - when :delete, :get - request.url(path, options) - when :post, :put - request.path = path - request.body = options unless options == {} - end + connection = Net::HTTP.new uri.host, uri.port + if uri.scheme == 'https' + require 'net/https' + connection.use_ssl = true + connection.verify_mode = OpenSSL::SSL::VERIFY_NONE end + connection.start + response = connection.request request response.body end + + def hash_to_query_string(hash) + hash.keys.inject('') do |query_string, key| + query_string << '&' unless key == hash.keys.first + query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key])}" + end + end end end