Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use taf2-curb instead of net/http: much nippier

  • Loading branch information...
commit 92f7ff93130e8286e379a6953293f3a727e8bf0b 1 parent a89edb9
Frederick Cheung authored

Showing 2 changed files with 41 additions and 37 deletions. Show diff stats Hide diff stats

  1. +2 0  lib/relaxdb.rb
  2. +39 37 lib/relaxdb/server.rb
2  lib/relaxdb.rb
@@ -2,6 +2,8 @@
2 2 require 'extlib'
3 3 require 'json'
4 4 require 'uuid'
  5 +gem 'taf2-curb'
  6 +require 'curb'
5 7
6 8 require 'cgi'
7 9 require 'net/http'
76 lib/relaxdb/server.rb
@@ -3,64 +3,66 @@ module RelaxDB
3 3 class HTTP_404 < StandardError; end
4 4 class HTTP_409 < StandardError; end
5 5 class HTTP_412 < StandardError; end
6   -
  6 +
7 7 class Server
8   -
  8 + class Response
  9 + attr_reader :body
  10 + def initialize body
  11 + @body = body
  12 + end
  13 + end
  14 +
9 15 def initialize(host, port)
10   - @host = host
11   - @port = port
  16 + @host, @port = host, port
12 17 end
13   -
  18 +
14 19 def delete(uri)
15   - request(Net::HTTP::Delete.new(uri))
  20 + request(uri, 'delete'){ |c| c.http_delete}
16 21 end
17 22
18 23 def get(uri)
19   - request(Net::HTTP::Get.new(uri))
  24 + request(uri, 'get'){ |c| c.http_get}
20 25 end
21 26
22 27 def put(uri, json)
23   - req = Net::HTTP::Put.new(uri)
24   - req["content-type"] = "application/json"
25   - req.body = json
26   - request(req)
  28 + request(uri, 'put') do |c|
  29 + c.headers['content-type'] = 'application/json'
  30 + c.http_put json
  31 + end
27 32 end
28 33
29 34 def post(uri, json)
30   - req = Net::HTTP::Post.new(uri)
31   - req["content-type"] = "application/json"
32   - req.body = json
33   - request(req)
  35 + request(uri, 'post') do |c|
  36 + c.headers['content-type'] = 'application/json'
  37 + c.http_post json
  38 + end
34 39 end
35 40
36   - def request(req)
37   - res = Net::HTTP.start(@host, @port) {|http|
38   - http.request(req)
39   - }
40   - if (not res.kind_of?(Net::HTTPSuccess))
41   - handle_error(req, res)
  41 + def request(uri, method)
  42 + c = Curl::Easy.new "http://#{@host}:#{@port}#{uri}"
  43 + yield c
  44 +
  45 + if c.response_code < 200 || c.response_code >= 300
  46 + status_line = c.header_str.split('\r\n').first
  47 + msg = "#{c.response_code}:#{status_line}\nMETHOD:#{method}\nURI:#{uri}\n#{c.body_str}"
  48 + begin
  49 + klass = RelaxDB.const_get("HTTP_#{c.response_code}")
  50 + e = klass.new(msg)
  51 + rescue
  52 + e = RuntimeError.new(msg)
  53 + end
  54 +
  55 + raise e
42 56 end
43   - res
44   - end
45   -
  57 + Response.new c.body_str
  58 + end
  59 +
46 60 def to_s
47 61 "http://#{@host}:#{@port}/"
48 62 end
49   -
50   - private
51 63
52   - def handle_error(req, res)
53   - msg = "#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}"
54   - begin
55   - klass = RelaxDB.const_get("HTTP_#{res.code}")
56   - e = klass.new(msg)
57   - rescue
58   - e = RuntimeError.new(msg)
59   - end
60   -
61   - raise e
62   - end
63 64 end
  65 +
64 66
65 67 class CouchDB
66 68

0 comments on commit 92f7ff9

Please sign in to comment.
Something went wrong with that request. Please try again.