Class: Newque::Http_json

Inherits:
Object
  • Object
show all
Defined in:
lib/newque/http/http_json.rb

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Http_json

Returns a new instance of Http_json.



6
7
8
# File 'lib/newque/http/http_json.rb', line 6

def initialize http
  @http = http
end

Instance Method Details

#read(channel, mode, limit = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/newque/http/http_json.rb', line 28

def read channel, mode, limit=nil
  thread = Thread.new do
    res = @http.conn.get do |req|
      @http.send :set_req_options, req
      req.url "/v1/#{channel}"
      req.headers['newque-mode'] = mode
      req.headers['newque-read-max'] = limit unless limit.nil?
    end
    parsed = @http.send :parse_json_response, res.body
    Read_response.new(
      res.headers['newque-response-length'].to_i,
      res.headers['newque-response-last-id'],
      res.headers['newque-response-last-ts'].to_i,
      parsed['messages']
    )
  end
  Future.new thread, @http.timeout
end

#write(channel, atomic, msgs, ids = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/newque/http/http_json.rb', line 10

def write channel, atomic, msgs, ids=nil
  thread = Thread.new do
    body = {
      'atomic' => false,
      'messages' => msgs
    }
    body["ids"] = ids unless ids.nil?
    res = @http.conn.post do |req|
      @http.send :set_req_options, req
      req.url "/v1/#{channel}"
      req.body = body.to_json
    end
    parsed = @http.send :parse_json_response, res.body
    Write_response.new parsed['saved']
  end
  Future.new thread, @http.timeout
end