Updated website to reflect proper git repo path
[couchobject.git] / lib / couch_object / server.rb
blobbd20d7db0da8e99cb690b524222a1737c1eac960
1 require "net/http"
3 module CouchObject
4   class Server
5     # Create a new Server object, +uri+ is the full URI of the server,
6     # eg. "http://localhost:8888"
7     def initialize(uri)
8       @uri = URI.parse(uri)
9       @host = @uri.host
10       @port = @uri.port
11       @connection = Net::HTTP.new(@host, @port)
12       @connection.set_debug_output($stderr) if $debug
13     end
14     attr_accessor :host, :port, :connection
15     
16     # Send a GET request to +path+
17     def get(path)
18       request(Net::HTTP::Get.new(path))
19     end
20     
21     # Send a POST request to +path+ with the body payload of +data+
22     # +content_type+ is the Content-Type header to send along (defaults to
23     # application/json)
24     def post(path, data, content_type="application/json")
25       post = Net::HTTP::Post.new(path)
26       post["content-type"] = content_type
27       post.body = data
28       request(post)      
29     end
30     
31     # Send a PUT request to +path+ with the body payload of +data+
32     # +content_type+ is the Content-Type header to send along (defaults to
33     # application/json)
34     def put(path, data, content_type="application/json")
35       put = Net::HTTP::Put.new(path)
36       put["content-type"] = content_type
37       put.body = data
38       request(put)
39     end
40     
41     # Send a DELETE request to +path+
42     def delete(path)
43       req = Net::HTTP::Delete.new(path)
44       request(req)
45     end
46     
47     # send off a +req+ object to the host. req is a Net::Http:: request class
48     # (eg Net::Http::Get/Net::Http::Post etc)
49     def request(req)
50       connection.request(req)
51     end    
52   end
53 end