Class: OpenRosa::MediaFile

Inherits:
Object
  • Object
show all
Defined in:
lib/open_rosa/media_file.rb

Overview

Represents a media file (image, audio, video, entity list) associated with a form. Media files are listed in the form's manifest and downloaded separately from the form definition.

Examples:

Create a media file with manual hash

media_file = OpenRosa::MediaFile.new(
  filename: "images/logo.png",
  hash: "md5:abc123def456",
  download_url: "https://example.com/media/logo.png"
)

Create a media file with automatic hash generation

media_file = OpenRosa::MediaFile.new(
  filename: "images/logo.png",
  file: "/path/to/logo.png",
  download_url: "https://example.com/media/logo.png"
)

Create an entity list media file

media_file = OpenRosa::MediaFile.new(
  filename: "entities.csv",
  hash: "md5:xyz789",
  download_url: "https://example.com/entities.csv",
  type: "entityList",
  integrity_url: "https://example.com/entities/integrity"
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename:, download_url:, hash: nil, file: nil, type: nil, integrity_url: nil) ⇒ MediaFile

Creates a new MediaFile

rubocop:disable Metrics/MethodLength, Metrics/ParameterLists

Parameters:

  • Unrooted file path (no drive letters, no absolute paths, no backslashes)

  • (defaults to: nil)

    MD5 hash in format "md5:..." (auto-generated if file provided)

  • (defaults to: nil)

    Path to file for hash generation

  • Full URI for downloading the file

  • (defaults to: nil)

    Optional type (e.g., "entityList")

  • (defaults to: nil)

    Required if type="entityList"

Raises:

  • if filename is invalid, or if hash/file not provided, or entityList missing integrity_url



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/open_rosa/media_file.rb', line 44

def initialize(filename:, download_url:, hash: nil, file: nil, type: nil, integrity_url: nil)
  # rubocop:enable Metrics/ParameterLists
  @filename = validate_filename(filename)
  @download_url = download_url
  @type = type
  @integrity_url = integrity_url

  # Hash can be provided or generated from file
  if hash
    @hash = hash
  elsif file
    @hash = generate_hash(file)
  else
    raise ArgumentError, "Either hash or file parameter must be provided"
  end

  # Validate entityList requirements
  validate_entity_list if type == "entityList"
end

Instance Attribute Details

#download_urlObject (readonly)

Returns the value of attribute download_url.



32
33
34
# File 'lib/open_rosa/media_file.rb', line 32

def download_url
  @download_url
end

#filenameObject (readonly)

Returns the value of attribute filename.



32
33
34
# File 'lib/open_rosa/media_file.rb', line 32

def filename
  @filename
end

#hashObject (readonly)

Returns the value of attribute hash.



32
33
34
# File 'lib/open_rosa/media_file.rb', line 32

def hash
  @hash
end

#integrity_urlObject (readonly)

Returns the value of attribute integrity_url.



32
33
34
# File 'lib/open_rosa/media_file.rb', line 32

def integrity_url
  @integrity_url
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/open_rosa/media_file.rb', line 32

def type
  @type
end