# You can use Kernel#caller
puts caller
# Or if you want to remove bin/bundle, lib/ruby/gems/* etc from the stack.
# you can filter for unique application path entry or other exclusions as needed.
def call_stack(app_path_identifier)
stack = caller.select { |i| i.include?(app_path_identifier) }
{
callee: stack.first.split('/').last.split(':')[0,2].join(':'),
stack: stack
}
end
puts JSON.pretty_generate(call_stack('my_company/my_app'))
# {
# "callee": "base_presenter.rb:83",
# "stack": [
# "/Users/xmen/dev/my_company/my_app/app/presenters/base_presenter.rb:83:in `block (2 levels) in outputs'",
# "/Users/xmen/dev/my_company/my_app/spec/presenters/base_presenter_spec.rb:26:in `call'",
# "/Users/xmen/dev/my_company/my_app/app/presenters/base_presenter.rb:25:in `around_call'",
# "/Users/xmen/dev/my_company/my_app/app/presenters/base_presenter.rb:65:in `present'",
# "/Users/xmen/dev/my_company/my_app/spec/presenters/base_presenter_spec.rb:92:in `block (4 levels) in <top (required)>'",
# "/Users/xmen/dev/my_company/my_app/spec/presenters/base_presenter_spec.rb:93:in `block (4 levels) in <top (required)>'"
# ]
# }