brauchbar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/README.md +71 -0
- data/Rakefile +77 -0
- data/lib/brauchbar/request.rb +26 -0
- data/lib/brauchbar/response.rb +16 -0
- data/lib/brauchbar.rb +18 -0
- metadata +97 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# brauchbar
|
2
|
+
|
3
|
+
Adj, German.
|
4
|
+
practical, convenient, utilizable, feasible
|
5
|
+
|
6
|
+
In other words, a decent HTTP client for Ruby!
|
7
|
+
|
8
|
+
## Introduction
|
9
|
+
|
10
|
+
### But what.... what is wrong with all the other client?
|
11
|
+
|
12
|
+
The majority of other clients are built on top off Net::HTTP. This is the HTTP library built into Ruby, and [it is slow](http://pivotallabs.com/users/steve/blog/articles/644-net-http-alternatives)... It also has a few annoying features, such as downcasing all headers. Oh, and they don't have a cool German name!
|
13
|
+
|
14
|
+
### So, this is special how?
|
15
|
+
|
16
|
+
brauchbar is built on top of the [patron](http://github.com/toland/patron) gem, which itself is based upon libcurl. In other words, it has history. History is good.
|
17
|
+
The brauchbar API is based upon a number of other good HTTP clients and is designed to be simple and easy to integrate to your application.
|
18
|
+
|
19
|
+
brauchbar doesn't introduce anything new into the world of HTTP clients, however it brings the best parts of all of them into one place, which is accessible by a simple, future-proof API.
|
20
|
+
|
21
|
+
## Install
|
22
|
+
|
23
|
+
gem install brauchbar
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Basic usage
|
28
|
+
|
29
|
+
Brauchbar.get "http://www.google.co.uk/search?hl=en&q=brauchbar&meta=&esrch=FT1"
|
30
|
+
|
31
|
+
### Headers can be added
|
32
|
+
|
33
|
+
Brauchbar.get "http://www.google.co.uk/search?hl=en&q=brauchbar&meta=&esrch=FT1",
|
34
|
+
:headers => { 'Accept' => 'text/html', 'User-Agent' => 'brauchbar test 1.0' }
|
35
|
+
|
36
|
+
### A Response object is returned
|
37
|
+
|
38
|
+
res = Brauchbar.get "http://www.google.co.uk/search?hl=en&q=brauchbar&meta=&esrch=FT1"
|
39
|
+
|
40
|
+
# response body
|
41
|
+
# <html><head>.....</html>
|
42
|
+
puts res
|
43
|
+
|
44
|
+
# headers
|
45
|
+
# { 'Content-Type' => 'text/html' }
|
46
|
+
puts res.headers
|
47
|
+
|
48
|
+
# http status
|
49
|
+
# 200
|
50
|
+
puts res.status
|
51
|
+
|
52
|
+
## Hacking
|
53
|
+
|
54
|
+
Feel free to hack away at this, however I won't merge anything unless you have adequate tests for it! Once you have done, send a pull request.
|
55
|
+
|
56
|
+
## Todo / What would be nice
|
57
|
+
|
58
|
+
* POST requests with POST data
|
59
|
+
* Remembering of cookies between requests
|
60
|
+
* Changing of timeouts
|
61
|
+
* HTTP authentication
|
62
|
+
|
63
|
+
## Contributors
|
64
|
+
|
65
|
+
* [Luca Spiller](http://github.com/lucaspiller/)
|
66
|
+
|
67
|
+
## License
|
68
|
+
|
69
|
+
brauchbar is licensed under a [Creative Commons Attribution 2.0 UK: England & Wales License](http://creativecommons.org/licenses/by/2.0/uk/).
|
70
|
+
|
71
|
+
![CC Some Rights Reserved](http://creativecommons.org/images/public/somerights20.png)
|
data/Rakefile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift 'lib'
|
5
|
+
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
require 'rspec/core/version'
|
9
|
+
|
10
|
+
Rspec::Core::RakeTask.new(:spec)
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
|
14
|
+
require 'rake/gempackagetask'
|
15
|
+
require 'rake/rdoctask'
|
16
|
+
|
17
|
+
# This builds the actual gem. For details of what all these options
|
18
|
+
# mean, and other ones you can add, check the documentation here:
|
19
|
+
#
|
20
|
+
# http://rubygems.org/read/chapter/20
|
21
|
+
#
|
22
|
+
spec = Gem::Specification.new do |s|
|
23
|
+
|
24
|
+
# Change these as appropriate
|
25
|
+
s.name = "brauchbar"
|
26
|
+
s.version = "0.1.0"
|
27
|
+
s.summary = "adj, German. practical, convenient, utilizable, feasible AKA a decent HTTP client for Ruby"
|
28
|
+
s.author = "Luca Spiller"
|
29
|
+
s.email = "luca@stackednotion.com"
|
30
|
+
s.homepage = "http://www.stackednotion.com/"
|
31
|
+
|
32
|
+
s.has_rdoc = true
|
33
|
+
s.extra_rdoc_files = %w(README.md)
|
34
|
+
s.rdoc_options = %w(--main README.md)
|
35
|
+
|
36
|
+
# Add any extra files to include in the gem
|
37
|
+
s.files = %w(Gemfile Rakefile README.md) + Dir.glob("{spec,lib/**/*}")
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
|
40
|
+
# If you want to depend on other gems, add them here, along with any
|
41
|
+
# relevant versions
|
42
|
+
s.add_dependency("patron", "0.4.6")
|
43
|
+
|
44
|
+
# If your tests use any gems, include them here
|
45
|
+
s.add_development_dependency("rspec", ">=2.0.0.beta.5")
|
46
|
+
end
|
47
|
+
|
48
|
+
# This task actually builds the gem. We also regenerate a static
|
49
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
50
|
+
# be automatically building a gem for this project. If you're not
|
51
|
+
# using GitHub, edit as appropriate.
|
52
|
+
#
|
53
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
54
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
55
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
56
|
+
pkg.gem_spec = spec
|
57
|
+
end
|
58
|
+
|
59
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
60
|
+
task :gemspec do
|
61
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
62
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
63
|
+
end
|
64
|
+
|
65
|
+
task :package => :gemspec
|
66
|
+
|
67
|
+
# Generate documentation
|
68
|
+
Rake::RDocTask.new do |rd|
|
69
|
+
rd.main = "README.md"
|
70
|
+
rd.rdoc_files.include("README.md", "lib/**/*.rb")
|
71
|
+
rd.rdoc_dir = "rdoc"
|
72
|
+
end
|
73
|
+
|
74
|
+
desc 'Clear out RDoc and generated packages'
|
75
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
76
|
+
rm "#{spec.name}.gemspec"
|
77
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Brauchbar::Request
|
2
|
+
def self.execute(params = {})
|
3
|
+
# validation
|
4
|
+
raise ArgumentError.new('Method option required') if params[:method].nil?
|
5
|
+
raise ArgumentError.new('Unsupported Method') unless %w{ get }.include?(params[:method].to_s)
|
6
|
+
|
7
|
+
raise ArgumentError.new('URI option required') if params[:uri].nil?
|
8
|
+
begin
|
9
|
+
URI.parse(params[:uri])
|
10
|
+
rescue URI::InvalidURIError => e
|
11
|
+
raise ArgumentError.new('Invalid URI')
|
12
|
+
end
|
13
|
+
|
14
|
+
# clear headers
|
15
|
+
params[:headers] ||= {}
|
16
|
+
|
17
|
+
# create request
|
18
|
+
session = Patron::Session.new
|
19
|
+
|
20
|
+
# make request
|
21
|
+
response = session.request(params[:method], params[:uri], params[:headers])
|
22
|
+
|
23
|
+
# create brauchbar response object
|
24
|
+
Brauchbar::Response.new response
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Brauchbar::Response < String
|
2
|
+
attr_reader :raw_status, :raw_body, :raw_headers
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
@raw_status = response.status
|
6
|
+
@raw_body = response.body
|
7
|
+
@raw_headers = response.headers
|
8
|
+
|
9
|
+
# no need to overwrite to_s :)
|
10
|
+
super(body)
|
11
|
+
end
|
12
|
+
|
13
|
+
alias :status :raw_status
|
14
|
+
alias :body :raw_body
|
15
|
+
alias :headers :raw_headers
|
16
|
+
end
|
data/lib/brauchbar.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'patron'
|
5
|
+
|
6
|
+
class Brauchbar
|
7
|
+
autoload :Request, 'brauchbar/request'
|
8
|
+
autoload :Response, 'brauchbar/response'
|
9
|
+
|
10
|
+
def self.get(uri, params = {})
|
11
|
+
options = {
|
12
|
+
:method => :get,
|
13
|
+
:uri => uri
|
14
|
+
}.merge(params)
|
15
|
+
|
16
|
+
Request.execute options
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brauchbar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Luca Spiller
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-08 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: patron
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 4
|
30
|
+
- 6
|
31
|
+
version: 0.4.6
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 0
|
44
|
+
- 0
|
45
|
+
- beta
|
46
|
+
- 5
|
47
|
+
version: 2.0.0.beta.5
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
description:
|
51
|
+
email: luca@stackednotion.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- README.md
|
58
|
+
files:
|
59
|
+
- Gemfile
|
60
|
+
- Rakefile
|
61
|
+
- README.md
|
62
|
+
- lib/brauchbar/request.rb
|
63
|
+
- lib/brauchbar/response.rb
|
64
|
+
- lib/brauchbar.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://www.stackednotion.com/
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --main
|
72
|
+
- README.md
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.3.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: adj, German. practical, convenient, utilizable, feasible AKA a decent HTTP client for Ruby
|
96
|
+
test_files: []
|
97
|
+
|