Class: Contrast::Framework::Rails::Support

Inherits:
Object
  • Object
show all
Extended by:
Components::Logger::InstanceMethods, BaseSupport, Patch::Support
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/framework/rails/support.rb

Overview

Used when Rails is present to define framework specific behavior

Constant Summary collapse

RAILS_MODULE_NAME_VERSION =
Gem::Version.new('6.0.0')
AC_INSTANCE =
'action_controller.instance'

Class Method Summary collapse

Methods included from BaseSupport

after_load_patches, application_name, before_load_patches!, collect_routes, current_route_coverage, detection_class, retrieve_request, server_type, streaming?, version

Methods included from Patch::Support

after_load_patches, before_load_patches!

Methods included from Components::Logger::InstanceMethods

cef_logger, logger

Class Method Details

.application_nameObject



30
31
32
33
34
35
36
37
# File 'lib/contrast/framework/rails/support.rb', line 30

def application_name
  app_class = ::Rails.application.cs__class
  # Rails version 6.0.0 deprecated Rails::Application#parent_name, in Rails 6.1.0 that method will be removed
  # entirely and instead we need to use parent_module_name
  return app_class.parent_module_name if Gem::Version.new(::Rails.version) >= RAILS_MODULE_NAME_VERSION

  app_class.parent_name
end

.application_rootObject



39
40
41
# File 'lib/contrast/framework/rails/support.rb', line 39

def application_root
  ::Rails.root
end

.collect_routesArray<Contrast::Agent::Reporting::DiscoveredRoute>



48
49
50
# File 'lib/contrast/framework/rails/support.rb', line 48

def collect_routes
  find_all_routes(::Rails.application, [])
end

.current_route_coverage(request) ⇒ Contrast::Agent::Reporting::RouteCoverage?

Find the current route, based on the provided Request wrapper

Parameters:

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/contrast/framework/rails/support.rb', line 56

def current_route_coverage request
  return unless ::Rails.cs__respond_to?(:application)

  # ActionDispatch::Journey::Path::Pattern::MatchData, Hash, ActionDispatch::Journey::Route, Array<String>
  match, _params, route, path = get_full_route(request.rack_request)
  unless route
    logger.debug("Unable to determine the current route of this request: #{ request.rack_request }")
    return
  end

  original_url = request.rack_request.path_info
  mounted_app = route&.app&.app
  # Route is either the final rails route, or a router that points to a Sinatra controller.
  if mounted_app && Contrast::Framework::Sinatra::Support.sinatra_controller?(mounted_app)
    return mounted_new_sinatra_route(request, match, path, route, original_url)
  end
  if mounted_app && Contrast::Framework::Grape::Support.grape_controller?(mounted_app)
    return mounted_new_grape_route(request, match, path, route, original_url)
  end

  new_route_coverage = Contrast::Agent::Reporting::RouteCoverage.new
  new_route_coverage&.attach_rails_data(route, original_url)
  new_route_coverage
rescue StandardError => e
  logger.error('Unable to determine the current route of this request due to exception: ', e)
  nil
end

.detection_classObject



22
23
24
# File 'lib/contrast/framework/rails/support.rb', line 22

def detection_class
  'Rails'
end

.retrieve_request(env) ⇒ ::ActionDispatch::Request

Copy a request for modification.

Parameters:

  • env (::ActionDispatch::Request)

    original env.

Returns:

  • (::ActionDispatch::Request)

    a copy of original env with rails env merged.



88
89
90
91
# File 'lib/contrast/framework/rails/support.rb', line 88

def retrieve_request env
  rails_env = ::Rails.application.env_config.merge(env)
  ::ActionDispatch::Request.new(rails_env || env)
end

.server_typeObject



43
44
45
# File 'lib/contrast/framework/rails/support.rb', line 43

def server_type
  'rails'
end

.streaming?(env) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/contrast/framework/rails/support.rb', line 94

def streaming? env
  return false unless defined?(::ActionController::Live)

  env[AC_INSTANCE].cs__class.included_modules.include?(::ActionController::Live)
end

.versionObject



26
27
28
# File 'lib/contrast/framework/rails/support.rb', line 26

def version
  ::Rails.version
end