-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Progress toward #4.
- Loading branch information
Showing
4 changed files
with
41 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |