Skip to content

Commit

Permalink
Remove faraday dependency
Browse files Browse the repository at this point in the history
Progress toward #4.
  • Loading branch information
sferik committed Jul 3, 2011
1 parent 0fbcd40 commit 777c1f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
1 change: 0 additions & 1 deletion gems.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 1 addition & 3 deletions lib/gems/client.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
27 changes: 0 additions & 27 deletions lib/gems/connection.rb

This file was deleted.

59 changes: 40 additions & 19 deletions lib/gems/request.rb
Original file line number Diff line number Diff line change
@@ -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.