Class: DebugBundle::Transport::FileTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/debugbundle/transport.rb

Constant Summary collapse

FILE_MODE =
0o600
DIRECTORY_MODE =
0o700

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ FileTransport

Returns a new instance of FileTransport.



116
117
118
119
# File 'lib/debugbundle/transport.rb', line 116

def initialize(directory)
  @raw_directory = directory.to_s
  @directory = File.expand_path(directory)
end

Instance Method Details

#call(request) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/debugbundle/transport.rb', line 121

def call(request)
  ensure_secure_directory!(@directory)

  timestamp = Time.now.utc.strftime('%Y%m%dT%H%M%S')
  service_fragment = request.fetch(:service_name, 'service').to_s.gsub(/[^a-zA-Z0-9_-]+/, '-')
  token_fragment = SecureRandom.hex(12)
  temp_path = File.join(@directory, "#{timestamp}-#{token_fragment}.tmp")
  final_path = File.join(@directory, "#{timestamp}-#{token_fragment}-#{service_fragment}.events.json")

  payload = JSON.generate(
    request.fetch(:events)
  )

  write_secure_file(temp_path, payload)
  reject_symlink!(final_path)
  File.rename(temp_path, final_path)
  File.chmod(FILE_MODE, final_path)

  Result.new(status_code: 202)
rescue StandardError
  Result.new(status_code: 500)
ensure
  File.delete(temp_path) if defined?(temp_path) && temp_path && File.exist?(temp_path)
end