Permalink
Browse files

Initial client library and docs

  • Loading branch information...
1 parent d3e10c6 commit e27b9431b277fd7e3d9b6bd2edb77ef648f7dc0b @tarcieri tarcieri committed Oct 6, 2011
Showing with 94 additions and 6 deletions.
  1. +3 −0 .rspec
  2. +24 −4 README.md
  3. +5 −0 Rakefile
  4. +3 −0 http.gemspec
  5. +14 −2 lib/http.rb
  6. +32 −0 lib/http/client.rb
  7. +11 −0 spec/http_spec.rb
  8. +2 −0 spec/spec_helper.rb
View
3 .rspec
@@ -0,0 +1,3 @@
+--color
+--format documentation
+--backtrace
View
28 README.md
@@ -1,8 +1,28 @@
HTTP
====
-This is an HTTP toolkit for doing HTTP things in Ruby. Ruby is a great
-language but one of its great shortcomings is its inability to speak
-HTTP properly.
+Ruby has always been this extremely web-focused language, and yet despite the
+selection of HTTP libraries out there, I always find myself falling back on
+Net::HTTP, and Net::HTTP sucks.
-Maybe I can fix that! Trololol...
+Ruby should be simple and elegant and beautiful. Net::HTTP is not. I've often
+found myself falling back on the Perlish horrors of open-uri just because I
+found Net::HTTP to be too much of a pain. This shouldn't be!
+
+HTTP should be simple and easy! It should be so straightforward it makes
+you happy with how delightful it is to use!
+
+API
+---
+
+Let's start with getting things:
+
+ Http.get("http://www.google.com")
+
+That's it! The result is the response body.
+
+Don't like "Http"? No worries, this works as well:
+
+ HTTP.get("http://www.google.com")
+
+After all, There Is More Than One Way To Do It!
View
5 Rakefile
@@ -1,2 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
View
3 http.gemspec
@@ -14,4 +14,7 @@ Gem::Specification.new do |gem|
gem.name = "http"
gem.require_paths = ["lib"]
gem.version = Http::VERSION
+
+ gem.add_development_dependency 'rake'
+ gem.add_development_dependency 'rspec', '>= 2.6.0'
end
View
16 lib/http.rb
@@ -1,5 +1,17 @@
-require "http/version"
+require 'http/version'
+require 'http/client'
+# THIS IS ENTIRELY TEMPORARY, I ASSURE YOU
+require 'net/http'
+require 'uri'
+
+# Http, it can be simple!
module Http
- # Coming soon!
+ def self.get(uri, options = {})
+ Client.new(uri).get(options = {})
+ end
end
+
+# TIMTOWTDI!
+HTTP = Http
+HttpClient = Http::Client
View
32 lib/http/client.rb
@@ -0,0 +1,32 @@
+module Http
+ # We all know what HTTP clients are, right?
+ class Client
+ # I swear I'll document that nebulous options hash
+ def initialize(uri, options = {})
+ # Argument coersion is a bit gnarly, isn't it?
+ case uri
+ when String
+ # Why the FUCK can't Net::HTTP do this?
+ @uri = URI.parse(uri)
+ when URI
+ @uri = uri
+ else
+ if uri.respond_to :to_uri
+ @uri = uri.to_uri
+ else
+ raise ArgumentError, "can't convert #{uri.class} to a URI"
+ end
+ end
+
+ @options = options
+ end
+
+ def get(options = {})
+ # Trolol these don't do anything yet
+ options = @options.merge(options)
+
+ # NO! Don't even thing about reusing the Net::HTTP options hash
+ Net::HTTP.get(@uri)
+ end
+ end
+end
View
11 spec/http_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe Http do
+ context "getting resources" do
+ it "should be easy" do
+ # Fuck it, we'll do it live! (Testing against WEBRick or something coming soon)
+ response = Http.get("http://www.google.com")
+ response.should match(/<!doctype html>/)
+ end
+ end
+end
View
2 spec/spec_helper.rb
@@ -0,0 +1,2 @@
+$:.push File.expand_path("../lib")
+require 'http'

0 comments on commit e27b943

Please sign in to comment.