Permalink
Browse files

Committing everything

  • Loading branch information...
0 parents commit 265a6ab7e798e6882de3d12a2b051c409400f866 @ctcherry committed Mar 27, 2011
Showing with 79 additions and 0 deletions.
  1. +22 −0 README.md
  2. +41 −0 lib/plain_http.rb
  3. +16 −0 plain_http.gemspec
@@ -0,0 +1,22 @@
+# plain_http
+
+I always forget the API for Net:HTTP. This library wraps Net::HTTP and doesn't try to do anything too clever on top of that. PlainHTTP has 'get', 'post', 'put', and 'delete' methods. Call any of them with the full URL, followed by a hash of headers. In the case of 'post' and 'put' you can include a 3rd parameter which is the data to send along, it can be a string or a hash.
+
+All of the calls return the built in Net:HTTPResponse objects.
+
+## Examples
+
+### GET
+
+ response = PlainHTTP.get('http://www.google.com')
+ puts response.body
+
+### POST with form params
+
+ response = PlainHTTP.post('http://example.com/test.php', {}, {:post_name => 'value'})
+ puts response.body
+
+### POST with string body
+
+ response = PlainHTTP.post('http://example.com/test.php', {}, '{"name":"Chris Cherry"}')
+ puts response.body
@@ -0,0 +1,41 @@
+require 'net/http'
+
+class PlainHTTP
+
+ def self.head(url, headers = {})
+ request('HEAD', url, headers, nil)
+ end
+
+ def self.get(url, headers = {})
+ request('GET', url, headers, nil)
+ end
+
+ def self.post(url, headers = {}, data = nil)
+ request('POST', url, headers, data)
+ end
+
+ def self.put(url, headers = {}, data = nil)
+ request('PUT', url, headers, data)
+ end
+
+ def self.delete(url, headers = {})
+ request('DELETE', url, headers, nil)
+ end
+
+ def self.request(method, url, headers, data)
+ method_class_name = method.to_s.downcase.sub(/^[a-z]/i) {|m| m.upcase}
+ req_class = Net::HTTP.const_get(method_class_name)
+ uri = URI.parse(url)
+ path = uri.path == '' ? '/' : uri.path
+ req = req_class.new(path, headers)
+ if data.respond_to?(:to_hash)
+ req.set_form_data(data.to_hash)
+ elsif data.to_s != ''
+ req.body = data
+ end
+ res = Net::HTTP.start(uri.host, uri.port) {|http|
+ http.request(req)
+ }
+ res
+ end
+end
@@ -0,0 +1,16 @@
+Gem::Specification.new do |s|
+ s.name = "plain_http"
+ s.version = "0.1"
+ s.date = Date.today.to_s
+
+ s.summary = "An easy to remember interface to Net::HTTP"
+ s.description = "An easy to remember interface to Net::HTTP"
+
+ s.author = "Chris Cherry"
+ s.email = "ctcherry@gmail.com"
+ s.homepage = "https://github.com/ctcherry/plain_http"
+
+ s.require_paths = ["lib"]
+ s.files = ["lib/plain_http.rb"]
+
+end

0 comments on commit 265a6ab

Please sign in to comment.