Skip to content

tommetge/httprb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

an http interface that doesn’t suck.

if you’re like me, your finger seize at the thought of using net/http. it’s practically inconceivable that we have this beast while python has urllib/urllib2- and even c++ and java have equally better interfaces for doing client-side http work.

the days of teeth-gnashing are over.

httprb is what ruby (and rubyists) always wanted out of net/http: a ruby interface for communicating with the HTTP world. example:

get 'www.google.com'
# => #<Net::HTTPOK 200 OK readbody=true>

woah. wait. what about all this Net::HTTP.start(…) {http.get(…)} nonsense? throw it out the window- this is Ruby, after all.

ok, so the simple case looks good. what about headers, parameters, and all the rest of the HTTP world?

have no fear, check this out:

post 'www.example.com/login' do
  parameter "username", "tom"
  parameter "password", "password"
end
# => #<Net::HTTPOK 200 OK readbody=true>

getting better, eh? so relax, wipe the incredulity off your face- i know you’ve been waiting years for net/http to get better. the wait is over.

Installation

NOW AVAILABLE! just run:

gem install httprb

Usage

this is sinatra-inspired (clearly). choose your verb, add a url, and watch the sparks fly.

simple get request:

get 'www.google.com'
# => #<Net::HTTPOK 200 OK readbody=true>

you want the response object? you mean you actually want the body? no worries, you’re covered:

res = get 'www.google.com'
# => #<Net::HTTPOK 200 OK readbody=true>
res.body
...

let’s do a little header work:

res = get 'www.example.com'
  header "x-some-key", "some value"
  header "x-some-other-key", "some other value"
end
# => #<Net::HTTPOK 200 OK readbody=true>

moving on to a post with parameters…

res = post 'www.example.com'
  parameter "some_key", "some value"
  parameter "some_other_key", "some other value"
end
# => #<Net::HTTPOK 200 OK readbody=true>

Namespacing and Collisions

wait, you use Sinatra too? no worries, i won’t leave you high and dry. when a namespace collision is detected (i.e. you’ve required ‘sinatra’), HTTPrb automatically redefines its methods to carry the prefix, “client_”. example:

require 'sinatra'
require 'httprb'
res = client_head 'www.google.com'

if you don’t like the “client_” prefix, feel free to change it. the definition is pulled from the environment variable named, ‘HTTPRB_COLLISION_PREFIX’. yet another example:

...
ENV['HTTPRB_COLLISION_PREFIX'] = 'the_right_'
res = the_right_get 'www.google.com'

how do you know when collisions occur, you ask? HTTPrb kindly notifies you:

require 'sinatra'
require 'httprb'
get 'www.google.com'
# => WARNING: Namespace collision detected
# => HTTPrb::get has been renamed to client_get
# => To disable this message, set HTTPRB_IGNORE_COLLISIONS to false
# => ArgumentError: tried to create Proc object without a block
...

Author

tom metge <tom@accident-prone.com>

he’s an awesome guy.

Home / Download

the source (and anything else):

* http://github.com/tommetge/httprb

Documentation

* http://rdoc.info/github/tommetge/httprb/master/frames

About

http the ruby way: rack and sinatra inspired DSL for client-side HTTP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages