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
350 351 352 |
# File 'lib/client_api_builder/router.rb', line 350 def base_url self.class.base_url end |
#build_body(body, options) ⇒ Object
403 404 405 406 407 408 409 410 |
# File 'lib/client_api_builder/router.rb', line 403 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
374 375 376 377 378 379 380 |
# File 'lib/client_api_builder/router.rb', line 374 def () if [:connection_options] self.class..merge([:connection_options]) else self.class. end end |
#build_headers(options) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/client_api_builder/router.rb', line 354 def build_headers() headers = {} add_header_proc = proc do |name, value| headers[name] = if value.is_a?(Proc) instance_eval(&value) elsif value.is_a?(Symbol) 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
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/client_api_builder/router.rb', line 382 def build_query(query, ) query_params = {} add_query_param_proc = proc do |name, value| query_params[name] = if value.is_a?(Proc) instance_eval(&value) elsif value.is_a?(Symbol) 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
412 413 414 415 416 |
# File 'lib/client_api_builder/router.rb', line 412 def build_uri(path, query, ) uri = URI(base_url + path) uri.query = build_query(query, ) uri end |
#escape_path(path) ⇒ Object
451 452 453 |
# File 'lib/client_api_builder/router.rb', line 451 def escape_path(path) path end |
#expected_response_code!(response, expected_response_codes, options) ⇒ Object
418 419 420 421 422 423 |
# File 'lib/client_api_builder/router.rb', line 418 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
481 482 483 |
# File 'lib/client_api_builder/router.rb', line 481 def get_retry_request_max_retries() [:retries] || self.class.[:max_retries] || 1 end |
#get_retry_request_sleep_time(e, options) ⇒ Object
477 478 479 |
# File 'lib/client_api_builder/router.rb', line 477 def get_retry_request_sleep_time(e, ) [:sleep] || self.class.[:sleep] || 0.05 end |
#handle_response(response, options, &block) ⇒ Object
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/client_api_builder/router.rb', line 429 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
455 456 457 458 459 460 |
# File 'lib/client_api_builder/router.rb', line 455 def instrument_request start_time = Time.now yield ensure @total_request_time = Time.now - start_time end |
#log_request_exception(exception) ⇒ Object
497 498 499 |
# File 'lib/client_api_builder/router.rb', line 497 def log_request_exception(exception) ::ClientApiBuilder.logger && ::ClientApiBuilder.logger.error(exception) end |
#parse_response(response, options) ⇒ Object
425 426 427 |
# File 'lib/client_api_builder/router.rb', line 425 def parse_response(response, ) response.body && JSON.parse(response.body) end |
#request_log_message ⇒ Object
501 502 503 504 505 506 507 508 |
# File 'lib/client_api_builder/router.rb', line 501 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
485 486 487 488 489 490 491 |
# File 'lib/client_api_builder/router.rb', line 485 def request_wrapper() retry_request() do instrument_request do yield end end end |
#retry_request(options) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/client_api_builder/router.rb', line 462 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
493 494 495 |
# File 'lib/client_api_builder/router.rb', line 493 def retry_request?(exception, ) true end |
#root_router ⇒ Object
447 448 449 |
# File 'lib/client_api_builder/router.rb', line 447 def root_router self end |