Class: Secretariat::Attachment

Inherits:
Struct
  • Object
show all
Includes:
Versioner
Defined in:
lib/secretariat/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Versioner

#by_version

Instance Attribute Details

#base64Object

Returns the value of attribute base64

Returns:

  • (Object)

    the current value of base64



22
23
24
# File 'lib/secretariat/attachment.rb', line 22

def base64
  @base64
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



22
23
24
# File 'lib/secretariat/attachment.rb', line 22

def filename
  @filename
end

#type_codeObject

Returns the value of attribute type_code

Returns:

  • (Object)

    the current value of type_code



22
23
24
# File 'lib/secretariat/attachment.rb', line 22

def type_code
  @type_code
end

Instance Method Details

#errorsObject



31
32
33
# File 'lib/secretariat/attachment.rb', line 31

def errors
  @errors
end

#mime_codeObject



60
61
62
63
64
65
# File 'lib/secretariat/attachment.rb', line 60

def mime_code
  type_for = MIME::Types.type_for(filename).first
  return if type_for.nil?

  type_for.content_type
end

#to_xml(xml, attachment_index, version: 2, validate: true) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/secretariat/attachment.rb', line 67

def to_xml(xml, attachment_index, version: 2, validate: true)
  if validate && !valid?
    pp errors
    raise ValidationError.new("Attachment #{attachment_index} is invalid", errors)
  end

  xml['ram'].AdditionalReferencedDocument do
    xml['ram'].IssuerAssignedID filename
    xml['ram'].TypeCode type_code
    xml['ram'].Name filename
    xml['ram'].AttachmentBinaryObject(mimeCode: mime_code, filename: filename) do
      xml.text(base64)
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/secretariat/attachment.rb', line 35

def valid?
  @errors = []

  @errors << "the attribute filename needs to be present" if filename.nil? || filename == ''
  @errors << "the attribute type_code needs to be present" if type_code.nil? || type_code == ''
  @errors << "the attribute base64 needs to be present" if base64.nil? || base64 == ''

  if type_code.to_i != 916
    @errors << "we only support type_code 916"
    return false
  end

  if mime_code.nil?
    @errors << "cannot determine content type for filename: #{filename}"
    return false
  end
  
  if !ALLOWED_MIME_TYPES.include?(mime_code)
    @errors << "the mime_code '#{mime_code}' is not allowed"
    return false
  end

  return true
end