Class: BestType::DcTypeLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/best_type/dc_type_lookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_type_lookup_instance) ⇒ DcTypeLookup

Returns a new instance of DcTypeLookup.



6
7
8
9
# File 'lib/best_type/dc_type_lookup.rb', line 6

def initialize(mime_type_lookup_instance)
  @mime_type_lookup = mime_type_lookup_instance
  @config = @mime_type_lookup.config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/best_type/dc_type_lookup.rb', line 4

def config
  @config
end

Instance Method Details

#for_file_name(file_name_or_path) ⇒ Object



11
12
13
# File 'lib/best_type/dc_type_lookup.rb', line 11

def for_file_name(file_name_or_path)
  for_mime_type(@mime_type_lookup.for_file_name(file_name_or_path))
end

#for_mime_type(mime_type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/best_type/dc_type_lookup.rb', line 15

def for_mime_type(mime_type)
  # Check config overrides first
  dc_type = @config.mime_type_to_dc_type_overrides.fetch(mime_type, nil)
  return dc_type unless dc_type.nil?

  mimes_to_dc = {
    /^image/ => 'StillImage',
    /^video/ => 'MovingImage',
    /^audio/ => 'Sound',
    /^text/ => 'Text',
    /^application\/(pdf|msword)/ => 'Text',
    /excel|spreadsheet|xls|application\/sql/ => 'Dataset',
    /^application/ => 'Software'
  }

  dc_type = mimes_to_dc.find { |pattern, _type_val| mime_type =~ pattern }
  dc_type.last
end