Class: CouchObject::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_object/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Server

Create a new Server object, uri is the full URI of the server, eg. “localhost:5984



7
8
9
10
11
12
13
# File 'lib/couch_object/server.rb', line 7

def initialize(uri)
  @uri = URI.parse(uri)
  @host = @uri.host
  @port = @uri.port
  @connection = Net::HTTP.new(@host, @port)
  @connection.set_debug_output($stderr) if $debug
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



14
15
16
# File 'lib/couch_object/server.rb', line 14

def connection
  @connection
end

#hostObject

Returns the value of attribute host.



14
15
16
# File 'lib/couch_object/server.rb', line 14

def host
  @host
end

#portObject

Returns the value of attribute port.



14
15
16
# File 'lib/couch_object/server.rb', line 14

def port
  @port
end

Instance Method Details

#delete(path) ⇒ Object

Send a DELETE request to path



42
43
44
45
# File 'lib/couch_object/server.rb', line 42

def delete(path)
  req = Net::HTTP::Delete.new(path)
  request(req)
end

#get(path) ⇒ Object

Send a GET request to path



17
18
19
# File 'lib/couch_object/server.rb', line 17

def get(path)
  request(Net::HTTP::Get.new(path))
end

#post(path, data, content_type = "application/json") ⇒ Object

Send a POST request to path with the body payload of data content_type is the Content-Type header to send along (defaults to application/json)



24
25
26
27
28
29
# File 'lib/couch_object/server.rb', line 24

def post(path, data, content_type="application/json")
  post = Net::HTTP::Post.new(path)
  post["content-type"] = content_type
  post.body = data
  request(post)      
end

#put(path, data, content_type = "application/json") ⇒ Object

Send a PUT request to path with the body payload of data content_type is the Content-Type header to send along (defaults to application/json)



34
35
36
37
38
39
# File 'lib/couch_object/server.rb', line 34

def put(path, data, content_type="application/json")
  put = Net::HTTP::Put.new(path)
  put["content-type"] = content_type
  put.body = data
  request(put)
end

#request(req) ⇒ Object

send off a req object to the host. req is a Net::Http

request class

(eg Net::Http::Get/Net::Http::Post etc)



49
50
51
# File 'lib/couch_object/server.rb', line 49

def request(req)
  connection.request(req)
end