Sha256: 044fe6151984cc6a42f08818812369aebe3c4cef7301e30c74914a16571210d6

Contents?: true

Size: 934 Bytes

Versions: 14

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

module Sentry
  class Attachment
    PathNotFoundError = Class.new(StandardError)

    attr_reader :bytes, :filename, :path, :content_type

    def initialize(bytes: nil, filename: nil, content_type: nil, path: nil)
      @bytes = bytes
      @filename = filename || infer_filename(path)
      @path = path
      @content_type = content_type
    end

    def to_envelope_headers
      { type: "attachment", filename: filename, content_type: content_type, length: payload.bytesize }
    end

    def payload
      @payload ||= if bytes
        bytes
      else
        File.binread(path)
      end
    rescue Errno::ENOENT
      raise PathNotFoundError, "Failed to read attachment file, file not found: #{path}"
    end

    private

    def infer_filename(path)
      if path
        File.basename(path)
      else
        raise ArgumentError, "filename or path is required"
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
sentry-ruby-5.22.3 lib/sentry/attachment.rb
sentry-ruby-core-5.22.3 lib/sentry/attachment.rb
sentry-ruby-core-5.22.2 lib/sentry/attachment.rb
sentry-ruby-5.22.2 lib/sentry/attachment.rb
sentry-ruby-core-5.22.1 lib/sentry/attachment.rb
sentry-ruby-5.22.1 lib/sentry/attachment.rb
sentry-ruby-5.22.0 lib/sentry/attachment.rb
sentry-ruby-core-5.22.0 lib/sentry/attachment.rb
sentry-ruby-5.21.0 lib/sentry/attachment.rb
sentry-ruby-core-5.21.0 lib/sentry/attachment.rb
sentry-ruby-5.20.1 lib/sentry/attachment.rb
sentry-ruby-core-5.20.1 lib/sentry/attachment.rb
sentry-ruby-5.20.0 lib/sentry/attachment.rb
sentry-ruby-core-5.20.0 lib/sentry/attachment.rb