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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



7
8
9
10
# File 'lib/relaxdb/net_http_server.rb', line 7

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#close_connectionObject



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

def close_connection
  @cx.finish if @cx
end

#cxObject



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

def cx
  unless @cx && @cx.active?
    @cx = Net::HTTP.start(@host, @port)
  end
  @cx
end

#delete(uri) ⇒ Object



23
24
25
# File 'lib/relaxdb/net_http_server.rb', line 23

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

#get(uri) ⇒ Object



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

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

#post(uri, json) ⇒ Object



38
39
40
41
42
43
# File 'lib/relaxdb/net_http_server.rb', line 38

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



31
32
33
34
35
36
# File 'lib/relaxdb/net_http_server.rb', line 31

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)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/relaxdb/net_http_server.rb', line 45

def request(req)
  begin
    res = cx.request(req)
  rescue
    @cx = nil
    res = cx.request(req)
  end
  
  if (not res.kind_of?(Net::HTTPSuccess))
    handle_error(req, res)
  end
  res
end

#to_sObject



59
60
61
# File 'lib/relaxdb/net_http_server.rb', line 59

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