Class: Raif::ModelFileInput

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/raif/model_file_input.rb

Direct Known Subclasses

ModelImageInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: nil, url: nil) ⇒ ModelFileInput

Returns a new instance of ModelFileInput.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/raif/model_file_input.rb', line 12

def initialize(input: nil, url: nil)
  raise ArgumentError, "You must provide either an input or a URL" if input.blank? && url.blank?
  raise ArgumentError, "Provide either input or URL, not both" if input.present? && url.present?

  super(input: input, url: url)

  if url.present?
    @source_type = :url
  elsif input.present?
    @source_type = :file_content
    process_input!
  end

  unless valid?
    raise Raif::Errors::InvalidModelFileInputError, errors.full_messages.join(", ")
  end
end

Instance Attribute Details

#base64_dataObject

Returns the value of attribute base64_data.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def base64_data
  @base64_data
end

#content_typeObject

Returns the value of attribute content_type.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def content_type
  @content_type
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def filename
  @filename
end

#inputObject

Returns the value of attribute input.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def input
  @input
end

#source_typeObject

Returns the value of attribute source_type.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def source_type
  @source_type
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'app/models/raif/model_file_input.rb', line 6

def url
  @url
end

Instance Method Details

#file_bytesObject



34
35
36
# File 'app/models/raif/model_file_input.rb', line 34

def file_bytes
  Base64.strict_decode64(base64_data)
end

#inspectObject



30
31
32
# File 'app/models/raif/model_file_input.rb', line 30

def inspect
  "#<#{self.class.name} input=#{input.inspect} url=#{url.inspect} base64_data=#{base64_data&.truncate(20)} filename=#{filename.inspect} content_type=#{content_type.inspect} source_type=#{source_type.inspect}>" # rubocop:disable Layout/LineLength
end