Class: Couch::Server::Request
- Inherits:
-
Object
- Object
- Couch::Server::Request
- Defined in:
- lib/couch.rb
Instance Method Summary collapse
- #couch_url(couch_url) ⇒ Object
- #fail_silent(fail_silent) ⇒ Object
- #handle_error(req, res) ⇒ Object
-
#initialize(req, json = nil, opts = {open_timeout: 5*30, read_timeout: 5*30, fail_silent: false}) ⇒ Request
constructor
A new instance of Request.
- #json(json) ⇒ Object
- #open_timeout(open_timeout) ⇒ Object
- #perform ⇒ Object
- #read_timeout(read_timeout) ⇒ Object
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 = 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) .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) .merge!({fail_silent: fail_silent}) self end |
#handle_error(req, res) ⇒ Object
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) .merge!({open_timeout: open_timeout}) self end |
#perform ⇒ Object
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 [:name], [:password] if @json @req['Content-Type'] = 'application/json;charset=utf-8' @req.body = @json end res = Net::HTTP.start( [:couch_url].host, [:couch_url].port, {:use_ssl => [:couch_url].scheme =='https'} ) do |http| http.open_timeout = [:open_timeout] http.read_timeout = [:read_timeout] http.request(@req) end unless [: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) .merge!({read_timeout: read_timeout}) self end |