class Newque::Http_json

Public Class Methods

new(http) click to toggle source
# File lib/newque/http/http_json.rb, line 6
def initialize http
  @http = http
end

Public Instance Methods

read(channel, mode, limit=nil) click to toggle source
# 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) click to toggle source
# 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