Class: Couch::Server::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(req, json = nil, opts = {open_timeout: 5*30, read_timeout: 5*30, fail_silent: false}) ⇒ Request



343
344
345
346
347
# File 'lib/couch.rb', line 343

def initialize(req, json=nil, opts={open_timeout: 5*30, read_timeout: 5*30, fail_silent: false})
  @req=req
  @json = json
  @options = opts
end

Instance Method Details

#couch_url(couch_url) ⇒ Object



354
355
356
357
# File 'lib/couch.rb', line 354

def couch_url(couch_url)
  @options.merge!({couch_url: couch_url})
  self
end

#fail_silent(fail_silent) ⇒ Object



369
370
371
372
# File 'lib/couch.rb', line 369

def fail_silent(fail_silent)
  @options.merge!({fail_silent: fail_silent})
  self
end

#handle_error(req, res) ⇒ Object

Raises:

  • (RuntimeError)


399
400
401
# File 'lib/couch.rb', line 399

def handle_error(req, res)
  raise RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
end

#json(json) ⇒ Object



349
350
351
352
# File 'lib/couch.rb', line 349

def json(json)
  @json = json
  self
end

#open_timeout(open_timeout) ⇒ Object



359
360
361
362
# File 'lib/couch.rb', line 359

def open_timeout(open_timeout)
  @options.merge!({open_timeout: open_timeout})
  self
end

#performObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/couch.rb', line 374

def perform
  @req.basic_auth @options[:name], @options[:password]

  if @json
    @req['Content-Type'] = 'application/json;charset=utf-8'
    @req.body = @json
  end

  res = Net::HTTP.start(
      @options[:couch_url].host,
      @options[:couch_url].port,
      {:use_ssl => @options[:couch_url].scheme =='https'}
  ) do |http|
    http.open_timeout = @options[:open_timeout]
    http.read_timeout = @options[:read_timeout]
    http.request(@req)
  end

  unless @options[:fail_silent] or res.kind_of?(Net::HTTPSuccess)
    # puts "CouchDb responsed with error code #{res.code}"

    handle_error(@req, res)
  end
  res
end

#read_timeout(read_timeout) ⇒ Object



364
365
366
367
# File 'lib/couch.rb', line 364

def read_timeout(read_timeout)
  @options.merge!({read_timeout: read_timeout})
  self
end