Module: ClientApiBuilder::Router
- Included in:
- NestedRouter
- Defined in:
- lib/client_api_builder/router.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #base_url ⇒ Object
- #build_body(body, options) ⇒ Object
- #build_connection_options(options) ⇒ Object
- #build_headers(options) ⇒ Object
- #build_query(query, options) ⇒ Object
- #build_uri(path, query, options) ⇒ Object
- #escape_path(path) ⇒ Object
- #expected_response_code!(response, expected_response_codes, options) ⇒ Object
- #get_retry_request_max_retries(options) ⇒ Object
- #get_retry_request_sleep_time(e, options) ⇒ Object
- #handle_response(response, options, &block) ⇒ Object
- #instrument_request ⇒ Object
- #log_request_exception(exception) ⇒ Object
- #parse_response(response, options) ⇒ Object
- #request_log_message ⇒ Object
- #request_wrapper(options) ⇒ Object
- #retry_request(options) ⇒ Object
- #retry_request?(exception, options) ⇒ Boolean
- #root_router ⇒ Object
Class Method Details
.included(base) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/client_api_builder/router.rb', line 7 def self.included(base) base.extend InheritanceHelper::Methods base.extend ClassMethods base.include ::ClientApiBuilder::Section base.include ::ClientApiBuilder::NetHTTP::Request base.include(::ClientApiBuilder::ActiveSupportNotifications) if defined?(ActiveSupport) base.send(:attr_reader, :response, :request_options, :total_request_time, :request_attempts) end |
Instance Method Details
#base_url ⇒ Object
352 353 354 |
# File 'lib/client_api_builder/router.rb', line 352 def base_url self.class.base_url end |
#build_body(body, options) ⇒ Object
405 406 407 408 409 410 411 412 |
# File 'lib/client_api_builder/router.rb', line 405 def build_body(body, ) body = [:body] if .key?(:body) return nil unless body return body if body.is_a?(String) self.class.build_body(self, body) end |
#build_connection_options(options) ⇒ Object
376 377 378 379 380 381 382 |
# File 'lib/client_api_builder/router.rb', line 376 def () if [:connection_options] self.class..merge([:connection_options]) else self.class. end end |
#build_headers(options) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/client_api_builder/router.rb', line 356 def build_headers() headers = {} add_header_proc = proc do |name, value| headers[name] = if value.is_a?(Proc) root_router.instance_eval(&value) elsif value.is_a?(Symbol) root_router.send(value) else value end end self.class.default_headers.each(&add_header_proc) [:headers] && [:headers].each(&add_header_proc) headers end |
#build_query(query, options) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/client_api_builder/router.rb', line 384 def build_query(query, ) query_params = {} add_query_param_proc = proc do |name, value| query_params[name] = if value.is_a?(Proc) root_router.instance_eval(&value) elsif value.is_a?(Symbol) root_router.send(value) else value end end self.class.default_query_params.each(&add_query_param_proc) query && query.each(&add_query_param_proc) [:query] && [:query].each(&add_query_param_proc) query_params.empty? ? nil : self.class.build_query(self, query_params) end |
#build_uri(path, query, options) ⇒ Object
414 415 416 417 418 |
# File 'lib/client_api_builder/router.rb', line 414 def build_uri(path, query, ) uri = URI(base_url + path) uri.query = build_query(query, ) uri end |
#escape_path(path) ⇒ Object
453 454 455 |
# File 'lib/client_api_builder/router.rb', line 453 def escape_path(path) path end |
#expected_response_code!(response, expected_response_codes, options) ⇒ Object
420 421 422 423 424 425 |
# File 'lib/client_api_builder/router.rb', line 420 def expected_response_code!(response, expected_response_codes, ) return if expected_response_codes.empty? && response.kind_of?(Net::HTTPSuccess) return if expected_response_codes.include?(response.code) raise(::ClientApiBuilder::UnexpectedResponse.new("unexpected response code #{response.code}", response)) end |
#get_retry_request_max_retries(options) ⇒ Object
483 484 485 |
# File 'lib/client_api_builder/router.rb', line 483 def get_retry_request_max_retries() [:retries] || self.class.[:max_retries] || 1 end |
#get_retry_request_sleep_time(e, options) ⇒ Object
479 480 481 |
# File 'lib/client_api_builder/router.rb', line 479 def get_retry_request_sleep_time(e, ) [:sleep] || self.class.[:sleep] || 0.05 end |
#handle_response(response, options, &block) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/client_api_builder/router.rb', line 431 def handle_response(response, , &block) data = case [:return] when :response response when :body response.body else parse_response(response, ) end if block instance_exec(data, &block) else data end end |
#instrument_request ⇒ Object
457 458 459 460 461 462 |
# File 'lib/client_api_builder/router.rb', line 457 def instrument_request start_time = Time.now yield ensure @total_request_time = Time.now - start_time end |
#log_request_exception(exception) ⇒ Object
499 500 501 |
# File 'lib/client_api_builder/router.rb', line 499 def log_request_exception(exception) ::ClientApiBuilder.logger && ::ClientApiBuilder.logger.error(exception) end |
#parse_response(response, options) ⇒ Object
427 428 429 |
# File 'lib/client_api_builder/router.rb', line 427 def parse_response(response, ) response.body && JSON.parse(response.body) end |
#request_log_message ⇒ Object
503 504 505 506 507 508 509 510 |
# File 'lib/client_api_builder/router.rb', line 503 def method = [:method].to_s.upcase uri = [:uri] response_code = response ? response.code : 'UNKNOWN' duration = (total_request_time * 1000).to_i "#{method} #{uri.scheme}://#{uri.host}#{uri.path}[#{response_code}] took #{duration}ms" end |
#request_wrapper(options) ⇒ Object
487 488 489 490 491 492 493 |
# File 'lib/client_api_builder/router.rb', line 487 def request_wrapper() retry_request() do instrument_request do yield end end end |
#retry_request(options) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/client_api_builder/router.rb', line 464 def retry_request() @request_attempts = 0 max_attempts = get_retry_request_max_retries() begin @request_attempts += 1 yield rescue Exception => e log_request_exception(e) raise(e) if @request_attempts >= max_attempts || !retry_request?(e, ) sleep_time = get_retry_request_sleep_time(e, ) sleep(sleep_time) if sleep_time && sleep_time > 0 retry end end |
#retry_request?(exception, options) ⇒ Boolean
495 496 497 |
# File 'lib/client_api_builder/router.rb', line 495 def retry_request?(exception, ) true end |
#root_router ⇒ Object
449 450 451 |
# File 'lib/client_api_builder/router.rb', line 449 def root_router self end |