Class: RelaxDB::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



9
10
11
12
# File 'lib/relaxdb/server.rb', line 9

def initialize(host, port)
  @host = host
  @port = port
end

Instance Method Details

#delete(uri) ⇒ Object



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

def delete(uri)
  request(Net::HTTP::Delete.new(uri))
end

#get(uri) ⇒ Object



18
19
20
# File 'lib/relaxdb/server.rb', line 18

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

#post(uri, json) ⇒ Object



29
30
31
32
33
34
# File 'lib/relaxdb/server.rb', line 29

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

#put(uri, json) ⇒ Object



22
23
24
25
26
27
# File 'lib/relaxdb/server.rb', line 22

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

#request(req) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/relaxdb/server.rb', line 36

def request(req)
  res = Net::HTTP.start(@host, @port) {|http|
    http.request(req)
  }
  if (not res.kind_of?(Net::HTTPSuccess))
    handle_error(req, res)
  end
  res
end

#to_sObject



46
47
48
# File 'lib/relaxdb/server.rb', line 46

def to_s
  "http://#{@host}:#{@port}/"
end