Permalink
Browse files

Remove faraday dependency

Progress toward #4.
  • Loading branch information...
sferik committed Jul 3, 2011
1 parent 0fbcd40 commit 777c1f5af3ae760e02e82c0820775481bfda94c2
Showing with 41 additions and 50 deletions.
  1. +0 −1 gems.gemspec
  2. +1 −3 lib/gems/client.rb
  3. +0 −27 lib/gems/connection.rb
  4. +40 −19 lib/gems/request.rb
View
@@ -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"]
View
@@ -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
View
@@ -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
View
@@ -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

0 comments on commit 777c1f5

Please sign in to comment.