Class: GovukPublishingComponents::Presenters::Attachment::SupportedContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_publishing_components/presenters/attachment.rb

Constant Summary collapse

TYPES =
[
  { content_type: "application/msword", name: "MS Word Document", document: true }.freeze, # doc
  { content_type: "application/pdf", abbr: "PDF", name: "Portable Document Format", document: true }.freeze,
  { content_type: "application/postscript", extension: ".ps", abbr: "PS", name: "PostScript" }.freeze,
  { content_type: "application/postscript", extension: ".eps", abbr: "EPS", name: "Encapsulated PostScript" }.freeze,
  { content_type: "application/rtf", abbr: "RTF", name: "Rich Text Format" }.freeze,
  { content_type: "application/vnd.ms-excel", name: "MS Excel Spreadsheet", spreadsheet: true }.freeze,
  { content_type: "application/vnd.ms-excel.sheet.macroenabled.12", abbr: "XLSM", name: "MS Excel Macro-Enabled Workbook" }.freeze,
  { content_type: "application/vnd.ms-powerpoint", name: "MS Powerpoint Presentation" }.freeze, # ppt
  { content_type: "application/vnd.oasis.opendocument.presentation", abbr: "ODP", name: "OpenDocument Presentation", opendocument: true }.freeze,
  { content_type: "application/vnd.oasis.opendocument.spreadsheet", abbr: "ODS", name: "OpenDocument Spreadsheet", opendocument: true, spreadsheet: true }.freeze,
  { content_type: "application/vnd.oasis.opendocument.text", abbr: "ODT", name: "OpenDocument Text document", opendocument: true, document: true }.freeze,
  { content_type: "application/vnd.openxmlformats-officedocument.presentationml.presentation", name: "MS Powerpoint Presentation" }.freeze, # pptx
  { content_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name: "MS Excel Spreadsheet", spreadsheet: true }.freeze, # xlsx
  { content_type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", name: "MS Word Document", document: true }.freeze, # docx
  { content_type: "application/xml", abbr: "XML", name: "XML Document" }.freeze,
  { content_type: "image/gif", abbr: "GIF", name: "Graphics Interchange Format" }.freeze,
  { content_type: "image/jpeg", name: "JPEG" }.freeze,
  { content_type: "image/png", abbr: "PNG", name: "Portable Network Graphic" }.freeze,
  { content_type: "image/vnd.dxf", abbr: "DXF", name: "AutoCAD Drawing Exchange Format" }.freeze,
  { content_type: "text/csv", abbr: "CSV", name: "Comma-separated Values", spreadsheet: true }.freeze,
  { content_type: "text/plain", name: "Plain Text" }.freeze,
  { content_type: "text/xml", extension: ".xml", abbr: "XML", name: "XML Document" }.freeze,
  { content_type: "text/xml", extension: ".xsd", abbr: "XSD", name: "XML Schema" }.freeze,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_type_data) ⇒ SupportedContentType

Returns a new instance of SupportedContentType.



93
94
95
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 93

def initialize(content_type_data)
  @content_type_data = content_type_data
end

Instance Attribute Details

#content_type_dataObject (readonly)

Returns the value of attribute content_type_data.



51
52
53
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 51

def content_type_data
  @content_type_data
end

Class Method Details

.find(content_type, extension = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 79

def self.find(content_type, extension = nil)
  matching_types = TYPES.select { |type| type[:content_type] == content_type }

  return UnsupportedContentType.new(content_type: content_type) if matching_types.empty?

  extension_match = if matching_types.length > 1
                      matching_types.find { |type| type[:extension] == extension }
                    end

  content_type = extension_match || matching_types.first

  new(content_type)
end

Instance Method Details

#abbrObject



101
102
103
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 101

def abbr
  content_type_data[:abbr]
end

#content_typeObject



97
98
99
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 97

def content_type
  content_type_data[:content_type]
end

#document?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 113

def document?
  !!content_type_data[:document]
end

#nameObject



105
106
107
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 105

def name
  content_type_data[:name]
end

#opendocument?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 109

def opendocument?
  !!content_type_data[:opendocument]
end

#spreadsheet?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/govuk_publishing_components/presenters/attachment.rb', line 117

def spreadsheet?
  !!content_type_data[:spreadsheet]
end