Class: RelaxDB::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/net_http_server.rb,
lib/relaxdb/taf2_curb_server.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



5
6
7
8
# File 'lib/relaxdb/net_http_server.rb', line 5

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

Instance Method Details

#delete(uri) ⇒ Object



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

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

#get(uri) ⇒ Object



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

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

#post(uri, json) ⇒ Object



25
26
27
28
29
30
# File 'lib/relaxdb/net_http_server.rb', line 25

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



18
19
20
21
22
23
# File 'lib/relaxdb/net_http_server.rb', line 18

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

#request(uri, method) {|c| ... } ⇒ Object

Yields:

  • (c)


32
33
34
35
36
37
38
39
40
# File 'lib/relaxdb/net_http_server.rb', line 32

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



42
43
44
# File 'lib/relaxdb/net_http_server.rb', line 42

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