class TicketAbstractorClient::Base::Attachment

Constants

SANITIZE_REGEXP

Carrierwave uses the same regexp

Attributes

communications_stack[R]
data_hash[R]
external_created_at[R]
file_path[R]

Public Class Methods

fetch() click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 10
def fetch
  method_not_implemented __method__
end
new(opts) click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 37
def initialize(opts)
  @file_path = opts[:file_path]
  @external_created_at = opts[:external_created_at]
  @data_hash = opts[:data_hash]
  @communications_stack = opts[:communications_stack] || []
  set_data_hash! if @data_hash.blank?
end

Protected Class Methods

method_not_implemented(method_name) click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 16
def method_not_implemented(method_name)
  raise Errors::NotImplementedError, "#{self}##{method_name} is not implemented"
end
save_content(content, original_filename) click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 20
def save_content(content, original_filename)
  tmp_file = Tempfile.new(Time.now.to_s)
  tmp_file.binmode
  tmp_file.write(content)
  tmp_file.close
  set_original_name(tmp_file.path, original_filename)
end
set_original_name(filepath, original_name) click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 28
def set_original_name(filepath, original_name)
  sanitized_name = original_name.gsub(SANITIZE_REGEXP, "_")
  tmp_basename = File.basename(filepath)
  tmp_path = filepath.gsub(tmp_basename, '')
  FileUtils.mv(filepath, File.join(tmp_path, sanitized_name))
  File.join(tmp_path, sanitized_name)
end

Public Instance Methods

set_data_hash!() click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 49
def set_data_hash!
  return if self.file_path.blank?
  file = File.new(self.file_path, 'rb')
  @data_hash = Digest::SHA512.hexdigest(file.read)
  file.close
end
sync!() click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 45
def sync!
  self.class.method_not_implemented __method__
end
to_hash() click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 56
def to_hash
  { file_path: @file_path, external_created_at: @external_created_at, data_hash: @data_hash }
end
to_json() click to toggle source
# File lib/ticket_abstractor_client/base/attachment.rb, line 60
def to_json
  self.to_hash.to_json
end